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

Changeset 4913

Show
Ignore:
Timestamp:
09/03/06 00:18:31 (2 years ago)
Author:
bitsweat
Message:

Thoroughly test the FCGI dispatcher. Closes #5970.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/railties/CHANGELOG

    r4899 r4913  
    11*SVN* 
     2 
     3* Thoroughly test the FCGI dispatcher.  #5970 [Kevin Clark] 
    24 
    35* Remove Dir.chdir in the Webrick DispatchServlet#initialize method.  Fix bad path errors when trying to load config/routes.rb.  [Rick Olson] 
  • trunk/railties/lib/fcgi_handler.rb

    r4885 r4913  
    5151    run_gc! if gc_request_period 
    5252 
    53     provider.each_cgi do |cgi|  
    54       process_request(cgi) 
    55  
    56       case when_ready 
    57         when :reload 
    58           reload! 
    59         when :restart 
    60           close_connection(cgi) 
    61           restart! 
    62         when :exit 
    63           close_connection(cgi) 
    64           break 
    65         when :breakpoint 
    66           close_connection(cgi) 
    67           breakpoint! 
    68       end 
    69  
    70       gc_countdown 
    71     end 
     53    process_each_request!(provider) 
    7254 
    7355    GC.enable 
     
    8870    end 
    8971  end 
    90    
    91    
    92   private 
     72 
     73 
     74  protected 
    9375    def logger 
    9476      @logger ||= Logger.new(@log_file_path) 
     
    145127      dispatcher_log :info, "asked to breakpoint ASAP" 
    146128      @when_ready = :breakpoint 
     129    end 
     130     
     131    def process_each_request!(provider) 
     132      provider.each_cgi do |cgi|  
     133        process_request(cgi) 
     134 
     135        case when_ready 
     136          when :reload 
     137            reload! 
     138          when :restart 
     139            close_connection(cgi) 
     140            restart! 
     141          when :exit 
     142            close_connection(cgi) 
     143            break 
     144          when :breakpoint 
     145            close_connection(cgi) 
     146            breakpoint! 
     147        end 
     148 
     149        gc_countdown 
     150      end 
    147151    end 
    148152 
  • trunk/railties/test/fcgi_dispatcher_test.rb

    r2847 r4913  
    1 $:.unshift File.dirname(__FILE__) + "/../lib" 
     1require File.dirname(__FILE__) + "/abstract_unit" 
     2 
    23$:.unshift File.dirname(__FILE__) + "/mocks" 
    34 
    4 require 'test/unit' 
    55require 'stringio' 
     6 
     7# Stubs 
    68require 'fcgi_handler' 
    7  
    8 RAILS_ROOT = File.dirname(__FILE__) if !defined?(RAILS_ROOT) 
     9require 'routes' 
     10require 'stubbed_breakpoint' 
     11require 'stubbed_kernel' 
    912 
    1013class RailsFCGIHandler 
     
    2730    @signal_handlers[which].call(which) 
    2831  end 
    29  
    30   def restore! 
    31     @reloaded = true 
    32   end 
    33    
    34   def reload! 
    35     @reloaded = true 
     32   
     33  def breakpoint 
    3634  end 
    3735 
     
    5452  end 
    5553 
     54  def test_process_restart 
     55    @handler.stubs(:when_ready).returns(:restart) 
     56     
     57    @handler.expects(:close_connection) 
     58    @handler.expects(:restart!) 
     59    @handler.process! 
     60  end 
     61   
     62  def test_process_exit 
     63    @handler.stubs(:when_ready).returns(:exit) 
     64     
     65    @handler.expects(:close_connection) 
     66    @handler.process! 
     67  end 
     68   
     69  def test_process_breakpoint 
     70    @handler.stubs(:when_ready).returns(:breakpoint) 
     71     
     72    @handler.expects(:close_connection) 
     73    @handler.expects(:breakpoint!) 
     74    @handler.process! 
     75  end 
     76   
     77  def test_process_with_system_exit_exception 
     78    @handler.stubs(:process_request).raises(SystemExit) 
     79     
     80    @handler.expects(:dispatcher_log).with(:info, "terminated by explicit exit") 
     81    @handler.process! 
     82  end 
     83   
     84  def test_restart_handler 
     85    @handler.expects(:dispatcher_log).with(:info, "asked to restart ASAP") 
     86     
     87    @handler.send(:restart_handler, nil) 
     88    assert_equal :restart, @handler.when_ready 
     89  end 
     90   
     91  def test_breakpoint_handler 
     92    @handler.expects(:dispatcher_log).with(:info, "asked to breakpoint ASAP") 
     93 
     94    @handler.send(:breakpoint_handler, nil) 
     95    assert_equal :breakpoint, @handler.when_ready 
     96  end 
     97   
     98  def test_install_signal_handler_should_log_on_bad_signal 
     99    @handler.stubs(:trap).raises(ArgumentError) 
     100 
     101    @handler.expects(:dispatcher_log).with(:warn, "Ignoring unsupported signal CHEESECAKE.") 
     102    @handler.send(:install_signal_handler, "CHEESECAKE", nil) 
     103  end 
     104   
     105  def test_reload 
     106    @handler.expects(:restore!) 
     107    @handler.expects(:dispatcher_log).with(:info, "reloaded") 
     108 
     109    @handler.send(:reload!) 
     110    assert_nil @handler.when_ready 
     111  end 
     112   
     113   
     114  def test_reload_runs_gc_when_gc_request_period_set 
     115    @handler.expects(:run_gc!) 
     116    @handler.expects(:restore!) 
     117    @handler.expects(:dispatcher_log).with(:info, "reloaded") 
     118    @handler.gc_request_period = 10 
     119    @handler.send(:reload!) 
     120  end 
     121   
     122  def test_reload_doesnt_run_gc_if_gc_request_period_isnt_set 
     123    @handler.expects(:run_gc!).never 
     124    @handler.expects(:restore!) 
     125    @handler.expects(:dispatcher_log).with(:info, "reloaded") 
     126    @handler.send(:reload!) 
     127  end 
     128   
     129  def test_restart! 
     130    @handler.expects(:dispatcher_log).with(:info, "restarted") 
     131    assert_equal true, @handler.send(:restart!), "Exec wasn't run" 
     132  end 
     133   
     134  def test_restore! 
     135    $".expects(:replace) 
     136    Dispatcher.expects(:reset_application!) 
     137    ActionController::Routing::Routes.expects(:reload) 
     138    @handler.send(:restore!) 
     139  end 
     140 
     141  def test_breakpoint! 
     142    @handler.expects(:require).with('breakpoint') 
     143    Breakpoint.expects(:activate_drb) 
     144    @handler.expects(:breakpoint) 
     145    @handler.expects(:dispatcher_log).with(:info, "breakpointing") 
     146    @handler.send(:breakpoint!) 
     147    assert_nil @handler.when_ready 
     148  end 
     149   
    56150  def test_uninterrupted_processing 
    57151    @handler.process! 
     
    61155 
    62156  def test_interrupted_via_HUP_when_not_in_request 
     157    @handler.expects(:reload!) 
    63158    FCGI.time_to_sleep = 1 
    64159    @handler.thread = Thread.new { @handler.process! } 
     
    68163    assert_nil @handler.exit_code 
    69164    assert_equal :reload, @handler.when_ready 
    70     assert @handler.reloaded 
    71165  end 
    72166 
    73167  def test_interrupted_via_HUP_when_in_request 
     168    @handler.expects(:reload!) 
     169     
    74170    Dispatcher.time_to_sleep = 1 
    75171    @handler.thread = Thread.new { @handler.process! } 
     
    79175    assert_nil @handler.exit_code 
    80176    assert_equal :reload, @handler.when_ready 
    81     assert @handler.reloaded 
    82177  end 
    83178