1 require File.dirname(__FILE__) + '/helper'
3 class TestWatch < Test::Unit::TestCase
8 @watch.start = lambda { }
9 @watch.stop = lambda { }
15 def test_new_should_have_no_behaviors
16 assert_equal [], @watch.behaviors
19 def test_new_should_have_no_metrics
20 Watch::VALID_STATES.each do |state|
21 assert_equal [], @watch.metrics[state]
25 def test_new_should_have_standard_attributes
26 assert_nothing_raised do
28 @watch.start = 'start'
30 @watch.restart = 'restart'
36 def test_new_should_have_unmonitored_state
37 assert_equal :unmonitored, @watch.state
42 def test_mutex_should_return_the_same_mutex_each_time
43 assert_equal @watch.mutex, @watch.mutex
49 God::Process.any_instance.expects(:valid?)
55 def test_behavior_should_record_behavior
57 @watch.behavior(:fake_behavior) { |b| beh = b }
58 assert_equal 1, @watch.behaviors.size
59 assert_equal beh, @watch.behaviors.first
62 def test_invalid_behavior_should_abort
64 @watch.behavior(:invalid)
70 def test_transition_should_abort_on_invalid_start_state
72 @watch.transition(:foo, :bar)
76 def test_transition_should_accept_all_valid_start_states
77 assert_nothing_raised do
78 Watch::VALID_STATES.each do |state|
79 @watch.transition(state, :bar) { }
84 def test_transition_should_create_and_record_a_metric_for_the_given_start_state
85 @watch.transition(:init, :start) { }
86 assert_equal 1, @watch.metrics[:init].size
91 def test_lifecycle_should_create_and_record_a_metric_for_nil_start_state
93 assert_equal 1, @watch.metrics[nil].size
98 def test_start_if_should_place_a_metric_on_up_state
100 assert_equal 1, @watch.metrics[:up].size
105 def test_restart_if_should_place_a_metric_on_up_state
106 @watch.restart_if { }
107 assert_equal 1, @watch.metrics[:up].size
112 def test_monitor_should_move_to_init_if_available
113 @watch.instance_eval do
114 transition(:init, :up) { }
116 @watch.expects(:move).with(:init)
120 def test_monitor_should_move_to_up_if_no_init_available
121 @watch.expects(:move).with(:up)
127 def test_unmonitor_should_move_to_nil
128 @watch.expects(:move).with(:unmonitored)
134 def test_move_should_not_clean_up_if_from_state_is_nil
137 @watch.instance_eval do
138 transition(:init, :up) do |on|
140 on.condition(:process_running) do |c|
147 metric.expects(:disable).never
149 no_stdout { @watch.move(:init) }
152 def test_move_should_clean_up_from_state_if_not_nil
155 @watch.instance_eval do
156 transition(:init, :up) do |on|
158 on.condition(:process_running) do |c|
165 no_stdout { @watch.move(:init) }
167 metric.expects(:disable)
169 no_stdout { @watch.move(:up) }
172 def test_move_should_call_action
173 @watch.expects(:action).with(:start)
175 no_stdout { @watch.move(:start) }
178 def test_move_should_move_to_up_state_if_no_start_or_restart_metric
179 [:start, :restart].each do |state|
180 @watch.expects(:action)
181 no_stdout { @watch.move(state) }
182 assert_equal :up, @watch.state
186 def test_move_should_enable_destination_metric
189 @watch.instance_eval do
190 transition(:init, :up) do |on|
192 on.condition(:process_running) do |c|
199 metric.expects(:enable)
201 no_stdout { @watch.move(:init) }
206 def test_action_should_pass_start_and_stop_actions_to_call_action
207 c = Conditions::FakePollCondition.new
208 [:start, :stop].each do |cmd|
209 @watch.expects(:call_action).with(c, cmd)
210 @watch.action(cmd, c)
214 def test_action_should_do_stop_then_start_if_no_restart_command
215 c = Conditions::FakePollCondition.new
216 @watch.expects(:call_action).with(c, :stop)
217 @watch.expects(:call_action).with(c, :start)
218 @watch.action(:restart, c)
221 def test_action_should_restart_to_call_action_if_present
222 @watch.restart = lambda { }
223 c = Conditions::FakePollCondition.new
224 @watch.expects(:call_action).with(c, :restart)
225 @watch.action(:restart, c)
231 c = Conditions::FakePollCondition.new
232 God::Process.any_instance.expects(:call_action).with(:start)
233 no_stdout { @watch.call_action(c, :start) }
236 def test_call_action_should_call_before_start_when_behavior_has_that
237 @watch.behavior(:fake_behavior)
238 c = Conditions::FakePollCondition.new
239 God::Process.any_instance.expects(:call_action).with(:start)
240 Behaviors::FakeBehavior.any_instance.expects(:before_start)
241 no_stdout { @watch.call_action(c, :start) }
244 def test_call_action_should_call_after_start_when_behavior_has_that
245 @watch.behavior(:fake_behavior)
246 c = Conditions::FakePollCondition.new
247 God::Process.any_instance.expects(:call_action).with(:start)
248 Behaviors::FakeBehavior.any_instance.expects(:after_start)
249 no_stdout { @watch.call_action(c, :start) }
252 # canonical_hash_form
254 def test_canonical_hash_form_should_convert_symbol_to_hash
255 assert_equal({true => :foo}, @watch.canonical_hash_form(:foo))
258 def test_canonical_hash_form_should_convert_hash_to_hash
259 assert_equal({true => :foo}, @watch.canonical_hash_form(true => :foo))