about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2011-02-10 13:41:32 -0800
committerEric Wong <normalperson@yhbt.net>2011-02-10 13:41:32 -0800
commit4cfb64f10784498b9625bbbd3364231710bc7c36 (patch)
tree6e18cb4f6c3f9cd7ae148fef78240a5075ed757c
parent6dd90cb902f43b32b0db204484d5e3df79ec0d0c (diff)
downloadunicorn-4cfb64f10784498b9625bbbd3364231710bc7c36.tar.gz
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
-rwxr-xr-xt/bin/unused_listen17
-rw-r--r--test/test_helper.rb17
2 files changed, 30 insertions, 4 deletions
diff --git a/t/bin/unused_listen b/t/bin/unused_listen
index cd536f1..b638f54 100755
--- a/t/bin/unused_listen
+++ b/t/bin/unused_listen
@@ -7,11 +7,24 @@ require 'tmpdir'
 
 default_port = 8080
 addr = ENV['UNICORN_TEST_ADDR'] || '127.0.0.1'
+retries = 100
+base = 5000
 port = sock = lock_path = nil
 
 begin
-  sock = TCPServer.new(addr, 0)
-  port = sock.addr[1]
+  begin
+    port = base + rand(32768 - base)
+    while port == 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
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