Bug 891551 - Part 7: Fix some intermittent failures in multi_tcp_socket_unittest. r=ekr
authorByron Campen [:bwc] <docfaraday@gmail.com>
Mon, 09 Jun 2014 15:04:42 -0700
changeset 249702 057cd9bbfbd31655284527a95d66ecbccbb37b8c
parent 249701 4c413795f28ab425eb581db5dac7334ca2981e98
child 249703 a468baf1358efbcd15bfebcdad90108e178cc573
push id28936
push userryanvm@gmail.com
push dateFri, 19 Jun 2015 20:34:42 +0000
treeherdermozilla-central@c319f262ce3e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersekr
bugs891551
milestone41.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 891551 - Part 7: Fix some intermittent failures in multi_tcp_socket_unittest. r=ekr
media/mtransport/test/multi_tcp_socket_unittest.cpp
--- a/media/mtransport/test/multi_tcp_socket_unittest.cpp
+++ b/media/mtransport/test/multi_tcp_socket_unittest.cpp
@@ -5,16 +5,18 @@
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include <iostream>
 #include <vector>
 
 #include "mozilla/Scoped.h"
 #include "mozilla/Atomics.h"
 #include "runnable_utils.h"
+#include "nss.h"
+#include "pk11pub.h"
 
 extern "C" {
 #include "nr_api.h"
 #include "nr_socket.h"
 #include "transport_addr.h"
 #include "ice_ctx.h"
 #include "nr_socket_multi_tcp.h"
 }
@@ -61,20 +63,35 @@ class MultiTcpSocketTest : public ::test
   void Shutdown_s() {
     ice_ctx_ = nullptr;
     for (std::vector<nr_socket *>::iterator it=socks.begin();
          it!=socks.end(); ++it) {
       nr_socket_destroy(&(*it));
     }
   }
 
+  static uint16_t GetRandomPort() {
+    uint16_t result;
+    if (PK11_GenerateRandom((unsigned char*)&result, 2) != SECSuccess) {
+      MOZ_ASSERT(false);
+      return 0;
+    }
+    return result;
+  }
+
+  static uint16_t EnsureEphemeral(uint16_t port) {
+    // IANA ephemeral port range (49152 to 65535)
+    return port | 49152;
+  }
+
   void Create_s(nr_socket_tcp_type tcp_type, nr_socket *stun_server_socket,
                 int use_framing, nr_socket **sock) {
     nr_transport_addr local;
-    static unsigned short port_s = 40000;
+    // Get start of port range for test
+    static unsigned short port_s = GetRandomPort();
     int r;
 
     if (stun_server_socket) {
       nr_transport_addr stun_addr;
       int port;
       char stun_host[1000];
 
       r = nr_socket_getaddr(stun_server_socket, &stun_addr);
@@ -91,17 +108,17 @@ class MultiTcpSocketTest : public ::test
       stun_servers.push_back(*server);
 
       ASSERT_TRUE(NS_SUCCEEDED(ice_ctx_->SetStunServers(stun_servers)));
     }
 
     r = 1;
     for (int tries=10; tries && r; --tries) {
       r = nr_ip4_str_port_to_transport_addr(
-        (char *)"127.0.0.1", port_s++, IPPROTO_TCP, &local);
+        (char *)"127.0.0.1", EnsureEphemeral(port_s++), IPPROTO_TCP, &local);
       ASSERT_EQ(0, r);
 
       r = nr_socket_multi_tcp_create(ice_ctx_->ctx(),
           &local, tcp_type, 1, use_framing, 2048, sock);
     }
 
     ASSERT_EQ(0, r);
     printf("Created socket on %s\n", local.as_string);
@@ -122,17 +139,17 @@ class MultiTcpSocketTest : public ::test
     return sock;
   }
 
   void Listen_s(nr_socket *sock) {
     nr_transport_addr addr;
     int r=nr_socket_getaddr(sock, &addr);
     ASSERT_EQ(0, r);
     printf("Listen on %s\n", addr.as_string);
-    r = nr_socket_listen(sock, 1);
+    r = nr_socket_listen(sock, 5);
     ASSERT_EQ(0, r);
   }
 
   void Listen(nr_socket *sock) {
     test_utils->sts_target()->Dispatch(
             WrapRunnable(
                 this, &MultiTcpSocketTest::Listen_s, sock),
             NS_DISPATCH_SYNC);
@@ -195,16 +212,17 @@ class MultiTcpSocketTest : public ::test
     test_utils->sts_target()->Dispatch(
             WrapRunnable(
                 this, &MultiTcpSocketTest::SendData_s, from, to, data, len),
             NS_DISPATCH_SYNC);
   }
 
   void RecvData_s(nr_socket *expected_from, nr_socket *sent_to,
                   const char *expected_data, size_t expected_len) {
+    SetReadable(false);
     char received_data[expected_len+1];
     nr_transport_addr addr_from, addr_to;
     nr_transport_addr retaddr;
     size_t retlen;
 
     int r=nr_socket_getaddr(sent_to, &addr_to);
     ASSERT_EQ(0, r);
     r=nr_socket_getaddr(expected_from, &addr_from);
@@ -224,17 +242,16 @@ class MultiTcpSocketTest : public ::test
   void RecvData(nr_socket *expected_from, nr_socket *sent_to,
                 const char *expected_data, size_t expected_len) {
     ASSERT_TRUE_WAIT(IsReadable(), 1000);
     test_utils->sts_target()->Dispatch(
             WrapRunnable(
                 this, &MultiTcpSocketTest::RecvData_s, expected_from, sent_to,
                 expected_data, expected_len),
             NS_DISPATCH_SYNC);
-    SetReadable(false);
   }
 
   void TransferData(nr_socket *from, nr_socket *to, const char *data,
                     size_t len) {
     SendData(from, to, data, len);
     RecvData(from, to, data, len);
   }
 
@@ -386,16 +403,17 @@ TEST_F(MultiTcpSocketTest, TestBigData) 
   RecvData(socks[1], socks[0], buf2, sizeof(buf2));
   RecvData(socks[1], socks[0], buf1, sizeof(buf1));
 }
 
 
 int main(int argc, char **argv)
 {
   test_utils = new MtransportTestUtils();
+  NSS_NoDB_Init(nullptr); // For random number generation
 
   ::testing::TestEventListeners& listeners =
         ::testing::UnitTest::GetInstance()->listeners();
   // Adds a listener to the end.  Google Test takes the ownership.
   listeners.Append(new test::RingbufferDumper(test_utils));
 
   // Start the tests
   ::testing::InitGoogleTest(&argc, argv);