fix process#alive? to not raise on no such file
[god.git] / test / test_metric.rb
blobb0479b6baeda180fbfec08b21d00dc0fcbd90a96
1 require File.dirname(__FILE__) + '/helper'
3 class TestMetric < Test::Unit::TestCase
4   def setup
5     @metric = Metric.new(stub(:interval => 10), nil)
6   end
7   
8   # watch
9   
10   def test_watch
11     w = stub()
12     m = Metric.new(w, nil)
13     assert_equal w, m.watch
14   end
15   
16   # destination
17   
18   def test_destination
19     d = stub()
20     m = Metric.new(nil, d)
21     assert_equal d, m.destination
22   end
23   
24   # condition
25   
26   def test_condition_should_be_block_optional
27     @metric.condition(:fake_poll_condition)
28     assert_equal 1, @metric.conditions.size
29   end
30   
31   def test_poll_condition_should_inherit_interval_from_watch_if_not_specified
32     @metric.condition(:fake_poll_condition)
33     assert_equal 10, @metric.conditions.first.interval
34   end
35   
36   def test_poll_condition_should_abort_if_no_interval_and_no_watch_interval
37     metric = Metric.new(stub(:name => 'foo', :interval => nil), nil)
38     
39     assert_abort do
40       metric.condition(:fake_poll_condition)
41     end
42   end
43   
44   def test_condition_should_allow_generation_of_subclasses_of_poll_or_event
45     metric = Metric.new(stub(:name => 'foo', :interval => 10), nil)
46     
47     assert_nothing_raised do
48       metric.condition(:fake_poll_condition)
49       metric.condition(:fake_event_condition)
50     end
51   end
52   
53   def test_condition_should_abort_if_not_subclass_of_poll_or_event
54     metric = Metric.new(stub(:name => 'foo', :interval => 10), nil)
55     
56     assert_abort do
57       metric.condition(:fake_condition) { |c| }
58     end
59   end
60   
61   def test_condition_should_abort_on_invalid_condition
62     assert_abort do
63       @metric.condition(:fake_poll_condition) { |c| c.stubs(:valid?).returns(false) }
64     end
65   end
66   
67   def test_condition_should_abort_on_no_such_condition
68     assert_abort do
69       @metric.condition(:invalid) { }
70     end
71   end
72   
73   # enable
74   
75   def test_enable_should_attach_conditions
76     @metric.condition(:fake_poll_condition)
77     Hub.expects(:attach).with(@metric.conditions.first, @metric)
78     @metric.enable
79   end
80   
81   # disable
82   
83   def test_disable_should_detach_conditions
84     @metric.condition(:fake_poll_condition)
85     Hub.expects(:detach).with(@metric.conditions.first)
86     @metric.disable
87   end
88 end