summary refs log tree commit homepage
path: root/t
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2011-02-07 19:48:46 -0800
committerEric Wong <normalperson@yhbt.net>2011-02-07 19:48:46 -0800
commit04b8de2cf6c3037c45e60d3720472b09dd23efd5 (patch)
treef355bb9890bf4548eee24d6f65b05b191ab9696d /t
parent662a146457689110c83bfd18987938475966f454 (diff)
This conflicts with ports clients may automatically use
in the ephemeral range.

This reverts commit c9a7560bb684bbdadb641ebc7597303f38c37d4f.
Diffstat (limited to 't')
-rwxr-xr-xt/bin/unused_listen17
1 files changed, 15 insertions, 2 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