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