OSDN Git Service

d78f8111b7c1bac92e6505e70861f8859e6a6a0e
[redminele/redminele.git] / ruby / lib / ruby / gems / 1.8 / gems / actionpack-2.3.11 / test / controller / reloader_test.rb
1 require 'abstract_unit'
2 require 'thread'
3
4 class ReloaderTests < ActiveSupport::TestCase
5   Reloader   = ActionController::Reloader
6   Dispatcher = ActionController::Dispatcher
7
8   class MyBody < Array
9     def initialize(&block)
10       @on_close = block
11     end
12
13     def foo
14       "foo"
15     end
16
17     def bar
18       "bar"
19     end
20
21     def close
22       @on_close.call if @on_close
23     end
24   end
25
26   def setup
27     @lock = Mutex.new
28   end
29
30   def test_it_reloads_the_application_before_yielding
31     Dispatcher.expects(:reload_application)
32     Reloader.run(@lock) do
33       [200, { "Content-Type" => "text/html" }, [""]]
34     end
35   end
36
37   def test_it_locks_before_yielding
38     lock = DummyMutex.new
39     Dispatcher.expects(:reload_application)
40     Reloader.run(lock) do
41       assert lock.locked?
42       [200, { "Content-Type" => "text/html" }, [""]]
43     end
44     assert lock.locked?
45   end
46
47   def test_it_unlocks_upon_calling_close_on_body
48     lock = DummyMutex.new
49     Dispatcher.expects(:reload_application)
50     headers, status, body = Reloader.run(lock) do
51       [200, { "Content-Type" => "text/html" }, [""]]
52     end
53     body.close
54     assert !lock.locked?
55   end
56
57   def test_it_unlocks_if_app_object_raises_exception
58     lock = DummyMutex.new
59     Dispatcher.expects(:reload_application)
60     assert_raise(RuntimeError) do
61       Reloader.run(lock) do
62         raise "oh no!"
63       end
64     end
65     assert !lock.locked?
66   end
67
68   def test_returned_body_object_always_responds_to_close
69     status, headers, body = Reloader.run(@lock) do
70       [200, { "Content-Type" => "text/html" }, [""]]
71     end
72     assert body.respond_to?(:close)
73   end
74
75   def test_returned_body_object_behaves_like_underlying_object
76     status, headers, body = Reloader.run(@lock) do
77       b = MyBody.new
78       b << "hello"
79       b << "world"
80       [200, { "Content-Type" => "text/html" }, b]
81     end
82     assert_equal 2, body.size
83     assert_equal "hello", body[0]
84     assert_equal "world", body[1]
85     assert_equal "foo", body.foo
86     assert_equal "bar", body.bar
87   end
88
89   def test_it_calls_close_on_underlying_object_when_close_is_called_on_body
90     close_called = false
91     status, headers, body = Reloader.run(@lock) do
92       b = MyBody.new do
93         close_called = true
94       end
95       [200, { "Content-Type" => "text/html" }, b]
96     end
97     body.close
98     assert close_called
99   end
100
101   def test_returned_body_object_responds_to_all_methods_supported_by_underlying_object
102     status, headers, body = Reloader.run(@lock) do
103       [200, { "Content-Type" => "text/html" }, MyBody.new]
104     end
105     assert body.respond_to?(:size)
106     assert body.respond_to?(:each)
107     assert body.respond_to?(:foo)
108     assert body.respond_to?(:bar)
109   end
110
111   def test_it_doesnt_clean_up_the_application_after_call
112     Dispatcher.expects(:cleanup_application).never
113     status, headers, body = Reloader.run(@lock) do
114       [200, { "Content-Type" => "text/html" }, MyBody.new]
115     end
116   end
117
118   def test_it_cleans_up_the_application_when_close_is_called_on_body
119     Dispatcher.expects(:cleanup_application)
120     status, headers, body = Reloader.run(@lock) do
121       [200, { "Content-Type" => "text/html" }, MyBody.new]
122     end
123     body.close
124   end
125 end