module Hula::Helpers::SocketTools

Public Instance Methods

free_port() click to toggle source
# File lib/hula/helpers/socket_tools.rb, line 33
                def free_port
  socket = Socket.new(:INET, :STREAM, 0)
  socket.bind(Addrinfo.tcp('127.0.0.1', 0))
  socket.local_address.ip_port
ensure
  socket.close
end
port_open?(host:, port:) click to toggle source
# File lib/hula/helpers/socket_tools.rb, line 25
                def port_open?(host:, port:)
  socket = TCPSocket.new(host, port)
  socket.close unless socket.nil?
  true
rescue Errno::ECONNREFUSED
  false
end
wait_for_port(host:, port:, timeout_seconds: 20) click to toggle source
# File lib/hula/helpers/socket_tools.rb, line 18
                def wait_for_port(host:, port:, timeout_seconds: 20)
  error = "Failed to connect to #{host}:#{port} within #{timeout_seconds} seconds"
  TimeoutTools.wait_for(error: error, timeout_seconds: timeout_seconds) do
    port_open?(host: host, port: port)
  end
end