Class: Selenium::WebDriver::PortProber

Inherits:
Object
  • Object
show all
Defined in:
lib/selenium/webdriver/common/port_prober.rb

Constant Summary collapse

IGNORED_ERRORS =
[Errno::EADDRNOTAVAIL]

Class Method Summary collapse

Class Method Details

.above(port) ⇒ Object


4
5
6
7
# File 'lib/selenium/webdriver/common/port_prober.rb', line 4

def self.above(port)
  port += 1 until free? port
  port
end

.free?(port) ⇒ Boolean

Returns:

  • (Boolean)

25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/selenium/webdriver/common/port_prober.rb', line 25

def self.free?(port)
  Platform.interfaces.each do |host|
    begin
      TCPServer.new(host, port).close
    rescue *IGNORED_ERRORS => ex
      $stderr.puts "port prober could not bind to #{host}:#{port} (#{ex.message})" if $DEBUG
      # ignored - some machines appear unable to bind to some of their interfaces
    end
  end

  true
rescue SocketError, Errno::EADDRINUSE
  false
end

.randomObject


9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/selenium/webdriver/common/port_prober.rb', line 9

def self.random
  # TODO: Avoid this
  #
  # (a) should pick a port that's guaranteed to be free on all interfaces
  # (b) should pick a random port outside the ephemeral port range
  #
  server = TCPServer.new(Platform.localhost, 0)
  port   = server.addr[1]
  server.close

  port
end