From 4cfb64f10784498b9625bbbd3364231710bc7c36 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 10 Feb 2011 13:41:32 -0800 Subject: Revert "test_helper: simplify random port binding" This causes conflicts with ports clients may use in the ephemeral range since those do not hold FS locks. This reverts commit e597e594ad88dc02d70f7d3521d0d3bdc23739bb. Conflicts: test/test_helper.rb --- test/test_helper.rb | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/test_helper.rb b/test/test_helper.rb index d9ff90c..92195e6 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -100,10 +100,23 @@ end # for a race condition is very small). You may also set UNICORN_TEST_ADDR # to override the default test address (127.0.0.1). def unused_port(addr = '127.0.0.1') + retries = 100 + base = 5000 port = sock = nil begin - sock = TCPServer.new(addr, 0) - port = sock.addr[1] + begin + port = base + rand(32768 - base) + while port == Unicorn::Const::DEFAULT_PORT + port = base + rand(32768 - base) + end + + sock = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0) + sock.bind(Socket.pack_sockaddr_in(port, addr)) + sock.listen(5) + rescue Errno::EADDRINUSE, Errno::EACCES + sock.close rescue nil + retry if (retries -= 1) >= 0 + end # since we'll end up closing the random port we just got, there's a race # condition could allow the random port we just chose to reselect itself -- cgit v1.2.3-24-ge0c7