1 require File.dirname(__FILE__) + '/helper'
3 class TestHub < Test::Unit::TestCase
5 God::Socket.stubs(:new).returns(true)
14 @watch = God.watches['foo']
15 @watch.phase = Time.now
20 def test_attach_should_store_condition_metric_association
21 c = Conditions::FakePollCondition.new
23 m = Metric.new(@watch, {true => :up})
26 assert_equal m, Hub.directory[c]
29 def test_attach_should_schedule_for_poll_condition
30 c = Conditions::FakePollCondition.new
32 m = Metric.new(@watch, {true => :up})
34 Timer.any_instance.expects(:schedule).with(c, 0)
39 def test_attach_should_regsiter_for_event_condition
40 c = Conditions::FakeEventCondition.new
42 m = Metric.new(@watch, {true => :up})
51 def test_detach_should_remove_condition_metric_association
52 c = Conditions::FakePollCondition.new
54 m = Metric.new(@watch, {true => :up})
59 assert_nil Hub.directory[c]
62 def test_detach_should_unschedule_poll_conditions
63 c = Conditions::FakePollCondition.new
65 m = Metric.new(@watch, {true => :up})
68 Timer.any_instance.expects(:unschedule).with(c)
69 c.expects(:deregister).never
74 def test_detach_should_deregister_event_conditions
75 c = Conditions::FakeEventCondition.new
77 m = Metric.new(@watch, {true => :up})
80 c.expects(:deregister).once
87 def test_trigger_should_handle_poll_for_poll_condition
88 c = Conditions::FakePollCondition.new
90 Hub.expects(:handle_poll).with(c, @watch.phase)
92 Hub.trigger(c, @watch.phase)
95 def test_trigger_should_handle_event_for_event_condition
96 c = Conditions::FakeEventCondition.new
97 Hub.expects(:handle_event).with(c)
99 Hub.trigger(c, @watch.phase)
104 def test_handle_poll_no_change_should_reschedule
105 c = Conditions::FakePollCondition.new
109 m = Metric.new(@watch, {true => :up})
112 c.expects(:test).returns(false)
113 Timer.any_instance.expects(:schedule)
116 t = Hub.handle_poll(c, @watch.phase)
121 def test_handle_poll_change_should_move
122 c = Conditions::FakePollCondition.new
126 m = Metric.new(@watch, {true => :up})
129 c.expects(:test).returns(true)
130 @watch.expects(:move).with(:up)
133 t = Hub.handle_poll(c, @watch.phase)
138 def test_handle_poll_should_not_abort_on_exception
139 c = Conditions::FakePollCondition.new
143 m = Metric.new(@watch, {true => :up})
146 c.expects(:test).raises(StandardError.new)
148 assert_nothing_raised do
150 t = Hub.handle_poll(c, @watch.phase)
156 def test_handle_poll_should_use_overridden_transition
157 c = Conditions::Tries.new
160 c.transition = :start
163 m = Metric.new(@watch, {true => :up})
166 @watch.expects(:move).with(:start)
169 t = Hub.handle_poll(c, @watch.phase)
174 def test_handle_poll_should_notify_if_triggering
175 c = Conditions::FakePollCondition.new
180 m = Metric.new(@watch, {true => :up})
183 c.expects(:test).returns(true)
187 t = Hub.handle_poll(c, @watch.phase)
192 def test_handle_poll_should_not_notify_if_not_triggering
193 c = Conditions::FakePollCondition.new
198 m = Metric.new(@watch, {true => :up})
201 c.expects(:test).returns(false)
202 Hub.expects(:notify).never
205 t = Hub.handle_poll(c, @watch.phase)
212 def test_handle_event_should_move
213 c = Conditions::FakeEventCondition.new
216 m = Metric.new(@watch, {true => :up})
219 @watch.expects(:move).with(:up)
222 t = Hub.handle_event(c)
227 def test_handle_event_should_notify_if_triggering
228 c = Conditions::FakeEventCondition.new
232 m = Metric.new(@watch, {true => :up})
238 t = Hub.handle_event(c)
243 def test_handle_event_should_not_notify_if_no_notify_set
244 c = Conditions::FakeEventCondition.new
247 m = Metric.new(@watch, {true => :up})
250 Hub.expects(:notify).never
253 t = Hub.handle_event(c)