class CapybaraSpa::Server::ExternalServer

CapybaraSpa::Server::ExternalServer is a class that wraps a server running as as an external process. For example, let's say you wanted to always have an a version of your single-page-application up and running for integration tests. You would start this in a different terminal or shell like this:

ng serve --port 5001

Then you would configure your CapybaraSpa server in your test helper (e.g. \ spec_helper.rb, rails_helper.rb, etc):

server = CapybaraSpa::Server::ExternalServer.new(
  port: 5001
)

Attributes

host[RW]

host is a string of the host or ip address to connect to

port[RW]

port is port number that the external process is running on

start_timeout[RW]

start_timeout is the number of seconds to wait for the external process to begin listening on port. Applies to start and started?

stop_timeout[RW]

stop_timeout is the number of seconds to wait when determining the external process is no longer running. Applies to stop and stopped?

Public Class Methods

new(host: 'localhost', port: 5001, start_timeout: 60, stop_timeout: 1) click to toggle source
# File lib/capybara_spa/server/external_server.rb, line 38
def initialize(host: 'localhost', port: 5001, start_timeout: 60, stop_timeout: 1)
  @host = host
  @port = port
  @start_timeout = start_timeout
  @stop_timeout = stop_timeout
end

Public Instance Methods

start() click to toggle source

start is a no-op, but it will wait up to the start_timeout for the external process to start listening on the specified port before giving up and raising an ExternalServerNotFoundOnPort error.

# File lib/capybara_spa/server/external_server.rb, line 48
      def start
        unless is_port_open?(timeout: start_timeout)
          raise ExternalServerNotFoundOnPort, <<-ERROR.gsub(/^\s*\|/, '')
            |Tried for #{start_timeout} seconds but nothing was listening
            |on port #{port}. Please make sure the external process is running
            |successfully and that the port is correct.
          ERROR
        end
      end
started?() click to toggle source

Returns true if the an external process is running on the host and port. Otherwise, returns false.

# File lib/capybara_spa/server/external_server.rb, line 60
def started?
  is_port_open?(timeout: start_timeout)
end
stop() click to toggle source

stop is a no-op, but it will wait up to the stop_timeout for the external process to stop listening on the specified port before giving up and raising an ExternalServerStillRunning error.

# File lib/capybara_spa/server/external_server.rb, line 67
      def stop
        unless !is_port_open?(timeout: stop_timeout)
          raise ExternalServerStillRunning, <<-ERROR.gsub(/^\s*\|/, '')
            |I tried for #{stop_timeout} seconds to verify that the
            |external process listening on #{port} had stopped listening,
            |but it hasn't. You may have a zombie process
            |or may need to increase the stop_timeout.
          ERROR
        end
      end
stopped?() click to toggle source

Returns true if the an external process is not running on the host and port. Otherwise, returns true.

# File lib/capybara_spa/server/external_server.rb, line 80
def stopped?
  !is_port_open?(timeout: stop_timeout)
end

Private Instance Methods

is_port_open?(timeout: 10) click to toggle source
# File lib/capybara_spa/server/external_server.rb, line 86
def is_port_open?(timeout: 10)
  Timeout.timeout(timeout) do
    begin
      s = TCPSocket.new(host, port)
      s.close
      return true
    rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
      sleep 1
      retry
    end
  end
rescue Timeout::Error
  return false
end