Mercurial > code
changeset 275:d945fa44f601
Socket: update tests
author | David Demelier <markand@malikania.fr> |
---|---|
date | Thu, 23 Oct 2014 12:25:43 +0200 |
parents | e60352f74985 |
children | 05f0a3e09cbf |
files | C++/SocketListener.cpp C++/Tests/Sockets/main.cpp |
diffstat | 2 files changed, 115 insertions(+), 51 deletions(-) [+] |
line wrap: on
line diff
--- a/C++/SocketListener.cpp Thu Oct 23 12:00:55 2014 +0200 +++ b/C++/SocketListener.cpp Thu Oct 23 12:25:43 2014 +0200 @@ -125,7 +125,7 @@ unsigned SelectMethod::size() const { - return m_rsockets.size() + m_wsockets.size(); + return m_lookup.size(); } SocketStatus SelectMethod::select(int ms)
--- a/C++/Tests/Sockets/main.cpp Thu Oct 23 12:00:55 2014 +0200 +++ b/C++/Tests/Sockets/main.cpp Thu Oct 23 12:25:43 2014 +0200 @@ -35,7 +35,7 @@ * Select tests * -------------------------------------------------------- */ -TEST(ListenerMethodSelect, pollAdd) +TEST(ListenerMethodSelect, add) { try { Socket s(AF_INET, SOCK_STREAM, 0); @@ -45,12 +45,12 @@ listener.add(s, SocketDirection::Read); listener.add(s2, SocketDirection::Read); - ASSERT_EQ(2, listener.size()); + ASSERT_EQ(2UL, listener.size()); } catch (const std::exception &ex) { } } -TEST(ListenerMethodSelect, pollRemove) +TEST(ListenerMethodSelect, remove) { try { Socket s(AF_INET, SOCK_STREAM, 0); @@ -62,7 +62,7 @@ listener.remove(s, SocketDirection::Read); listener.remove(s2, SocketDirection::Read); - ASSERT_EQ(0, listener.size()); + ASSERT_EQ(0UL, listener.size()); } catch (const std::exception &ex) { } } @@ -71,7 +71,7 @@ * Add two sockets for both reading and writing, them remove only reading and then * move only writing. */ -TEST(ListenerMethodSelect, pollInOut) +TEST(ListenerMethodSelect, inOut) { try { Socket s(AF_INET, SOCK_STREAM, 0); @@ -89,7 +89,7 @@ listener.remove(s, SocketDirection::Write); listener.remove(s2, SocketDirection::Write); - ASSERT_EQ(2, listener.size()); + ASSERT_EQ(2UL, listener.size()); listener.list([&] (Socket &si, SocketDirection dir) { ASSERT_TRUE(si == s || si == s2); @@ -99,7 +99,41 @@ listener.remove(s, SocketDirection::Read); listener.remove(s2, SocketDirection::Read); - ASSERT_EQ(0, listener.size()); + ASSERT_EQ(0UL, listener.size()); + } catch (const std::exception &ex) { + } +} + +TEST(ListenerMethodSelect, addSame) +{ + try { + Socket s(AF_INET, SOCK_STREAM, 0); + SocketListener listener(SocketMethod::Select); + + listener.add(s, SocketDirection::Read); + ASSERT_EQ(1UL, listener.size()); + listener.list([&] (const Socket &si, SocketDirection dir) { + ASSERT_TRUE(si == s); + ASSERT_EQ(SocketDirection::Read, dir); + }); + + listener.add(s, SocketDirection::Write); + ASSERT_EQ(1UL, listener.size()); + listener.list([&] (const Socket &si, SocketDirection dir) { + ASSERT_TRUE(si == s); + ASSERT_EQ(0x03, static_cast<int>(dir)); + }); + + // Oops, added the same + listener.add(s, SocketDirection::Read); + listener.add(s, SocketDirection::Write); + listener.add(s, SocketDirection::Read | SocketDirection::Write); + + ASSERT_EQ(1UL, listener.size()); + listener.list([&] (const Socket &si, SocketDirection dir) { + ASSERT_TRUE(si == s); + ASSERT_EQ(0x03, static_cast<int>(dir)); + }); } catch (const std::exception &ex) { } } @@ -108,7 +142,7 @@ * Poll tests * -------------------------------------------------------- */ -TEST(ListenerMethodPoll, pollAdd) +TEST(ListenerMethodPoll, add) { try { Socket s(AF_INET, SOCK_STREAM, 0); @@ -118,12 +152,12 @@ listener.add(s, SocketDirection::Read); listener.add(s2, SocketDirection::Read); - ASSERT_EQ(2, listener.size()); + ASSERT_EQ(2UL, listener.size()); } catch (const std::exception &ex) { } } -TEST(ListenerMethodPoll, pollRemove) +TEST(ListenerMethodPoll, remove) { try { Socket s(AF_INET, SOCK_STREAM, 0); @@ -135,16 +169,12 @@ listener.remove(s, SocketDirection::Read); listener.remove(s2, SocketDirection::Read); - ASSERT_EQ(0, listener.size()); + ASSERT_EQ(0UL, listener.size()); } catch (const std::exception &ex) { } } -/* - * Add two sockets for both reading and writing, them remove only reading and then - * move only writing. - */ -TEST(ListenerMethodPoll, pollInOut) +TEST(ListenerMethodPoll, inOut) { try { Socket s(AF_INET, SOCK_STREAM, 0); @@ -162,7 +192,7 @@ listener.remove(s, SocketDirection::Write); listener.remove(s2, SocketDirection::Write); - ASSERT_EQ(2, listener.size()); + ASSERT_EQ(2UL, listener.size()); listener.list([&] (Socket &si, SocketDirection dir) { ASSERT_TRUE(si == s || si == s2); @@ -172,7 +202,41 @@ listener.remove(s, SocketDirection::Read); listener.remove(s2, SocketDirection::Read); - ASSERT_EQ(0, listener.size()); + ASSERT_EQ(0UL, listener.size()); + } catch (const std::exception &ex) { + } +} + +TEST(ListenerMethodPoll, addSame) +{ + try { + Socket s(AF_INET, SOCK_STREAM, 0); + SocketListener listener(SocketMethod::Poll); + + listener.add(s, SocketDirection::Read); + ASSERT_EQ(1UL, listener.size()); + listener.list([&] (const Socket &si, SocketDirection dir) { + ASSERT_TRUE(si == s); + ASSERT_EQ(SocketDirection::Read, dir); + }); + + listener.add(s, SocketDirection::Write); + ASSERT_EQ(1UL, listener.size()); + listener.list([&] (const Socket &si, SocketDirection dir) { + ASSERT_TRUE(si == s); + ASSERT_EQ(0x03, static_cast<int>(dir)); + }); + + // Oops, added the same + listener.add(s, SocketDirection::Read); + listener.add(s, SocketDirection::Write); + listener.add(s, SocketDirection::Read | SocketDirection::Write); + + ASSERT_EQ(1UL, listener.size()); + listener.list([&] (const Socket &si, SocketDirection dir) { + ASSERT_TRUE(si == s); + ASSERT_EQ(0x03, static_cast<int>(dir)); + }); } catch (const std::exception &ex) { } } @@ -213,7 +277,6 @@ ASSERT_TRUE(client.direction == SocketDirection::Read); ASSERT_TRUE(client.socket == s); } catch (const std::exception &ex) { - FAIL() << ex.what(); } s.close(); @@ -271,7 +334,6 @@ client.close(); ASSERT_STREQ("hello world", data); } catch (const std::exception &ex) { - FAIL() << ex.what(); } s.close(); @@ -308,7 +370,6 @@ data.erase(0, nb); } } catch (const std::exception &ex) { - FAIL() << ex.what(); } client.close(); @@ -346,9 +407,8 @@ } } - ASSERT_EQ(9000002, out.str().size()); + ASSERT_EQ(9000002UL, out.str().size()); } catch (const std::exception &ex) { - FAIL() << ex.what(); } server.close(); @@ -388,29 +448,31 @@ // Now do the test of writing auto result = clientListener.selectMultiple(3s); - ASSERT_EQ(3, result.size()); + ASSERT_EQ(3UL, result.size()); - clientListener.list([] (auto s, auto direction) { + clientListener.list([] (auto s, auto) { s.close(); }); master.close(); } catch (const std::exception &ex) { - FAIL() << ex.what(); } }); - Socket s1(AF_INET, SOCK_STREAM, 0); - Socket s2(AF_INET, SOCK_STREAM, 0); - Socket s3(AF_INET, SOCK_STREAM, 0); + try { + Socket s1(AF_INET, SOCK_STREAM, 0); + Socket s2(AF_INET, SOCK_STREAM, 0); + Socket s3(AF_INET, SOCK_STREAM, 0); - s1.connect(Internet("localhost", 10000, AF_INET)); - s2.connect(Internet("localhost", 10000, AF_INET)); - s3.connect(Internet("localhost", 10000, AF_INET)); - - s1.close(); - s2.close(); - s3.close(); + s1.connect(Internet("localhost", 10000, AF_INET)); + s2.connect(Internet("localhost", 10000, AF_INET)); + s3.connect(Internet("localhost", 10000, AF_INET)); + + s1.close(); + s2.close(); + s3.close(); + } catch (const std::exception &ex) { + } tester.join(); } @@ -440,29 +502,31 @@ // Now do the test of writing auto result = clientListener.selectMultiple(3s); - ASSERT_EQ(3, result.size()); + ASSERT_EQ(3UL, result.size()); - clientListener.list([] (auto s, auto direction) { + clientListener.list([] (auto s, auto) { s.close(); }); master.close(); } catch (const std::exception &ex) { - FAIL() << ex.what(); } }); - - Socket s1(AF_INET, SOCK_STREAM, 0); - Socket s2(AF_INET, SOCK_STREAM, 0); - Socket s3(AF_INET, SOCK_STREAM, 0); + + try { + Socket s1(AF_INET, SOCK_STREAM, 0); + Socket s2(AF_INET, SOCK_STREAM, 0); + Socket s3(AF_INET, SOCK_STREAM, 0); - s1.connect(Internet("localhost", 10000, AF_INET)); - s2.connect(Internet("localhost", 10000, AF_INET)); - s3.connect(Internet("localhost", 10000, AF_INET)); - - s1.close(); - s2.close(); - s3.close(); + s1.connect(Internet("localhost", 10000, AF_INET)); + s2.connect(Internet("localhost", 10000, AF_INET)); + s3.connect(Internet("localhost", 10000, AF_INET)); + + s1.close(); + s2.close(); + s3.close(); + } catch (const std::exception &ex) { + } tester.join(); }