Class: Kookaburra::RackAppServer

Inherits:
Object
  • Object
show all
Defined in:
lib/kookaburra/rack_app_server.rb

Overview

Handles Starting/Stopping Rack Server for Tests

RackAppServer is basically a wrapper around Capybara::Server that makes it a bit easier to use Kookaburra with a Rack application (such as Rails or Sinatra) when you want to run your tests locally against a server that is only running for the duration of the tests. You simply tell it how to get ahold of your Rack application (see #initialize) and then call #boot before your tests run and #shutdown after your tests run.

Examples:

using RSpec

# put this in something like `spec_helper.rb`
app_server = Kookaburra::RackAppServer.new do
  # unless you are in JRuby, this stuff all runs in a new fork later
  # on
  ENV['RAILS_ENV'] = 'test'
  require File.expand_path(File.join('..', '..', 'config', 'environment'), __FILE__)
  MyAppName::Application
end
RSpec.configure do
  c.before(:all) do
    app_server.boot
  end
  c.after(:all) do
    app_server.shutdown
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(startup_timeout = 10, &rack_app_initializer) ⇒ RackAppServer

Sets up a new app server

Parameters:

  • startup_timeout (Integer) (defaults to: 10)

    The maximum number of seconds to wait for the app server to respond

Yield Returns:

  • (#call)

    block must return a valid Rack application



40
41
42
43
44
# File 'lib/kookaburra/rack_app_server.rb', line 40

def initialize(startup_timeout=10, &rack_app_initializer)
  self.startup_timeout = startup_timeout
  self.rack_app_initializer = rack_app_initializer
  self.port = FindAPort.available_port
end

Instance Attribute Details

#portObject

Returns the value of attribute port.



33
34
35
# File 'lib/kookaburra/rack_app_server.rb', line 33

def port
  @port
end

Instance Method Details

#bootObject

Start the application server

This will launch the server on a (detected to be) available port. It will then monitor that port and only return once the app server is responding (or after the timeout period specified on #initialize).



51
52
53
54
# File 'lib/kookaburra/rack_app_server.rb', line 51

def boot
  fork_app_server
  wait_for_app_to_respond
end

#shutdownObject



56
57
58
59
# File 'lib/kookaburra/rack_app_server.rb', line 56

def shutdown
  Process.kill(9, rack_server_pid)
  Process.wait
end