Fix simple mode lifecycle gap
[god.git] / test / test_watch.rb
blob545a00666ce24c0c5380dec9803254dde3f52f8d
1 require File.dirname(__FILE__) + '/helper'
3 class TestWatch < Test::Unit::TestCase
4   def setup
5     @watch = Watch.new(nil)
6   end
7   
8   # new
9   
10   def test_new_should_have_no_behaviors
11     assert_equal [], @watch.behaviors
12   end
13   
14   def test_new_should_have_no_metrics
15     Watch::VALID_STATES.each do |state|
16       assert_equal [], @watch.metrics[state]
17     end
18   end
19     
20   def test_new_should_have_standard_attributes
21     assert_nothing_raised do
22       @watch.name = 'foo'
23       @watch.start = 'start'
24       @watch.stop = 'stop'
25       @watch.restart = 'restart'
26       @watch.interval = 30
27       @watch.grace = 5
28     end
29   end
30   
31   def test_new_should_have_nil_state
32     assert_equal nil, @watch.state
33   end
34   
35   # mutex
36   
37   def test_mutex_should_return_the_same_mutex_each_time
38     assert_equal @watch.mutex, @watch.mutex
39   end
40   
41   # behavior
42   
43   def test_behavior_should_record_behavior
44     beh = nil
45     @watch.behavior(:fake_behavior) { |b| beh = b }
46     assert_equal 1, @watch.behaviors.size
47     assert_equal beh, @watch.behaviors.first
48   end
49   
50   # transition
51   
52   def test_transition_should_abort_on_invalid_start_state
53     assert_raise AbortCalledError do
54       @watch.transition(:foo, :bar)
55     end
56   end
57   
58   def test_transition_should_accept_all_valid_start_states
59     assert_nothing_raised do
60       Watch::VALID_STATES.each do |state|
61         @watch.transition(state, :bar) { }
62       end
63     end
64   end
65   
66   def test_transition_should_create_and_record_a_metric_for_the_given_start_state
67     @watch.transition(:init, :start) { }
68     assert_equal 1, @watch.metrics[:init].size
69   end
70   
71   # start_if
72   
73   def test_start_if_should_place_a_metric_on_up_state
74     @watch.start_if { }
75     assert_equal 1, @watch.metrics[:up].size
76   end
77   
78   # restart_if
79   
80   def test_restart_if_should_place_a_metric_on_up_state
81     @watch.restart_if { }
82     assert_equal 1, @watch.metrics[:up].size
83   end
84   
85   # canonical_hash_form
86   
87   def test_canonical_hash_form_should_convert_symbol_to_hash
88     assert_equal({true => :foo}, @watch.canonical_hash_form(:foo))
89   end
90   
91   def test_canonical_hash_form_should_convert_hash_to_hash
92     assert_equal({true => :foo}, @watch.canonical_hash_form(true => :foo))
93   end
94 end