From 31f77b2a01f69956b034d35a7f12038947b6bdbc Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sat, 5 Feb 2011 03:27:54 -0800 Subject: [PATCH] test: cleanup unused_port function Duh! --- test/test_io_splice.rb | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/test/test_io_splice.rb b/test/test_io_splice.rb index a00552e..28d3dae 100644 --- a/test/test_io_splice.rb +++ b/test/test_io_splice.rb @@ -20,26 +20,15 @@ def unused_port(addr = '127.0.0.1') base = 5000 port = sock = nil begin - begin - port = base + rand(32768 - base) - while port == 8080 - 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 + sock = TCPServer.new(addr, 0) + port = sock.addr[1] # 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 # when running tests in parallel with gmake. Create a lock file while # we have the port here to ensure that does not happen . lock_path = "#{Dir::tmpdir}/unicorn_test.#{addr}:#{port}.lock" - lock = File.open(lock_path, File::WRONLY|File::CREAT|File::EXCL, 0600) + File.open(lock_path, File::WRONLY|File::CREAT|File::EXCL, 0600).close at_exit { File.unlink(lock_path) rescue nil } rescue Errno::EEXIST sock.close rescue nil -- 2.11.4.GIT