1 require File.dirname(__FILE__) + '/helper'
3 class TestWatch < Test::Unit::TestCase
7 @watch.start = lambda { }
8 @watch.stop = lambda { }
13 def test_new_should_have_no_behaviors
14 assert_equal [], @watch.behaviors
17 def test_new_should_have_no_metrics
18 Watch::VALID_STATES.each do |state|
19 assert_equal [], @watch.metrics[state]
23 def test_new_should_have_standard_attributes
24 assert_nothing_raised do
26 @watch.start = 'start'
28 @watch.restart = 'restart'
34 def test_new_should_have_nil_state
35 assert_equal nil, @watch.state
40 def test_mutex_should_return_the_same_mutex_each_time
41 assert_equal @watch.mutex, @watch.mutex
47 God::Process.any_instance.expects(:valid?)
53 def test_behavior_should_record_behavior
55 @watch.behavior(:fake_behavior) { |b| beh = b }
56 assert_equal 1, @watch.behaviors.size
57 assert_equal beh, @watch.behaviors.first
60 def test_invalid_behavior_should_abort
62 @watch.behavior(:invalid)
68 def test_transition_should_abort_on_invalid_start_state
70 @watch.transition(:foo, :bar)
74 def test_transition_should_accept_all_valid_start_states
75 assert_nothing_raised do
76 Watch::VALID_STATES.each do |state|
77 @watch.transition(state, :bar) { }
82 def test_transition_should_create_and_record_a_metric_for_the_given_start_state
83 @watch.transition(:init, :start) { }
84 assert_equal 1, @watch.metrics[:init].size
89 def test_start_if_should_place_a_metric_on_up_state
91 assert_equal 1, @watch.metrics[:up].size
96 def test_restart_if_should_place_a_metric_on_up_state
98 assert_equal 1, @watch.metrics[:up].size
103 def test_monitor_should_move_to_init_if_available
104 @watch.instance_eval do
105 transition(:init, :up) { }
107 @watch.expects(:move).with(:init)
111 def test_monitor_should_move_to_up_if_no_init_available
112 @watch.expects(:move).with(:up)
118 def test_unmonitor_should_move_to_nil
119 @watch.expects(:move).with(nil)
125 def test_move_should_not_clean_up_if_from_state_is_nil
128 @watch.instance_eval do
129 transition(:init, :up) do |on|
131 on.condition(:process_running) do |c|
138 metric.expects(:disable).never
140 no_stdout { @watch.move(:init) }
143 def test_move_should_clean_up_from_state_if_not_nil
146 @watch.instance_eval do
147 transition(:init, :up) do |on|
149 on.condition(:process_running) do |c|
156 no_stdout { @watch.move(:init) }
158 metric.expects(:disable)
160 no_stdout { @watch.move(:up) }
163 def test_move_should_call_action
164 @watch.expects(:action).with(:start)
166 no_stdout { @watch.move(:start) }
169 def test_move_should_move_to_up_state_if_no_start_or_restart_metric
170 [:start, :restart].each do |state|
171 @watch.expects(:action)
172 no_stdout { @watch.move(state) }
173 assert_equal :up, @watch.state
177 def test_move_should_enable_destination_metric
180 @watch.instance_eval do
181 transition(:init, :up) do |on|
183 on.condition(:process_running) do |c|
190 metric.expects(:enable)
192 no_stdout { @watch.move(:init) }
197 def test_action_should_pass_start_and_stop_actions_to_call_action
198 c = Conditions::FakePollCondition.new
199 [:start, :stop].each do |cmd|
200 @watch.expects(:call_action).with(c, cmd)
201 no_stdout { @watch.action(cmd, c) }
205 def test_action_should_do_stop_then_start_if_no_restart_command
206 c = Conditions::FakePollCondition.new
207 @watch.expects(:call_action).with(c, :stop)
208 @watch.expects(:call_action).with(c, :start)
209 no_stdout { @watch.action(:restart, c) }
212 def test_action_should_restart_to_call_action_if_present
213 @watch.restart = lambda { }
214 c = Conditions::FakePollCondition.new
215 @watch.expects(:call_action).with(c, :restart)
216 no_stdout { @watch.action(:restart, c) }
222 c = Conditions::FakePollCondition.new
223 God::Process.any_instance.expects(:call_action).with(:start)
224 @watch.call_action(c, :start)
227 # canonical_hash_form
229 def test_canonical_hash_form_should_convert_symbol_to_hash
230 assert_equal({true => :foo}, @watch.canonical_hash_form(:foo))
233 def test_canonical_hash_form_should_convert_hash_to_hash
234 assert_equal({true => :foo}, @watch.canonical_hash_form(true => :foo))