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