2 # -*- encoding: binary -*-
3 # this is to remain compatible with the unused_port function in the
4 # Unicorn test/test_helper.rb file
9 addr
= ENV['UNICORN_TEST_ADDR'] || '127.0.0.1'
12 port
= sock
= lock_path
= nil
16 port
= base
+ rand(32768 - base
)
17 while port
== default_port
18 port
= base
+ rand(32768 - base
)
21 sock
= Socket
.new(Socket
::AF_INET, Socket
::SOCK_STREAM, 0)
22 sock
.bind(Socket
.pack_sockaddr_in(port
, addr
))
24 rescue Errno
::EADDRINUSE, Errno
::EACCES
26 retry if (retries
-= 1) >= 0
29 # since we'll end up closing the random port we just got, there's a race
30 # condition could allow the random port we just chose to reselect itself
31 # when running tests in parallel with gmake. Create a lock file while
32 # we have the port here to ensure that does not happen.
33 lock_path
= "#{Dir::tmpdir}/unicorn_test.#{addr}:#{port}.lock"
34 File
.open(lock_path
, File
::WRONLY|File
::CREAT|File
::EXCL, 0600)
40 puts
%Q(listen=#{addr}:#{port} T_RM_LIST="$T_RM_LIST #{lock_path}")