Sha256: 8225988af0ddb0370b73212abf65efa65703404a16cd28315df5d45dbc9c5bef
Contents?: true
Size: 1.73 KB
Versions: 2
Compression:
Stored size: 1.73 KB
Contents
# rubocop:disable Lint/HandleExceptions require "socket" require "timeout" module RSpec::Httpd module Server MAX_STARTUP_TIME = 10 extend self # builds and returns a server object. # # You can use this method to retrieve a client connection to a server # specified via host:, port:, and, optionally, a command. def start!(host:, port:, command:) @servers ||= {} @servers[[host, port, command]] ||= do_start(host, port, command) end private def do_start(host, port, command) logger = RSpec::Httpd.logger logger.debug "Starting server: #{command}" pid = spawn(command) at_exit do begin logger.debug "Stopping server at pid #{pid}: #{command}" Process.kill("KILL", pid) sleep 0.2 rescue Errno::ESRCH end die "Cannot stop server at pid #{pid}: #{command}" if port_open?(host, port) end unless wait_for_server(host: host, port: port, pid: pid, timeout: MAX_STARTUP_TIME) logger.error "server didn't start at http://#{host}:#{port} pid #{pid}: #{command}" exit 1 end logger.info "Started server at pid #{pid}: #{command}" pid end def wait_for_server(host:, port:, pid:, timeout:) while timeout > 0 sleep 0.1 return true if port_open?(host, port) return false if Process.waitpid(pid, Process::WNOHANG) timeout -= 0.1 next if timeout > 0 return false end end def port_open?(host, port) Timeout.timeout(0.01) do s = TCPSocket.new(host, port) s.close return true end rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Timeout::Error false end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
rspec-httpd-0.0.7 | lib/rspec/httpd/server.rb |
rspec-httpd-0.0.5 | lib/rspec/httpd/server.rb |