1 require File.dirname(__FILE__) + '/helper'
3 class TestHub < Test::Unit::TestCase
5 God::Socket.stubs(:new).returns(true)
14 @watch = God.watches['foo']
19 def test_attach_should_store_condition_metric_association
20 c = Conditions::FakePollCondition.new
21 m = Metric.new(@watch, :foo)
24 assert_equal m, Hub.directory[c]
27 def test_attach_should_schedule_for_poll_condition
28 c = Conditions::FakePollCondition.new
29 m = Metric.new(@watch, :foo)
31 Timer.any_instance.expects(:schedule).with(c, 0)
36 def test_attach_should_regsiter_for_event_condition
37 c = Conditions::FakeEventCondition.new
38 m = Metric.new(@watch, :foo)
47 def test_detach_should_remove_condition_metric_association
48 c = Conditions::FakePollCondition.new
49 m = Metric.new(@watch, :foo)
54 assert_nil Hub.directory[c]
57 def test_detach_should_unschedule_poll_conditions
58 c = Conditions::FakePollCondition.new
59 m = Metric.new(@watch, :foo)
62 Timer.any_instance.expects(:unschedule).with(c)
63 c.expects(:deregister).never
68 def test_detach_should_deregister_event_conditions
69 c = Conditions::FakeEventCondition.new
70 m = Metric.new(@watch, :foo)
73 c.expects(:deregister).once
80 def test_trigger_should_handle_poll_for_poll_condition
81 c = Conditions::FakePollCondition.new
82 Hub.expects(:handle_poll).with(c)
87 def test_trigger_should_handle_event_for_event_condition
88 c = Conditions::FakeEventCondition.new
89 Hub.expects(:handle_event).with(c)
96 def test_handle_poll_no_change_should_reschedule
97 c = Conditions::FakePollCondition.new
100 m = Metric.new(@watch, {true => :up})
103 c.expects(:test).returns(false)
104 Timer.any_instance.expects(:schedule)
107 t = Hub.handle_poll(c)
112 def test_handle_poll_change_should_move
113 c = Conditions::FakePollCondition.new
116 m = Metric.new(@watch, {true => :up})
119 c.expects(:test).returns(true)
120 @watch.expects(:move).with(:up)
123 t = Hub.handle_poll(c)
128 def test_handle_poll_should_not_abort_on_exception
129 c = Conditions::FakePollCondition.new
132 m = Metric.new(@watch, {true => :up})
135 c.expects(:test).raises(StandardError.new)
137 assert_nothing_raised do
139 t = Hub.handle_poll(c)
145 def test_handle_poll_should_use_overridden_transition
146 c = Conditions::Tries.new
148 c.transition = :start
151 m = Metric.new(@watch, {true => :up})
154 @watch.expects(:move).with(:start)
157 t = Hub.handle_poll(c)
162 def test_handle_poll_should_notify_if_triggering
163 c = Conditions::FakePollCondition.new
167 m = Metric.new(@watch, {true => :up})
170 c.expects(:test).returns(true)
174 t = Hub.handle_poll(c)
179 def test_handle_poll_should_not_notify_if_not_triggering
180 c = Conditions::FakePollCondition.new
184 m = Metric.new(@watch, {true => :up})
187 c.expects(:test).returns(false)
188 Hub.expects(:notify).never
191 t = Hub.handle_poll(c)
198 def test_handle_event_should_move
199 c = Conditions::FakeEventCondition.new
201 m = Metric.new(@watch, {true => :up})
204 @watch.expects(:move).with(:up)
207 t = Hub.handle_event(c)
212 def test_handle_event_should_notify_if_triggering
213 c = Conditions::FakeEventCondition.new
216 m = Metric.new(@watch, {true => :up})
222 t = Hub.handle_event(c)
227 def test_handle_event_should_not_notify_if_no_notify_set
228 c = Conditions::FakeEventCondition.new
230 m = Metric.new(@watch, {true => :up})
233 Hub.expects(:notify).never
236 t = Hub.handle_event(c)