From: Nadav Har'El <n...@scylladb.com>
Committer: Nadav Har'El <n...@scylladb.com>
Branch: master

Fix tests/tst-tcp-listen

tests/tst-tcp-listen has two separate tests which listen on the same port.
Depending on timing, the second test may start while there are still some
remaining entries in the socket table (despite the file descriptor already
having been closed). We need to use SO_REUSEADDR to allow the second listen
to succeed every time.

Also, before this patch, even thought the second test sometimes failed,
nobody noticed because the test silently did exit(1). That's not how a
test with Boost framework should fail. Fixed that too.

Fixes #888

Signed-off-by: Nadav Har'El <n...@scylladb.com>

---
diff --git a/tests/tst-tcp-listen.cc b/tests/tst-tcp-listen.cc
--- a/tests/tst-tcp-listen.cc
+++ b/tests/tst-tcp-listen.cc
@@ -54,6 +54,9 @@ BOOST_AUTO_TEST_CASE(test_connections_get_accepted_even_when_backlog_gets_overfl

     sockets_to_close.push_back(listen_s);

+    int reuse = 1;
+ BOOST_REQUIRE(setsockopt(listen_s, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse)) == 0);
+
     struct sockaddr_in laddr = {};
     laddr.sin_family = AF_INET;
     laddr.sin_addr.s_addr = htonl(INADDR_ANY);
@@ -104,16 +107,21 @@ BOOST_AUTO_TEST_CASE(test_clients_are_not_reset_when_backlog_is_full_and_they_wr
     auto listen_s = socket(AF_INET, SOCK_STREAM, 0);
     if (listen_s < 0) {
         perror("socket");
+        BOOST_REQUIRE(false);
         exit(1);
     }

+    int reuse = 1;
+ BOOST_REQUIRE(setsockopt(listen_s, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse)) == 0);
+
     struct sockaddr_in laddr = {};
     laddr.sin_family = AF_INET;
     laddr.sin_addr.s_addr = htonl(INADDR_ANY);
     laddr.sin_port = htons(LISTEN_PORT);

     if (bind(listen_s, (struct sockaddr *) &laddr, sizeof(laddr)) < 0) {
         perror("bind");
+        BOOST_REQUIRE(false);
         exit(1);
     }

@@ -130,6 +138,7 @@ BOOST_AUTO_TEST_CASE(test_clients_are_not_reset_when_backlog_is_full_and_they_wr
             int s = socket(AF_INET, SOCK_STREAM, 0);
             if (s < 0) {
                 perror("socket");
+                BOOST_REQUIRE(false);
                 exit(1);
             }

@@ -142,6 +151,7 @@ BOOST_AUTO_TEST_CASE(test_clients_are_not_reset_when_backlog_is_full_and_they_wr

             if (connect(s, (struct sockaddr *)&raddr, sizeof(raddr))) {
                 perror("connect");
+                BOOST_REQUIRE(false);
                 exit(1);
             }

@@ -152,6 +162,7 @@ BOOST_AUTO_TEST_CASE(test_clients_are_not_reset_when_backlog_is_full_and_they_wr
                     break;
                 }
             }
+            close(s);
         }));
     }

@@ -170,6 +181,7 @@ BOOST_AUTO_TEST_CASE(test_clients_are_not_reset_when_backlog_is_full_and_they_wr
                 int bytes = read(client_s, &buf, sizeof(buf));
                 if (bytes < 0) {
                     perror("read");
+                    BOOST_REQUIRE(false);
                     exit(1);
                 }
             }

--
You received this message because you are subscribed to the Google Groups "OSv 
Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to osv-dev+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to