common: make a common interface for creating a server socket

This commit is contained in:
Tyler Wilding
2022-04-26 21:32:46 -04:00
parent d41fe7e6ad
commit fe36687366
19 changed files with 416 additions and 325 deletions
+35 -35
View File
@@ -15,12 +15,12 @@ TEST(Listener, ListenerCreation) {
}
TEST(Listener, DeciCreation) {
Deci2Server s(always_false);
Deci2Server s(always_false, DECI2_PORT);
}
TEST(Listener, DeciInit) {
Deci2Server s(always_false);
EXPECT_TRUE(s.init());
Deci2Server s(always_false, DECI2_PORT);
EXPECT_TRUE(s.init_server());
}
/*!
@@ -38,61 +38,61 @@ TEST(Listener, ListenToNothing) {
}
TEST(Listener, DeciCheckNoListener) {
Deci2Server s(always_false);
EXPECT_TRUE(s.init());
EXPECT_FALSE(s.check_for_listener());
EXPECT_FALSE(s.check_for_listener());
EXPECT_FALSE(s.check_for_listener());
Deci2Server s(always_false, DECI2_PORT);
EXPECT_TRUE(s.init_server());
EXPECT_FALSE(s.wait_for_connection());
EXPECT_FALSE(s.wait_for_connection());
EXPECT_FALSE(s.wait_for_connection());
}
TEST(Listener, CheckConnectionStaysAlive) {
Deci2Server s(always_false);
EXPECT_TRUE(s.init());
EXPECT_FALSE(s.check_for_listener());
Deci2Server s(always_false, DECI2_PORT);
EXPECT_TRUE(s.init_server());
EXPECT_FALSE(s.wait_for_connection());
Listener l;
EXPECT_FALSE(s.check_for_listener());
EXPECT_FALSE(s.wait_for_connection());
bool connected = l.connect_to_target();
EXPECT_TRUE(connected);
// TODO - some sort of backoff and retry would be better
while (connected && !s.check_for_listener()) {
while (connected && !s.wait_for_connection()) {
}
EXPECT_TRUE(s.check_for_listener());
EXPECT_TRUE(s.wait_for_connection());
std::this_thread::sleep_for(std::chrono::milliseconds(500));
EXPECT_TRUE(s.check_for_listener());
EXPECT_TRUE(s.wait_for_connection());
EXPECT_TRUE(l.is_connected());
}
TEST(Listener, DeciThenListener) {
for (int i = 0; i < 3; i++) {
Deci2Server s(always_false);
EXPECT_TRUE(s.init());
EXPECT_FALSE(s.check_for_listener());
EXPECT_FALSE(s.check_for_listener());
EXPECT_FALSE(s.check_for_listener());
Deci2Server s(always_false, DECI2_PORT);
EXPECT_TRUE(s.init_server());
EXPECT_FALSE(s.wait_for_connection());
EXPECT_FALSE(s.wait_for_connection());
EXPECT_FALSE(s.wait_for_connection());
Listener l;
EXPECT_FALSE(s.check_for_listener());
EXPECT_FALSE(s.check_for_listener());
EXPECT_FALSE(s.wait_for_connection());
EXPECT_FALSE(s.wait_for_connection());
bool connected = l.connect_to_target();
EXPECT_TRUE(connected);
// TODO - some sort of backoff and retry would be better
while (connected && !s.check_for_listener()) {
while (connected && !s.wait_for_connection()) {
}
EXPECT_TRUE(s.check_for_listener());
EXPECT_TRUE(s.wait_for_connection());
}
}
TEST(Listener, DeciThenListener2) {
for (int i = 0; i < 3; i++) {
Deci2Server s(always_false);
EXPECT_TRUE(s.init());
EXPECT_FALSE(s.check_for_listener());
EXPECT_FALSE(s.check_for_listener());
EXPECT_FALSE(s.check_for_listener());
Deci2Server s(always_false, DECI2_PORT);
EXPECT_TRUE(s.init_server());
EXPECT_FALSE(s.wait_for_connection());
EXPECT_FALSE(s.wait_for_connection());
EXPECT_FALSE(s.wait_for_connection());
Listener l;
EXPECT_FALSE(s.check_for_listener());
EXPECT_FALSE(s.check_for_listener());
EXPECT_FALSE(s.wait_for_connection());
EXPECT_FALSE(s.wait_for_connection());
EXPECT_TRUE(l.connect_to_target());
}
}
@@ -101,13 +101,13 @@ TEST(Listener, ListenerThenDeci) {
for (int i = 0; i < 3; i++) {
Listener l;
EXPECT_FALSE(l.connect_to_target());
Deci2Server s(always_false);
EXPECT_TRUE(s.init());
EXPECT_FALSE(s.check_for_listener());
Deci2Server s(always_false, DECI2_PORT);
EXPECT_TRUE(s.init_server());
EXPECT_FALSE(s.wait_for_connection());
bool connected = l.connect_to_target();
EXPECT_TRUE(connected);
// TODO - some sort of backoff and retry would be better
while (connected && !s.check_for_listener()) {
while (connected && !s.wait_for_connection()) {
}
}
}