Class: CukeForker::WebDriver::PortPool

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

Defined Under Namespace

Classes: BindError, TimeoutError, TooManyPortsError

Constant Summary collapse

START_PORT =
1024
HOST =
"127.0.0.1"
RELEASE_TIMEOUT =
20
PORT_INCREMENT =
2

Instance Method Summary collapse

Constructor Details

#initialize(capacity) ⇒ PortPool

Returns a new instance of PortPool.



17
18
19
20
21
22
23
24
# File 'lib/cukeforker-webdriver/port_pool.rb', line 17

def initialize(capacity)
  @last_port     = START_PORT - 1
  @capacity      = capacity
  @servers       = []
  @running       = []

  bind
end

Instance Method Details

#close_on_execObject



57
58
59
60
# File 'lib/cukeforker-webdriver/port_pool.rb', line 57

def close_on_exec
  STDERR.puts "#{self.class}: #{Process.pid} pool closed on exec"
  @servers.each { |s| ChildProcess.close_on_exec s }
end

#getObject



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cukeforker-webdriver/port_pool.rb', line 26

def get
  if @servers.empty?
    STDERR.puts "out of ports, finding next"
    @servers << next_server
  end

  s = @servers.shift
  port = s.addr.fetch(1)
  s.close

  wait_while_listening port
  port
end

#release(port) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/cukeforker-webdriver/port_pool.rb', line 40

def release(port)
  raise TooManyPortsError if @servers.size == @capacity
  server = wait_for_server(port)
  @servers << server
rescue WebDriverPortPool::TimeoutError
  STDERR.puts "timed out while releasing #{port}"
end

#sizeObject



62
63
64
# File 'lib/cukeforker-webdriver/port_pool.rb', line 62

def size
  @servers.size
end

#stopObject



48
49
50
51
52
53
54
55
# File 'lib/cukeforker-webdriver/port_pool.rb', line 48

def stop
  @servers.each { |s|
    begin
      s.close
    rescue IOError
    end
  }
end