more diagnostics
[god.git] / test / test_hub.rb
blobee0526d02aeb2506549d00ea985a4d2ca772d8d8
1 require File.dirname(__FILE__) + '/helper'
3 class TestHub < Test::Unit::TestCase
4   def setup
5     God::Socket.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     @watch.phase = Time.now
16   end
17   
18   # attach
19   
20   def test_attach_should_store_condition_metric_association
21     c = Conditions::FakePollCondition.new
22     c.watch = @watch
23     m = Metric.new(@watch, {true => :up})
24     Hub.attach(c, m)
25     
26     assert_equal m, Hub.directory[c]
27   end
28   
29   def test_attach_should_schedule_for_poll_condition
30     c = Conditions::FakePollCondition.new
31     c.watch = @watch
32     m = Metric.new(@watch, {true => :up})
33     
34     Timer.any_instance.expects(:schedule).with(c, 0)
35     
36     Hub.attach(c, m)
37   end
38   
39   def test_attach_should_regsiter_for_event_condition
40     c = Conditions::FakeEventCondition.new
41     c.watch = @watch
42     m = Metric.new(@watch, {true => :up})
43     
44     c.expects(:register)
45     
46     Hub.attach(c, m)
47   end
48   
49   # detach
50   
51   def test_detach_should_remove_condition_metric_association
52     c = Conditions::FakePollCondition.new
53     c.watch = @watch
54     m = Metric.new(@watch, {true => :up})
55     
56     Hub.attach(c, m)
57     Hub.detach(c)
58     
59     assert_nil Hub.directory[c]
60   end
61   
62   def test_detach_should_unschedule_poll_conditions
63     c = Conditions::FakePollCondition.new
64     c.watch = @watch
65     m = Metric.new(@watch, {true => :up})
66     Hub.attach(c, m)
67     
68     Timer.any_instance.expects(:unschedule).with(c)
69     c.expects(:deregister).never
70     
71     Hub.detach(c)
72   end
73   
74   def test_detach_should_deregister_event_conditions
75     c = Conditions::FakeEventCondition.new
76     c.watch = @watch
77     m = Metric.new(@watch, {true => :up})
78     Hub.attach(c, m)
79     
80     c.expects(:deregister).once
81     
82     Hub.detach(c)
83   end
84   
85   # trigger
86   
87   def test_trigger_should_handle_poll_for_poll_condition
88     c = Conditions::FakePollCondition.new
89     c.watch = @watch
90     Hub.expects(:handle_poll).with(c, @watch.phase)
91     
92     Hub.trigger(c, @watch.phase)
93   end
94   
95   def test_trigger_should_handle_event_for_event_condition
96     c = Conditions::FakeEventCondition.new
97     Hub.expects(:handle_event).with(c)
98     
99     Hub.trigger(c, @watch.phase)
100   end
101   
102   # handle_poll
103   
104   def test_handle_poll_no_change_should_reschedule
105     c = Conditions::FakePollCondition.new
106     c.watch = @watch
107     c.interval = 10
108     
109     m = Metric.new(@watch, {true => :up})
110     Hub.attach(c, m)
111     
112     c.expects(:test).returns(false)
113     Timer.any_instance.expects(:schedule)
114     
115     no_stdout do
116       t = Hub.handle_poll(c, @watch.phase)
117       t.join
118     end
119   end
120   
121   def test_handle_poll_change_should_move
122     c = Conditions::FakePollCondition.new
123     c.watch = @watch
124     c.interval = 10
125     
126     m = Metric.new(@watch, {true => :up})
127     Hub.attach(c, m)
128     
129     c.expects(:test).returns(true)
130     @watch.expects(:move).with(:up)
131     
132     no_stdout do
133       t = Hub.handle_poll(c, @watch.phase)
134       t.join
135     end
136   end
137   
138   def test_handle_poll_should_not_abort_on_exception
139     c = Conditions::FakePollCondition.new
140     c.watch = @watch
141     c.interval = 10
142     
143     m = Metric.new(@watch, {true => :up})
144     Hub.attach(c, m)
145     
146     c.expects(:test).raises(StandardError.new)
147     
148     assert_nothing_raised do
149       no_stdout do
150         t = Hub.handle_poll(c, @watch.phase)
151         t.join
152       end
153     end
154   end
155   
156   def test_handle_poll_should_use_overridden_transition
157     c = Conditions::Tries.new
158     c.watch = @watch
159     c.times = 1
160     c.transition = :start
161     c.prepare
162     
163     m = Metric.new(@watch, {true => :up})
164     Hub.attach(c, m)
165     
166     @watch.expects(:move).with(:start)
167     
168     no_stdout do
169       t = Hub.handle_poll(c, @watch.phase)
170       t.join
171     end
172   end
173   
174   def test_handle_poll_should_notify_if_triggering
175     c = Conditions::FakePollCondition.new
176     c.watch = @watch
177     c.interval = 10
178     c.notify = 'tom'
179     
180     m = Metric.new(@watch, {true => :up})
181     Hub.attach(c, m)
182     
183     c.expects(:test).returns(true)
184     Hub.expects(:notify)
185     
186     no_stdout do
187       t = Hub.handle_poll(c, @watch.phase)
188       t.join
189     end
190   end
191   
192   def test_handle_poll_should_not_notify_if_not_triggering
193     c = Conditions::FakePollCondition.new
194     c.watch = @watch
195     c.interval = 10
196     c.notify = 'tom'
197     
198     m = Metric.new(@watch, {true => :up})
199     Hub.attach(c, m)
200     
201     c.expects(:test).returns(false)
202     Hub.expects(:notify).never
203     
204     no_stdout do
205       t = Hub.handle_poll(c, @watch.phase)
206       t.join
207     end
208   end
209   
210   # handle_event
211   
212   def test_handle_event_should_move
213     c = Conditions::FakeEventCondition.new
214     c.watch = @watch
215     
216     m = Metric.new(@watch, {true => :up})
217     Hub.attach(c, m)
218     
219     @watch.expects(:move).with(:up)
220     
221     no_stdout do
222       t = Hub.handle_event(c)
223       t.join
224     end
225   end
226   
227   def test_handle_event_should_notify_if_triggering
228     c = Conditions::FakeEventCondition.new
229     c.watch = @watch
230     c.notify = 'tom'
231     
232     m = Metric.new(@watch, {true => :up})
233     Hub.attach(c, m)
234     
235     Hub.expects(:notify)
236     
237     no_stdout do
238       t = Hub.handle_event(c)
239       t.join
240     end
241   end
242   
243   def test_handle_event_should_not_notify_if_no_notify_set
244     c = Conditions::FakeEventCondition.new
245     c.watch = @watch
246     
247     m = Metric.new(@watch, {true => :up})
248     Hub.attach(c, m)
249     
250     Hub.expects(:notify).never
251     
252     no_stdout do
253       t = Hub.handle_event(c)
254       t.join
255     end
256   end