Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

root/branches/2-1-caching/railties/test/fcgi_dispatcher_test.rb

Revision 7593, 7.1 kB (checked in by bitsweat, 1 year ago)

RailsFCGIHandler tests. Closes #9630.

Line 
1 require File.dirname(__FILE__) + "/abstract_unit"
2
3 uses_mocha 'fcgi dispatcher tests' do
4
5 require 'fcgi_handler'
6
7 module ActionController; module Routing; module Routes; end end end
8
9 class RailsFCGIHandlerTest < Test::Unit::TestCase
10   def setup
11     @log = StringIO.new
12     @handler = RailsFCGIHandler.new(@log)
13   end
14
15   def test_process_restart
16     cgi = mock
17     FCGI.stubs(:each_cgi).yields(cgi)
18
19     @handler.expects(:process_request).once
20     @handler.expects(:dispatcher_error).never
21
22     @handler.expects(:when_ready).returns(:restart)
23     @handler.expects(:close_connection).with(cgi)
24     @handler.expects(:reload!).never
25     @handler.expects(:restart!)
26
27     @handler.process!
28   end
29
30   def test_process_exit
31     cgi = mock
32     FCGI.stubs(:each_cgi).yields(cgi)
33
34     @handler.expects(:process_request).once
35     @handler.expects(:dispatcher_error).never
36
37     @handler.expects(:when_ready).returns(:exit)
38     @handler.expects(:close_connection).with(cgi)
39     @handler.expects(:reload!).never
40     @handler.expects(:restart!).never
41
42     @handler.process!
43   end
44
45   def test_process_with_system_exit_exception
46     cgi = mock
47     FCGI.stubs(:each_cgi).yields(cgi)
48
49     @handler.expects(:process_request).once.raises(SystemExit)
50     @handler.stubs(:dispatcher_log)
51     @handler.expects(:dispatcher_log).with(:info, regexp_matches(/^stopping/))
52     @handler.expects(:dispatcher_error).never
53
54     @handler.expects(:when_ready).never
55     @handler.expects(:close_connection).never
56     @handler.expects(:reload!).never
57     @handler.expects(:restart!).never
58
59     @handler.process!
60   end
61
62   def test_restart_handler
63     @handler.expects(:dispatcher_log).with(:info, "asked to restart ASAP")
64
65     @handler.send(:restart_handler, nil)
66     assert_equal :restart, @handler.when_ready
67   end
68
69   def test_install_signal_handler_should_log_on_bad_signal
70     @handler.stubs(:trap).raises(ArgumentError)
71
72     @handler.expects(:dispatcher_log).with(:warn, "Ignoring unsupported signal CHEESECAKE.")
73     @handler.send(:install_signal_handler, "CHEESECAKE", nil)
74   end
75
76   def test_reload
77     @handler.expects(:restore!)
78     @handler.expects(:dispatcher_log).with(:info, "reloaded")
79
80     @handler.send(:reload!)
81     assert_nil @handler.when_ready
82   end
83
84
85   def test_reload_runs_gc_when_gc_request_period_set
86     @handler.expects(:run_gc!)
87     @handler.expects(:restore!)
88     @handler.expects(:dispatcher_log).with(:info, "reloaded")
89     @handler.gc_request_period = 10
90     @handler.send(:reload!)
91   end
92
93   def test_reload_doesnt_run_gc_if_gc_request_period_isnt_set
94     @handler.expects(:run_gc!).never
95     @handler.expects(:restore!)
96     @handler.expects(:dispatcher_log).with(:info, "reloaded")
97     @handler.send(:reload!)
98   end
99
100   def test_restart!
101     @handler.expects(:dispatcher_log).with(:info, "restarted")
102     @handler.expects(:exec).returns('restarted')
103     assert_equal 'restarted', @handler.send(:restart!)
104   end
105
106   def test_restore!
107     $".expects(:replace)
108     Dispatcher.expects(:reset_application!)
109     ActionController::Routing::Routes.expects(:reload)
110     @handler.send(:restore!)
111   end
112
113   def test_uninterrupted_processing
114     cgi = mock
115     FCGI.expects(:each_cgi).yields(cgi)
116     @handler.expects(:process_request).with(cgi)
117
118     @handler.process!
119
120     assert_nil @handler.when_ready
121   end
122 end
123
124
125 class RailsFCGIHandlerSignalsTest < Test::Unit::TestCase
126   def setup
127     @log = StringIO.new
128     @handler = RailsFCGIHandler.new(@log)
129   end
130
131   def test_interrupted_via_HUP_when_not_in_request
132     cgi = mock
133     FCGI.expects(:each_cgi).once.yields(cgi)
134     @handler.expects(:gc_countdown).returns { Process.kill 'HUP', $$ }
135
136     @handler.expects(:reload!).once
137     @handler.expects(:close_connection).never
138     @handler.expects(:exit).never
139
140     @handler.process!
141     assert_equal :reload, @handler.when_ready
142   end
143
144   def test_interrupted_via_HUP_when_in_request
145     cgi = mock
146     FCGI.expects(:each_cgi).once.yields(cgi)
147     Dispatcher.expects(:dispatch).with(cgi).returns { Process.kill 'HUP', $$ }
148
149     @handler.expects(:reload!).once
150     @handler.expects(:close_connection).never
151     @handler.expects(:exit).never
152
153     @handler.process!
154     assert_equal :reload, @handler.when_ready
155   end
156
157   def test_interrupted_via_USR1_when_not_in_request
158     cgi = mock
159     FCGI.expects(:each_cgi).once.yields(cgi)
160     @handler.expects(:gc_countdown).returns { Process.kill 'USR1', $$ }
161     @handler.expects(:exit_handler).never
162
163     @handler.expects(:reload!).never
164     @handler.expects(:close_connection).with(cgi).once
165     @handler.expects(:exit).never
166
167     @handler.process!
168     assert_nil @handler.when_ready
169   end
170
171   def test_interrupted_via_USR1_when_in_request
172     cgi = mock
173     FCGI.expects(:each_cgi).once.yields(cgi)
174     Dispatcher.expects(:dispatch).with(cgi).returns { Process.kill 'USR1', $$ }
175
176     @handler.expects(:reload!).never
177     @handler.expects(:close_connection).with(cgi).once
178     @handler.expects(:exit).never
179
180     @handler.process!
181     assert_equal :exit, @handler.when_ready
182   end
183
184   def test_interrupted_via_TERM
185     cgi = mock
186     FCGI.expects(:each_cgi).once.yields(cgi)
187     Dispatcher.expects(:dispatch).with(cgi).returns { Process.kill 'TERM', $$ }
188
189     @handler.expects(:reload!).never
190     @handler.expects(:close_connection).never
191
192     @handler.process!
193     assert_nil @handler.when_ready
194   end
195
196   def test_runtime_exception_in_fcgi
197     error = RuntimeError.new('foo')
198     FCGI.expects(:each_cgi).times(2).raises(error)
199     @handler.expects(:dispatcher_error).with(error, regexp_matches(/^retrying/))
200     @handler.expects(:dispatcher_error).with(error, regexp_matches(/^stopping/))
201     @handler.process!
202   end
203
204   def test_runtime_error_in_dispatcher
205     cgi = mock
206     error = RuntimeError.new('foo')
207     FCGI.expects(:each_cgi).once.yields(cgi)
208     Dispatcher.expects(:dispatch).once.with(cgi).raises(error)
209     @handler.expects(:dispatcher_error).with(error, regexp_matches(/^unhandled/))
210     @handler.process!
211   end
212
213   def test_signal_exception_in_fcgi
214     error = SignalException.new('USR2')
215     FCGI.expects(:each_cgi).once.raises(error)
216     @handler.expects(:dispatcher_error).with(error, regexp_matches(/^stopping/))
217     @handler.process!
218   end
219
220   def test_signal_exception_in_dispatcher
221     cgi = mock
222     error = SignalException.new('USR2')
223     FCGI.expects(:each_cgi).once.yields(cgi)
224     Dispatcher.expects(:dispatch).once.with(cgi).raises(error)
225     @handler.expects(:dispatcher_error).with(error, regexp_matches(/^stopping/))
226     @handler.process!
227   end
228 end
229
230
231 class RailsFCGIHandlerPeriodicGCTest < Test::Unit::TestCase
232   def setup
233     @log = StringIO.new
234   end
235
236   def teardown
237     GC.enable
238   end
239
240   def test_normal_gc
241     @handler = RailsFCGIHandler.new(@log)
242     assert_nil @handler.gc_request_period
243
244     # When GC is enabled, GC.disable disables and returns false.
245     assert_equal false, GC.disable
246   end
247
248   def test_periodic_gc
249     @handler = RailsFCGIHandler.new(@log, 10)
250     assert_equal 10, @handler.gc_request_period
251
252     cgi = mock
253     FCGI.expects(:each_cgi).times(10).yields(cgi)
254     Dispatcher.expects(:dispatch).times(10).with(cgi)
255
256     @handler.expects(:run_gc!).never
257     9.times { @handler.process! }
258     @handler.expects(:run_gc!).once
259     @handler.process!
260
261     assert_nil @handler.when_ready
262   end
263 end
264
265 end # uses_mocha
Note: See TracBrowser for help on using the browser.