fix process#alive? to not raise on no such file
[god.git] / test / test_hub.rb
blobec8f7e509c59d1822cd2c3433889f7e31a6fe241
1 require File.dirname(__FILE__) + '/helper'
3 class TestHub < Test::Unit::TestCase
4   def setup
5     Server.stubs(:new).returns(true)
6     God.reset
7     
8     God.watch do |w|
9       w.name = 'foo'
10       w.start = 'bar'
11       w.interval = 10
12     end
13     
14     @watch = God.watches['foo']
15   end
16   
17   # attach
18   
19   def test_attach_should_store_condition_metric_association
20     c = Conditions::FakePollCondition.new
21     m = Metric.new(@watch, :foo)
22     Hub.attach(c, m)
23     
24     assert_equal m, Hub.directory[c]
25   end
26   
27   def test_attach_should_schedule_for_poll_condition
28     c = Conditions::FakePollCondition.new
29     m = Metric.new(@watch, :foo)
30     
31     Timer.any_instance.expects(:schedule).with(c, 0)
32     
33     Hub.attach(c, m)
34   end
35   
36   def test_attach_should_regsiter_for_event_condition
37     c = Conditions::FakeEventCondition.new
38     m = Metric.new(@watch, :foo)
39     
40     c.expects(:register)
41     
42     Hub.attach(c, m)
43   end
44   
45   # detach
46   
47   def test_detach_should_remove_condition_metric_association
48     c = Conditions::FakePollCondition.new
49     m = Metric.new(@watch, :foo)
50     
51     Hub.attach(c, m)
52     Hub.detach(c)
53     
54     assert_nil Hub.directory[c]
55   end
56   
57   def test_detach_should_unschedule_poll_conditions
58     c = Conditions::FakePollCondition.new
59     m = Metric.new(@watch, :foo)
60     Hub.attach(c, m)
61     
62     Timer.any_instance.expects(:unschedule).with(c)
63     c.expects(:deregister).never
64     
65     Hub.detach(c)
66   end
67   
68   def test_detach_should_deregister_event_conditions
69     c = Conditions::FakeEventCondition.new
70     m = Metric.new(@watch, :foo)
71     Hub.attach(c, m)
72     
73     c.expects(:deregister).once
74     
75     Hub.detach(c)
76   end
77   
78   # trigger
79   
80   def test_trigger_should_handle_poll_for_poll_condition
81     c = Conditions::FakePollCondition.new
82     Hub.expects(:handle_poll).with(c)
83     
84     Hub.trigger(c)
85   end
86   
87   def test_trigger_should_handle_event_for_event_condition
88     c = Conditions::FakeEventCondition.new
89     Hub.expects(:handle_event).with(c)
90     
91     Hub.trigger(c)
92   end
93   
94   # handle_poll
95   
96   def test_handle_poll_no_change_should_reschedule
97     c = Conditions::FakePollCondition.new
98     c.interval = 10
99     
100     m = Metric.new(@watch, {true => :up})
101     Hub.attach(c, m)
102     
103     c.expects(:test).returns(false)
104     Timer.any_instance.expects(:schedule)
105     
106     no_stdout do
107       t = Hub.handle_poll(c)
108       t.join
109     end
110   end
111   
112   def test_handle_poll_change_should_move
113     c = Conditions::FakePollCondition.new
114     c.interval = 10
115     
116     m = Metric.new(@watch, {true => :up})
117     Hub.attach(c, m)
118     
119     c.expects(:test).returns(true)
120     @watch.expects(:move).with(:up)
121     
122     no_stdout do
123       t = Hub.handle_poll(c)
124       t.join
125     end
126   end
127   
128   def test_handle_poll_should_abort_on_exception
129     c = Conditions::FakePollCondition.new
130     c.interval = 10
131     
132     m = Metric.new(@watch, {true => :up})
133     Hub.attach(c, m)
134     
135     c.expects(:test).raises(StandardError.new)
136     
137     assert_abort do
138       t = Hub.handle_poll(c)
139       t.join
140     end
141   end
142   
143   def test_handle_poll_should_use_overridden_transition
144     c = Conditions::Tries.new
145     c.times = 1
146     c.transition = :start
147     c.prepare
148     
149     m = Metric.new(@watch, {true => :up})
150     Hub.attach(c, m)
151     
152     @watch.expects(:move).with(:start)
153     
154     no_stdout do
155       t = Hub.handle_poll(c)
156       t.join
157     end
158   end
159   
160   # handle_event
161   
162   def test_handle_event_should_move
163     c = Conditions::FakeEventCondition.new
164     
165     m = Metric.new(@watch, {true => :up})
166     Hub.attach(c, m)
167     
168     @watch.expects(:move).with(:up)
169     
170     no_stdout do
171       t = Hub.handle_event(c)
172       t.join
173     end
174   end