From 707089c425f2001517d9b3472c78cfc3d9052288 Mon Sep 17 00:00:00 2001 From: Aravind Anbudurai Date: Fri, 19 Aug 2016 16:33:50 -0700 Subject: [PATCH] fix flaky EventHandlerTest Summary: Lets the kernel choose the port instead of hardcoding it. Reviewed By: djwatson Differential Revision: D3745911 fbshipit-source-id: d9680ec286e8015abb9274c30b572ff1d91548ce --- folly/io/async/test/EventHandlerTest.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/folly/io/async/test/EventHandlerTest.cpp b/folly/io/async/test/EventHandlerTest.cpp index daef1b72..9b89724e 100644 --- a/folly/io/async/test/EventHandlerTest.cpp +++ b/folly/io/async/test/EventHandlerTest.cpp @@ -190,10 +190,9 @@ TEST_F(EventHandlerTest, many_concurrent_consumers) { #ifdef EV_PRI TEST(EventHandlerSocketTest, EPOLLPRI) { - std::promise serverReady; - std::thread t([serverReadyFuture = serverReady.get_future()] { + std::promise serverReady; + std::thread t([serverReadyFuture = serverReady.get_future()]() mutable { // client - serverReadyFuture.wait(); LOG(INFO) << "Server is ready"; int sockfd = socket(AF_INET, SOCK_STREAM, 0); SCOPE_EXIT { @@ -208,7 +207,7 @@ TEST(EventHandlerSocketTest, EPOLLPRI) { memcpy(&server.sin_addr, he->h_addr_list[0], he->h_length); server.sin_family = AF_INET; - server.sin_port = htons(12345); + server.sin_port = serverReadyFuture.get(); PCHECK(::connect(sockfd, (struct sockaddr*)&server, sizeof(server)) == 0); LOG(INFO) << "Server connection available"; @@ -228,14 +227,18 @@ TEST(EventHandlerSocketTest, EPOLLPRI) { PCHECK(sockfd != -1) << "unable to open socket"; struct sockaddr_in sin; - sin.sin_port = htons(12345); + sin.sin_port = htons(0); sin.sin_addr.s_addr = INADDR_ANY; sin.sin_family = AF_INET; - PCHECK(::bind(sockfd, (struct sockaddr*)&sin, sizeof(sin)) >= 0) + PCHECK(bind(sockfd, (struct sockaddr*)&sin, sizeof(sin)) >= 0) << "Can't bind to port"; listen(sockfd, 5); - serverReady.set_value(); + + struct sockaddr_in findSockName; + socklen_t sz = sizeof(findSockName); + getsockname(sockfd, (struct sockaddr*)&findSockName, &sz); + serverReady.set_value(findSockName.sin_port); socklen_t clilen; struct sockaddr_in cli_addr; -- 2.34.1