2008-01-05 08:33:00 -05:00
|
|
|
require 'abstract_unit'
|
2006-09-02 20:18:31 -04:00
|
|
|
|
2009-02-16 16:12:28 -05:00
|
|
|
uses_gem "fcgi", "0.8.7" do
|
|
|
|
|
2008-12-28 21:31:33 -05:00
|
|
|
require 'action_controller'
|
2006-09-02 20:18:31 -04:00
|
|
|
require 'fcgi_handler'
|
2005-06-22 07:18:01 -04:00
|
|
|
|
2009-02-07 01:35:15 -05:00
|
|
|
Dispatcher.middleware.clear
|
|
|
|
|
2005-06-22 07:18:01 -04:00
|
|
|
class RailsFCGIHandlerTest < Test::Unit::TestCase
|
|
|
|
def setup
|
|
|
|
@log = StringIO.new
|
|
|
|
@handler = RailsFCGIHandler.new(@log)
|
|
|
|
end
|
|
|
|
|
2006-09-02 20:18:31 -04:00
|
|
|
def test_process_restart
|
2009-02-07 01:35:15 -05:00
|
|
|
request = mock
|
|
|
|
FCGI.stubs(:each).yields(request)
|
2007-09-23 07:20:25 -04:00
|
|
|
|
|
|
|
@handler.expects(:process_request).once
|
|
|
|
@handler.expects(:dispatcher_error).never
|
|
|
|
|
|
|
|
@handler.expects(:when_ready).returns(:restart)
|
2009-02-07 01:35:15 -05:00
|
|
|
@handler.expects(:close_connection).with(request)
|
2007-09-23 07:20:25 -04:00
|
|
|
@handler.expects(:reload!).never
|
2006-09-02 20:18:31 -04:00
|
|
|
@handler.expects(:restart!)
|
2007-09-23 07:20:25 -04:00
|
|
|
|
2006-09-02 20:18:31 -04:00
|
|
|
@handler.process!
|
|
|
|
end
|
2007-09-23 07:20:25 -04:00
|
|
|
|
2006-09-02 20:18:31 -04:00
|
|
|
def test_process_exit
|
2009-02-07 01:35:15 -05:00
|
|
|
request = mock
|
|
|
|
FCGI.stubs(:each).yields(request)
|
2007-09-23 07:20:25 -04:00
|
|
|
|
|
|
|
@handler.expects(:process_request).once
|
|
|
|
@handler.expects(:dispatcher_error).never
|
|
|
|
|
|
|
|
@handler.expects(:when_ready).returns(:exit)
|
2009-02-07 01:35:15 -05:00
|
|
|
@handler.expects(:close_connection).with(request)
|
2007-09-23 07:20:25 -04:00
|
|
|
@handler.expects(:reload!).never
|
|
|
|
@handler.expects(:restart!).never
|
|
|
|
|
2006-09-02 20:18:31 -04:00
|
|
|
@handler.process!
|
|
|
|
end
|
2007-09-23 07:20:25 -04:00
|
|
|
|
2006-09-02 20:18:31 -04:00
|
|
|
def test_process_with_system_exit_exception
|
2009-02-07 01:35:15 -05:00
|
|
|
request = mock
|
|
|
|
FCGI.stubs(:each).yields(request)
|
2007-09-23 07:20:25 -04:00
|
|
|
|
|
|
|
@handler.expects(:process_request).once.raises(SystemExit)
|
|
|
|
@handler.stubs(:dispatcher_log)
|
|
|
|
@handler.expects(:dispatcher_log).with(:info, regexp_matches(/^stopping/))
|
|
|
|
@handler.expects(:dispatcher_error).never
|
|
|
|
|
|
|
|
@handler.expects(:when_ready).never
|
|
|
|
@handler.expects(:close_connection).never
|
|
|
|
@handler.expects(:reload!).never
|
|
|
|
@handler.expects(:restart!).never
|
|
|
|
|
2006-09-02 20:18:31 -04:00
|
|
|
@handler.process!
|
|
|
|
end
|
2007-09-23 07:20:25 -04:00
|
|
|
|
2008-04-03 16:00:33 -04:00
|
|
|
def test_restart_handler_outside_request
|
2006-09-02 20:18:31 -04:00
|
|
|
@handler.expects(:dispatcher_log).with(:info, "asked to restart ASAP")
|
2008-04-03 16:00:33 -04:00
|
|
|
@handler.expects(:restart!).once
|
2007-09-23 07:20:25 -04:00
|
|
|
|
2006-09-02 20:18:31 -04:00
|
|
|
@handler.send(:restart_handler, nil)
|
2008-04-03 16:00:33 -04:00
|
|
|
assert_equal nil, @handler.when_ready
|
2006-09-02 20:18:31 -04:00
|
|
|
end
|
2007-09-23 07:20:25 -04:00
|
|
|
|
2006-09-02 20:18:31 -04:00
|
|
|
def test_install_signal_handler_should_log_on_bad_signal
|
|
|
|
@handler.stubs(:trap).raises(ArgumentError)
|
|
|
|
|
|
|
|
@handler.expects(:dispatcher_log).with(:warn, "Ignoring unsupported signal CHEESECAKE.")
|
|
|
|
@handler.send(:install_signal_handler, "CHEESECAKE", nil)
|
|
|
|
end
|
2007-09-23 07:20:25 -04:00
|
|
|
|
2006-09-02 20:18:31 -04:00
|
|
|
def test_reload
|
|
|
|
@handler.expects(:restore!)
|
|
|
|
@handler.expects(:dispatcher_log).with(:info, "reloaded")
|
|
|
|
|
|
|
|
@handler.send(:reload!)
|
|
|
|
assert_nil @handler.when_ready
|
|
|
|
end
|
2007-09-23 07:20:25 -04:00
|
|
|
|
|
|
|
|
2006-09-02 20:18:31 -04:00
|
|
|
def test_reload_runs_gc_when_gc_request_period_set
|
|
|
|
@handler.expects(:run_gc!)
|
|
|
|
@handler.expects(:restore!)
|
|
|
|
@handler.expects(:dispatcher_log).with(:info, "reloaded")
|
|
|
|
@handler.gc_request_period = 10
|
|
|
|
@handler.send(:reload!)
|
|
|
|
end
|
2007-09-23 07:20:25 -04:00
|
|
|
|
2006-09-02 20:18:31 -04:00
|
|
|
def test_reload_doesnt_run_gc_if_gc_request_period_isnt_set
|
|
|
|
@handler.expects(:run_gc!).never
|
|
|
|
@handler.expects(:restore!)
|
|
|
|
@handler.expects(:dispatcher_log).with(:info, "reloaded")
|
|
|
|
@handler.send(:reload!)
|
|
|
|
end
|
2007-09-23 07:20:25 -04:00
|
|
|
|
2006-09-02 20:18:31 -04:00
|
|
|
def test_restart!
|
|
|
|
@handler.expects(:dispatcher_log).with(:info, "restarted")
|
2007-09-23 07:20:25 -04:00
|
|
|
@handler.expects(:exec).returns('restarted')
|
|
|
|
assert_equal 'restarted', @handler.send(:restart!)
|
2006-09-02 20:18:31 -04:00
|
|
|
end
|
2007-09-23 07:20:25 -04:00
|
|
|
|
2006-09-02 20:18:31 -04:00
|
|
|
def test_restore!
|
|
|
|
$".expects(:replace)
|
|
|
|
Dispatcher.expects(:reset_application!)
|
|
|
|
ActionController::Routing::Routes.expects(:reload)
|
|
|
|
@handler.send(:restore!)
|
|
|
|
end
|
|
|
|
|
2005-06-22 07:18:01 -04:00
|
|
|
def test_uninterrupted_processing
|
2009-02-07 01:35:15 -05:00
|
|
|
request = mock
|
|
|
|
FCGI.expects(:each).yields(request)
|
|
|
|
@handler.expects(:process_request).with(request)
|
2007-09-23 07:20:25 -04:00
|
|
|
|
2005-06-22 07:18:01 -04:00
|
|
|
@handler.process!
|
2007-09-23 07:20:25 -04:00
|
|
|
|
2005-06-29 07:07:20 -04:00
|
|
|
assert_nil @handler.when_ready
|
2005-06-22 07:18:01 -04:00
|
|
|
end
|
2007-09-23 07:20:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
class RailsFCGIHandlerSignalsTest < Test::Unit::TestCase
|
2008-04-21 12:54:23 -04:00
|
|
|
class ::RailsFCGIHandler
|
|
|
|
attr_accessor :signal
|
|
|
|
alias_method :old_gc_countdown, :gc_countdown
|
|
|
|
def gc_countdown
|
|
|
|
signal ? Process.kill(signal, $$) : old_gc_countdown
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-09-23 07:20:25 -04:00
|
|
|
def setup
|
|
|
|
@log = StringIO.new
|
|
|
|
@handler = RailsFCGIHandler.new(@log)
|
2008-12-28 21:31:33 -05:00
|
|
|
@dispatcher = mock
|
|
|
|
Dispatcher.stubs(:new).returns(@dispatcher)
|
2007-09-23 07:20:25 -04:00
|
|
|
end
|
2005-06-22 07:18:01 -04:00
|
|
|
|
2005-06-29 07:07:20 -04:00
|
|
|
def test_interrupted_via_HUP_when_not_in_request
|
2009-02-07 01:35:15 -05:00
|
|
|
request = mock
|
|
|
|
FCGI.expects(:each).once.yields(request)
|
2008-04-21 12:54:23 -04:00
|
|
|
@handler.expects(:signal).times(2).returns('HUP')
|
2007-09-23 07:20:25 -04:00
|
|
|
|
|
|
|
@handler.expects(:reload!).once
|
|
|
|
@handler.expects(:close_connection).never
|
|
|
|
@handler.expects(:exit).never
|
|
|
|
|
|
|
|
@handler.process!
|
2005-11-02 08:21:22 -05:00
|
|
|
assert_equal :reload, @handler.when_ready
|
2005-06-29 07:07:20 -04:00
|
|
|
end
|
2005-06-22 07:18:01 -04:00
|
|
|
|
2005-06-29 07:07:20 -04:00
|
|
|
def test_interrupted_via_USR1_when_not_in_request
|
2009-02-07 01:35:15 -05:00
|
|
|
request = mock
|
|
|
|
FCGI.expects(:each).once.yields(request)
|
2008-04-21 12:54:23 -04:00
|
|
|
@handler.expects(:signal).times(2).returns('USR1')
|
2007-09-23 07:20:25 -04:00
|
|
|
@handler.expects(:exit_handler).never
|
|
|
|
|
|
|
|
@handler.expects(:reload!).never
|
2009-02-07 01:35:15 -05:00
|
|
|
@handler.expects(:close_connection).with(request).once
|
2007-09-23 07:20:25 -04:00
|
|
|
@handler.expects(:exit).never
|
|
|
|
|
|
|
|
@handler.process!
|
|
|
|
assert_nil @handler.when_ready
|
2005-06-29 07:07:20 -04:00
|
|
|
end
|
|
|
|
|
2008-04-03 16:00:33 -04:00
|
|
|
def test_restart_via_USR2_when_in_request
|
2009-02-07 01:35:15 -05:00
|
|
|
request = mock
|
|
|
|
FCGI.expects(:each).once.yields(request)
|
2008-04-21 12:54:23 -04:00
|
|
|
@handler.expects(:signal).times(2).returns('USR2')
|
2008-04-03 16:00:33 -04:00
|
|
|
@handler.expects(:exit_handler).never
|
|
|
|
|
|
|
|
@handler.expects(:reload!).never
|
2009-02-07 01:35:15 -05:00
|
|
|
@handler.expects(:close_connection).with(request).once
|
2008-04-03 16:00:33 -04:00
|
|
|
@handler.expects(:exit).never
|
|
|
|
@handler.expects(:restart!).once
|
|
|
|
|
|
|
|
@handler.process!
|
|
|
|
assert_equal :restart, @handler.when_ready
|
|
|
|
end
|
|
|
|
|
2005-11-02 08:21:22 -05:00
|
|
|
def test_interrupted_via_TERM
|
2009-02-07 01:35:15 -05:00
|
|
|
request = mock
|
|
|
|
FCGI.expects(:each).once.yields(request)
|
2008-12-28 21:31:33 -05:00
|
|
|
::Rack::Handler::FastCGI.expects(:serve).once.returns('TERM')
|
2007-09-23 07:20:25 -04:00
|
|
|
|
|
|
|
@handler.expects(:reload!).never
|
|
|
|
@handler.expects(:close_connection).never
|
|
|
|
|
|
|
|
@handler.process!
|
2005-11-02 08:21:22 -05:00
|
|
|
assert_nil @handler.when_ready
|
|
|
|
end
|
2005-06-22 07:18:01 -04:00
|
|
|
|
2007-09-23 07:20:25 -04:00
|
|
|
def test_runtime_exception_in_fcgi
|
|
|
|
error = RuntimeError.new('foo')
|
2009-02-07 01:35:15 -05:00
|
|
|
FCGI.expects(:each).times(2).raises(error)
|
2007-09-23 07:20:25 -04:00
|
|
|
@handler.expects(:dispatcher_error).with(error, regexp_matches(/^retrying/))
|
|
|
|
@handler.expects(:dispatcher_error).with(error, regexp_matches(/^stopping/))
|
|
|
|
@handler.process!
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_runtime_error_in_dispatcher
|
2009-02-07 01:35:15 -05:00
|
|
|
request = mock
|
2007-09-23 07:20:25 -04:00
|
|
|
error = RuntimeError.new('foo')
|
2009-02-07 01:35:15 -05:00
|
|
|
FCGI.expects(:each).once.yields(request)
|
2008-12-28 21:31:33 -05:00
|
|
|
::Rack::Handler::FastCGI.expects(:serve).once.raises(error)
|
2007-09-23 07:20:25 -04:00
|
|
|
@handler.expects(:dispatcher_error).with(error, regexp_matches(/^unhandled/))
|
|
|
|
@handler.process!
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_signal_exception_in_fcgi
|
|
|
|
error = SignalException.new('USR2')
|
2009-02-07 01:35:15 -05:00
|
|
|
FCGI.expects(:each).once.raises(error)
|
2007-09-23 07:20:25 -04:00
|
|
|
@handler.expects(:dispatcher_error).with(error, regexp_matches(/^stopping/))
|
|
|
|
@handler.process!
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_signal_exception_in_dispatcher
|
2009-02-07 01:35:15 -05:00
|
|
|
request = mock
|
2007-09-23 07:20:25 -04:00
|
|
|
error = SignalException.new('USR2')
|
2009-02-07 01:35:15 -05:00
|
|
|
FCGI.expects(:each).once.yields(request)
|
2008-12-28 21:31:33 -05:00
|
|
|
::Rack::Handler::FastCGI.expects(:serve).once.raises(error)
|
2007-09-23 07:20:25 -04:00
|
|
|
@handler.expects(:dispatcher_error).with(error, regexp_matches(/^stopping/))
|
|
|
|
@handler.process!
|
2005-06-22 07:18:01 -04:00
|
|
|
end
|
|
|
|
end
|
2005-07-02 00:52:14 -04:00
|
|
|
|
2007-09-23 07:20:25 -04:00
|
|
|
|
2005-07-02 00:52:14 -04:00
|
|
|
class RailsFCGIHandlerPeriodicGCTest < Test::Unit::TestCase
|
|
|
|
def setup
|
|
|
|
@log = StringIO.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
GC.enable
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_normal_gc
|
|
|
|
@handler = RailsFCGIHandler.new(@log)
|
|
|
|
assert_nil @handler.gc_request_period
|
|
|
|
|
|
|
|
# When GC is enabled, GC.disable disables and returns false.
|
|
|
|
assert_equal false, GC.disable
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_periodic_gc
|
|
|
|
@handler = RailsFCGIHandler.new(@log, 10)
|
|
|
|
assert_equal 10, @handler.gc_request_period
|
|
|
|
|
2009-02-07 01:35:15 -05:00
|
|
|
request = mock
|
|
|
|
FCGI.expects(:each).times(10).yields(request)
|
2005-07-02 00:52:14 -04:00
|
|
|
|
2007-09-23 07:20:25 -04:00
|
|
|
@handler.expects(:run_gc!).never
|
|
|
|
9.times { @handler.process! }
|
|
|
|
@handler.expects(:run_gc!).once
|
2005-07-02 00:52:14 -04:00
|
|
|
@handler.process!
|
|
|
|
|
|
|
|
assert_nil @handler.when_ready
|
|
|
|
end
|
|
|
|
end
|
2009-02-16 16:12:28 -05:00
|
|
|
end # uses_gem "fcgi"
|