Randomize the Unix socket name in AsyncSocketTest.SendMessageAncillaryData test to...
authorMaxim Georgiev <maxgeorg@fb.com>
Thu, 23 Mar 2017 17:31:34 +0000 (10:31 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Thu, 23 Mar 2017 17:36:38 +0000 (10:36 -0700)
Summary:
Our test framework reports frequent failures of AsyncSocketTest.SendMessageAncillaryData. According to the logs the socket fails to bind:

    folly/io/async/test/AsyncSocketTest2.cpp:3098: Failure
    Expected: (bind(lfd, (struct sockaddr*)&addr, sizeof(addr))) != (-1), actual: -1 vs -1
    Bind failed: 98

This diff adds the socket name randomization to avoid name collisions between tests running concurrently on the same test box.

Reviewed By: yfeldblum

Differential Revision: D4758942

fbshipit-source-id: 6066dbc18222a4521c40b2ff218cb7dab8bd789d

folly/io/async/test/AsyncSocketTest2.cpp

index cbca3d348844522270f184e6dc1144b2cd78818d..e90bca5c728c5788c1038b547118b7abbcf35459 100644 (file)
@@ -3085,38 +3085,21 @@ TEST(AsyncSocketTest, SendMessageFlags) {
 }
 
 TEST(AsyncSocketTest, SendMessageAncillaryData) {
-  struct sockaddr_un addr = {AF_UNIX,
-                             "AsyncSocketTest.SendMessageAncillaryData\0"};
+  int fds[2];
+  EXPECT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, fds), 0);
 
-  // Clean up the name in the name space we're going to use
-  ASSERT_FALSE(remove(addr.sun_path) == -1 && errno != ENOENT);
+  // "Client" socket
+  int cfd = fds[0];
+  ASSERT_NE(cfd, -1);
 
-  // Set up listening socket
-  int lfd = fsp::socket(AF_UNIX, SOCK_STREAM, 0);
-  ASSERT_NE(lfd, -1);
-  SCOPE_EXIT { close(lfd); };
-  ASSERT_NE(bind(lfd, (struct sockaddr*)&addr, sizeof(addr)), -1)
-      << "Bind failed: " << errno;
-
-  // Create the connecting socket
-  int csd = fsp::socket(AF_UNIX, SOCK_STREAM, 0);
-  ASSERT_NE(csd, -1);
-
-  // Listen for incoming connect
-  ASSERT_NE(listen(lfd, 5), -1);
-
-  // Connect to the listening socket
-  ASSERT_NE(fsp::connect(csd, (struct sockaddr*)&addr, sizeof(addr)), -1)
-      << "Connect request failed: " << errno;
-
-  // Accept the connection
-  int sfd = accept(lfd, nullptr, nullptr);
+  // "Server" socket
+  int sfd = fds[1];
   ASSERT_NE(sfd, -1);
   SCOPE_EXIT { close(sfd); };
 
   // Instantiate AsyncSocket object for the connected socket
   EventBase evb;
-  std::shared_ptr<AsyncSocket> socket = AsyncSocket::newSocket(&evb, csd);
+  std::shared_ptr<AsyncSocket> socket = AsyncSocket::newSocket(&evb, cfd);
 
   // Open a temporary file and write a magic string to it
   // We'll transfer the file handle to test the message parameters