summaryrefslogtreecommitdiffstats
path: root/net/socket
diff options
context:
space:
mode:
authormmenke@chromium.org <mmenke@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-08-29 18:52:39 +0000
committermmenke@chromium.org <mmenke@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-08-29 18:52:39 +0000
commitce690c2692fd79fbeb607314896852933b769e85 (patch)
treefb1deec57519f010a2d4e5add2b0ce9bdfe7c454 /net/socket
parentc303617213c42c1a3f26c7f777beb6f14daa1d3c (diff)
downloadchromium_src-ce690c2692fd79fbeb607314896852933b769e85.zip
chromium_src-ce690c2692fd79fbeb607314896852933b769e85.tar.gz
chromium_src-ce690c2692fd79fbeb607314896852933b769e85.tar.bz2
Fix flakiness in TCPListenSocketTests due to using a fixed port.
BUG=280733 Review URL: https://chromiumcodereview.appspot.com/23719007 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@220346 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/socket')
-rw-r--r--net/socket/tcp_listen_socket_unittest.cc36
-rw-r--r--net/socket/tcp_listen_socket_unittest.h13
2 files changed, 40 insertions, 9 deletions
diff --git a/net/socket/tcp_listen_socket_unittest.cc b/net/socket/tcp_listen_socket_unittest.cc
index b951ade..9589ce2 100644
--- a/net/socket/tcp_listen_socket_unittest.cc
+++ b/net/socket/tcp_listen_socket_unittest.cc
@@ -10,14 +10,14 @@
#include "base/bind.h"
#include "base/posix/eintr_wrapper.h"
#include "base/sys_byteorder.h"
+#include "net/base/ip_endpoint.h"
+#include "net/base/net_errors.h"
#include "net/base/net_util.h"
#include "net/socket/socket_descriptor.h"
#include "testing/platform_test.h"
namespace net {
-const int TCPListenSocketTester::kTestPort = 9999;
-
static const int kReadBufSize = 1024;
static const char kHelloWorld[] = "HELLO, WORLD";
static const int kMaxQueueSize = 20;
@@ -25,7 +25,11 @@ static const char kLoopback[] = "127.0.0.1";
static const int kDefaultTimeoutMs = 5000;
TCPListenSocketTester::TCPListenSocketTester()
- : loop_(NULL), server_(NULL), connection_(NULL), cv_(&lock_) {}
+ : loop_(NULL),
+ server_(NULL),
+ connection_(NULL),
+ cv_(&lock_),
+ server_port_(0) {}
void TCPListenSocketTester::SetUp() {
base::Thread::Options options;
@@ -42,13 +46,16 @@ void TCPListenSocketTester::SetUp() {
ASSERT_FALSE(server_.get() == NULL);
ASSERT_EQ(ACTION_LISTEN, last_action_.type());
+ int server_port = GetServerPort();
+ ASSERT_GT(server_port, 0);
+
// verify the connect/accept and setup test_socket_
test_socket_ = CreatePlatformSocket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
ASSERT_NE(kInvalidSocket, test_socket_);
struct sockaddr_in client;
client.sin_family = AF_INET;
client.sin_addr.s_addr = inet_addr(kLoopback);
- client.sin_port = base::HostToNet16(kTestPort);
+ client.sin_port = base::HostToNet16(server_port);
int ret = HANDLE_EINTR(
connect(test_socket_, reinterpret_cast<sockaddr*>(&client),
sizeof(client)));
@@ -125,6 +132,12 @@ void TCPListenSocketTester::Listen() {
server_ = DoListen();
ASSERT_TRUE(server_.get());
server_->AddRef();
+
+ // The server's port will be needed to open the client socket.
+ IPEndPoint local_address;
+ ASSERT_EQ(OK, server_->GetLocalAddress(&local_address));
+ SetServerPort(local_address.port());
+
ReportAction(TCPListenSocketTestAction(ACTION_LISTEN));
}
@@ -249,10 +262,21 @@ void TCPListenSocketTester::DidClose(StreamListenSocket* sock) {
TCPListenSocketTester::~TCPListenSocketTester() {}
scoped_refptr<TCPListenSocket> TCPListenSocketTester::DoListen() {
- return TCPListenSocket::CreateAndListen(kLoopback, kTestPort, this);
+ // Let the OS pick a free port.
+ return TCPListenSocket::CreateAndListen(kLoopback, 0, this);
+}
+
+int TCPListenSocketTester::GetServerPort() {
+ base::AutoLock locked(lock_);
+ return server_port_;
+}
+
+void TCPListenSocketTester::SetServerPort(int server_port) {
+ base::AutoLock locked(lock_);
+ server_port_ = server_port;
}
-class TCPListenSocketTest: public PlatformTest {
+class TCPListenSocketTest : public PlatformTest {
public:
TCPListenSocketTest() {
tester_ = NULL;
diff --git a/net/socket/tcp_listen_socket_unittest.h b/net/socket/tcp_listen_socket_unittest.h
index 048a018..93adbd5 100644
--- a/net/socket/tcp_listen_socket_unittest.h
+++ b/net/socket/tcp_listen_socket_unittest.h
@@ -103,18 +103,25 @@ class TCPListenSocketTester :
TCPListenSocketTestAction last_action_;
SocketDescriptor test_socket_;
- static const int kTestPort;
- base::Lock lock_; // protects |queue_| and wraps |cv_|
+ base::Lock lock_; // Protects |queue_| and |server_port_|. Wraps |cv_|.
base::ConditionVariable cv_;
std::deque<TCPListenSocketTestAction> queue_;
- protected:
+ private:
friend class base::RefCountedThreadSafe<TCPListenSocketTester>;
virtual ~TCPListenSocketTester();
virtual scoped_refptr<TCPListenSocket> DoListen();
+
+ // Getters/setters for |server_port_|. They use |lock_| for thread safety.
+ int GetServerPort();
+ void SetServerPort(int server_port);
+
+ // Port the server is using. Must have |lock_| to access. Set by Listen() on
+ // the server's thread.
+ int server_port_;
};
} // namespace net