Class: NoLockFirefox::PortPool

Inherits:
Object
  • Object
show all
Defined in:
lib/webdriver-firefox/port_pool.rb

Constant Summary collapse

HOST =
"127.0.0.1"
START_PORT =
24576

Instance Method Summary collapse

Constructor Details

#initializePortPool

Returns a new instance of PortPool.



10
11
12
13
14
15
16
# File 'lib/webdriver-firefox/port_pool.rb', line 10

def initialize
  begin
    @random = ::Random.new
  rescue
    @random = nil
  end
end

Instance Method Details

#find_free_portObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/webdriver-firefox/port_pool.rb', line 18

def find_free_port
  probed = nil

  tid = ENV.fetch('TDDIUM_TID', 0).to_i

  range = 256*tid
  if @random then
    index = @random.rand(256)
  else
    index = rand(256)
  end
  limit = START_PORT+256*(tid+1)

  timeout = Time.now+90

  while Time.now < timeout do
    port = START_PORT+range+index
    while port < limit do
      probed = probe_port(port)
      if probed then
        probed.close
        return port
      end
      port += 1
	  Kernel.sleep(0.1)
    end
  end

  raise "unable to find open port in reasonable time"
end

#probe_port(port) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/webdriver-firefox/port_pool.rb', line 49

def probe_port(port)
  begin
    s = TCPServer.new(HOST, port)
    if s.respond_to?(:close_on_exec=) then
      s.close_on_exec = true
    end
#        ChildProcess.close_on_exec s
    return s
  rescue SocketError, Errno::EADDRINUSE => e
    return nil
  end
end