Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Unicorn rails #57

Open
wants to merge 5 commits into from

1 participant

@jwinky

Implements an application server adapter to allow selenium to run unicorn as the application server. Includes spec.

To use:

Webrat.configure do |config|
config.application_framework = :rails_unicorn
config.unicorn_conf_file = "/path/to/unicorn.rb" # optional
end

This change was motivated by:
(1) need to test ajax functionality with a server capable of processing more than 1 request simultaneously
(2) increasing issues using mongrel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
3  lib/webrat/core/configuration.rb
@@ -53,6 +53,9 @@ def application_port_for_selenium
# Which server the application is running on for selenium testing? Defaults to localhost
attr_accessor :application_address
+ # Path to unicorn configuration file if :application_framework == :unicorn
+ attr_accessor :unicorn_conf_file
+
# Which server Selenium server is running on. Defaults to nil(server starts in webrat process and runs locally)
attr_accessor :selenium_server_address
View
3  lib/webrat/selenium/application_server_factory.rb
@@ -14,6 +14,9 @@ def self.app_server_instance
when :rails
require "webrat/selenium/application_servers/rails"
return Webrat::Selenium::ApplicationServers::Rails.new
+ when :rails_unicorn
+ require "webrat/selenium/application_servers/rails_unicorn"
+ return Webrat::Selenium::ApplicationServers::RailsUnicorn.new
when :rack
require "webrat/selenium/application_servers/rack"
return Webrat::Selenium::ApplicationServers::Rack.new
View
1  lib/webrat/selenium/application_servers.rb
@@ -2,4 +2,5 @@
require "webrat/selenium/application_servers/sinatra"
require "webrat/selenium/application_servers/merb"
require "webrat/selenium/application_servers/rails"
+require "webrat/selenium/application_servers/rails_unicorn"
require "webrat/selenium/application_servers/external"
View
64 lib/webrat/selenium/application_servers/rails_unicorn.rb
@@ -0,0 +1,64 @@
+require "webrat/selenium/application_servers/base"
+
+module Webrat
+ module Selenium
+ module ApplicationServers
+ class RailsUnicorn < Webrat::Selenium::ApplicationServers::Base
+
+ def start
+ system start_command
+ end
+
+ def stop
+ silence_stream(STDOUT) do
+ system stop_command
+ end if File.exist?(pid_file)
+ end
+
+ def fail
+ $stderr.puts
+ $stderr.puts
+ $stderr.puts "==> Failed to boot the Rails Unicorn application server... exiting!"
+ $stderr.puts
+ $stderr.puts "Verify you can start a Rails Unicorn server on port #{Webrat.configuration.application_port} with the following command:"
+ $stderr.puts
+ $stderr.puts " #{start_command}"
+ exit
+ end
+
+ def pid_file
+ prepare_pid_file("#{::Rails.root}/tmp/pids", "unicorn.pid")
+ end
+
+ def start_command
+ "cd '#{::Rails.root}' && #{bundler}#{unicorn} -D -p #{Webrat.configuration.application_port} -E #{Webrat.configuration.application_environment}#{unicorn_config}"
+ end
+
+ def stop_command
+ "kill `cat '#{pid_file}'`"
+ end
+
+ private
+
+ def bundler
+ File.exist?("./Gemfile") ? "bundle exec " : ""
+ end
+
+ def unicorn
+ if ::Rails.version && ::Rails.version[0,2] == "2."
+ "unicorn_rails"
+ else
+ "unicorn"
+ end
+ end
+
+ def unicorn_config
+ conf_file = Webrat.configuration.unicorn_conf_file
+ conf_file.blank? ? "" : " -c #{conf_file}"
+ end
+
+ end
+ end
+ end
+end
+
View
4 spec/private/core/configuration_spec.rb
@@ -82,6 +82,10 @@
@config.selenium_browser_startup_timeout = 10
@config.selenium_browser_startup_timeout.should == 10
end
+
+ it 'should default unicorn configuration file path to nil' do
+ @config.unicorn_conf_file.should be_nil
+ end
end
end
View
7 spec/private/selenium/application_servers/rails_spec.rb
@@ -11,6 +11,13 @@
# require "rubygems"; require "ruby-debug"; Debugger.start; debugger
@server.stub!(:system)
@server.stub!(:at_exit)
+
+
+ class Rails
+ def self.root
+ RAILS_ROOT
+ end
+ end
end
describe "boot" do
View
8 spec/public/selenium/application_server_factory_spec.rb
@@ -31,6 +31,14 @@
Webrat::Selenium::ApplicationServerFactory.app_server_instance
end
+ it "should require and create a rails unicorn server in rails_unicorn mode" do
+ server = mock(Webrat::Selenium::ApplicationServers::RailsUnicorn)
+ Webrat.configuration.application_framework = :rails_unicorn
+ Webrat::Selenium::ApplicationServerFactory.should_receive(:require).with("webrat/selenium/application_servers/rails_unicorn")
+ Webrat::Selenium::ApplicationServers::RailsUnicorn.should_receive(:new).and_return(server)
+ Webrat::Selenium::ApplicationServerFactory.app_server_instance
+ end
+
it "should require and create a rails server in external mode" do
server = mock(Webrat::Selenium::ApplicationServers::External)
Webrat.configuration.application_framework = :external
View
72 spec/public/selenium/application_servers/rails_unicorn_spec.rb
@@ -0,0 +1,72 @@
+require File.expand_path(File.dirname(__FILE__) + "/../../../spec_helper")
+require "webrat/selenium/application_servers/rails_unicorn"
+
+RAILS_ROOT = "." unless defined?(RAILS_ROOT)
+
+
+describe Webrat::Selenium::ApplicationServers::RailsUnicorn do
+ include Webrat::Selenium::SilenceStream
+
+ before do
+ @server = Webrat::Selenium::ApplicationServers::RailsUnicorn.new
+ # require "rubygems"; require "ruby-debug"; Debugger.start; debugger
+ @server.stub!(:system)
+ @server.stub!(:at_exit)
+
+ # Fake the top-level Rails object
+ class Rails
+ cattr_accessor :root
+ cattr_accessor :version
+ end
+
+ Rails.root = RAILS_ROOT
+ Rails.version = "3."
+ end
+
+ describe "boot" do
+ it "should wait for the server to start on 0.0.0.0" do
+ TCPSocket.should_receive(:wait_for_service_with_timeout).
+ with(hash_including(:host => "0.0.0.0"))
+
+ silence_stream(STDERR) do
+ @server.boot
+ end
+ end
+ end
+
+ describe "start command" do
+ context "when run under Rails v2" do
+ before do
+ ::Rails.version = "2.x.x"
+ end
+
+ it "should invoke unicorn_rails" do
+ @server.start_command.should == "cd '#{RAILS_ROOT}' && bundle exec unicorn_rails -D -p 3001 -E test"
+ end
+ end
+
+ context "when run under Rails v3" do
+ before do
+ ::Rails.version = "3.x.x"
+ end
+
+ context "with the default configuration" do
+ it "should invoke unicorn" do
+ @server.start_command.should == "cd '#{RAILS_ROOT}' && bundle exec unicorn -D -p 3001 -E test"
+ end
+ end
+
+ context "when configuration specifies a :unicorn_conf_file" do
+ before do
+ Webrat.configure do |conf|
+ conf.unicorn_conf_file = "/foo/bar.rb"
+ end
+ end
+
+ it "should invoke unicorn" do
+ @server.start_command.should == "cd '#{RAILS_ROOT}' && bundle exec unicorn -D -p 3001 -E test -c /foo/bar.rb"
+ end
+ end
+ end
+ end
+end
View
2  webrat.gemspec
@@ -205,6 +205,7 @@ Most Ruby web frameworks and testing frameworks are supported.}
"spec/private/rails/attaches_file_spec.rb",
"spec/private/rails/rails_adapter_spec.rb",
"spec/private/selenium/application_servers/rails_spec.rb",
+ "spec/private/selenium/application_servers/rails_unicorn_spec.rb",
"spec/public/basic_auth_spec.rb",
"spec/public/check_spec.rb",
"spec/public/choose_spec.rb",
@@ -304,6 +305,7 @@ Most Ruby web frameworks and testing frameworks are supported.}
"spec/private/rails/attaches_file_spec.rb",
"spec/private/rails/rails_adapter_spec.rb",
"spec/private/selenium/application_servers/rails_spec.rb",
+ "spec/private/selenium/application_servers/rails_unicorn_spec.rb",
"spec/public/basic_auth_spec.rb",
"spec/public/check_spec.rb",
"spec/public/choose_spec.rb",
Something went wrong with that request. Please try again.