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



23
24
25
26
# File 'lib/selenium/webdriver/common/port_prober.rb', line 23

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

.free?(port) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/selenium/webdriver/common/port_prober.rb', line 45

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

  true
rescue SocketError, Errno::EADDRINUSE
  false
end

.randomObject



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

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