Class: NoLockFirefox::PortPool
- Inherits:
-
Object
- Object
- NoLockFirefox::PortPool
- Defined in:
- lib/webdriver-firefox/port_pool.rb
Constant Summary collapse
- HOST =
"127.0.0.1"- START_PORT =
24576
Instance Method Summary collapse
- #find_free_port ⇒ Object
-
#initialize ⇒ PortPool
constructor
A new instance of PortPool.
- #probe_port(port) ⇒ Object
Constructor Details
#initialize ⇒ PortPool
Returns a new instance of PortPool.
11 12 13 14 15 16 17 |
# File 'lib/webdriver-firefox/port_pool.rb', line 11 def initialize begin @random = ::Random.new rescue @random = nil end end |
Instance Method Details
#find_free_port ⇒ Object
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 48 |
# File 'lib/webdriver-firefox/port_pool.rb', line 19 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
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/webdriver-firefox/port_pool.rb', line 50 def probe_port(port) begin s = TCPServer.new(HOST, port) s.close_on_exec = true # ChildProcess.close_on_exec s return s rescue SocketError, Errno::EADDRINUSE => e return nil end end |