tighten up Hub and add comments
[god.git] / test / test_hub.rb
blob41478ce7dfa61ed1fda5d296e4217ca0a18da868
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   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_not_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_nothing_raised do
138       no_stdout do
139         t = Hub.handle_poll(c)
140         t.join
141       end
142     end
143   end
144   
145   def test_handle_poll_should_use_overridden_transition
146     c = Conditions::Tries.new
147     c.times = 1
148     c.transition = :start
149     c.prepare
150     
151     m = Metric.new(@watch, {true => :up})
152     Hub.attach(c, m)
153     
154     @watch.expects(:move).with(:start)
155     
156     no_stdout do
157       t = Hub.handle_poll(c)
158       t.join
159     end
160   end
161   
162   def test_handle_poll_should_notify_if_triggering
163     c = Conditions::FakePollCondition.new
164     c.interval = 10
165     c.notify = 'tom'
166     
167     m = Metric.new(@watch, {true => :up})
168     Hub.attach(c, m)
169     
170     c.expects(:test).returns(true)
171     Hub.expects(:notify)
172     
173     no_stdout do
174       t = Hub.handle_poll(c)
175       t.join
176     end
177   end
178   
179   def test_handle_poll_should_not_notify_if_not_triggering
180     c = Conditions::FakePollCondition.new
181     c.interval = 10
182     c.notify = 'tom'
183     
184     m = Metric.new(@watch, {true => :up})
185     Hub.attach(c, m)
186     
187     c.expects(:test).returns(false)
188     Hub.expects(:notify).never
189     
190     no_stdout do
191       t = Hub.handle_poll(c)
192       t.join
193     end
194   end
195   
196   # handle_event
197   
198   def test_handle_event_should_move
199     c = Conditions::FakeEventCondition.new
200     
201     m = Metric.new(@watch, {true => :up})
202     Hub.attach(c, m)
203     
204     @watch.expects(:move).with(:up)
205     
206     no_stdout do
207       t = Hub.handle_event(c)
208       t.join
209     end
210   end
211   
212   def test_handle_event_should_notify_if_triggering
213     c = Conditions::FakeEventCondition.new
214     c.notify = 'tom'
215     
216     m = Metric.new(@watch, {true => :up})
217     Hub.attach(c, m)
218     
219     Hub.expects(:notify)
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_not_notify_if_no_notify_set
228     c = Conditions::FakeEventCondition.new
229     
230     m = Metric.new(@watch, {true => :up})
231     Hub.attach(c, m)
232     
233     Hub.expects(:notify).never
234     
235     no_stdout do
236       t = Hub.handle_event(c)
237       t.join
238     end
239   end