aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/fcgi_dispatcher_test.rb
blob: 466eefe22549000af922ac81eab09ebffbfc0fec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
$:.unshift File.dirname(__FILE__) + "/mocks"

require 'test/unit'
require 'stringio'

if !defined?(RailsFCGIHandler)
  RAILS_ROOT = File.dirname(__FILE__)
  load File.dirname(__FILE__) + "/../dispatches/dispatch.fcgi"
end

class RailsFCGIHandler
  attr_reader :exit_code
  attr_accessor :thread

  def trap(signal, handler, &block)
    handler ||= block
    (@signal_handlers ||= Hash.new)[signal] = handler
  end

  def exit(code=0)
    @exit_code = code
    (thread || Thread.current).exit
  end

  def send_signal(which)
    @signal_handlers[which].call(which)
  end
end

class RailsFCGIHandlerTest < Test::Unit::TestCase
  def setup
    @log = StringIO.new
    @handler = RailsFCGIHandler.new(@log)
    FCGI.time_to_sleep = nil
    FCGI.raise_exception = nil
    Dispatcher.time_to_sleep = nil
    Dispatcher.raise_exception = nil
  end

  def test_uninterrupted_processing
    @handler.process!
    assert_nil @handler.exit_code
    assert !@handler.please_exit_at_your_earliest_convenience
    assert !@handler.i_am_currently_processing_a_request
  end

  %w(HUP USR1).each do |signal|
    define_method("test_interrupted_via_#{signal}_when_not_in_request") do
      FCGI.time_to_sleep = 1
      @handler.thread = Thread.new { @handler.process! }
      sleep 0.1 # let the thread get started
      @handler.send_signal(signal)
      @handler.thread.join
      assert_equal 0, @handler.exit_code
      assert !@handler.please_exit_at_your_earliest_convenience
      assert !@handler.i_am_currently_processing_a_request
    end

    define_method("test_interrupted_via_#{signal}_when_in_request") do
      Dispatcher.time_to_sleep = 1
      @handler.thread = Thread.new { @handler.process! }
      sleep 0.1 # let the thread get started
      @handler.send_signal(signal)
      @handler.thread.join
      assert_nil @handler.exit_code
      assert @handler.please_exit_at_your_earliest_convenience
      assert !@handler.i_am_currently_processing_a_request
    end
  end

  %w(RuntimeError SignalException).each do |exception|
    define_method("test_#{exception}_in_fcgi") do
      FCGI.raise_exception = Object.const_get(exception)
      @handler.process!
      assert_match %r{Dispatcher failed to catch}, @log.string
      case exception
        when "RuntimeError"
          assert_match %r{almost killed}, @log.string
        when "SignalException"
          assert_match %r{\d killed}, @log.string
      end
    end

    define_method("test_#{exception}_in_dispatcher") do
      Dispatcher.raise_exception = Object.const_get(exception)
      @handler.process!
      assert_match %r{Dispatcher failed to catch}, @log.string
      case exception
        when "RuntimeError"
          assert_no_match %r{killed}, @log.string
        when "SignalException"
          assert_match %r{\d killed}, @log.string
      end
    end
  end
end