diff options
Diffstat (limited to 'test/test_helper.rb')
-rw-r--r-- | test/test_helper.rb | 17 |
1 files changed, 15 insertions, 2 deletions
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 |