From e1b3425846832294c48b032c52f99f678bd61a1c Mon Sep 17 00:00:00 2001 From: Tom Preston-Werner Date: Fri, 28 Sep 2007 22:27:37 -0700 Subject: [PATCH] fix tests to work with reasonable at_exit, restrict notifications to be sent only on triggers --- lib/god.rb | 2 +- lib/god/hub.rb | 20 +++++++++++------- test/helper.rb | 14 ++----------- test/test_god.rb | 6 +++--- test/test_hub.rb | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 81 insertions(+), 24 deletions(-) diff --git a/lib/god.rb b/lib/god.rb index a92b9cc..4b736fd 100644 --- a/lib/god.rb +++ b/lib/god.rb @@ -398,7 +398,7 @@ module God # start the timer system Timer.get - + # start monitoring any watches set to autostart self.watches.values.each { |w| w.monitor if w.autostart? } diff --git a/lib/god/hub.rb b/lib/god/hub.rb index 04b3439..feebc8c 100644 --- a/lib/god/hub.rb +++ b/lib/god/hub.rb @@ -58,7 +58,7 @@ module God messages = self.log(watch, metric, condition, result) # notify - if condition.notify + if condition.notify && self.trigger?(metric, result) self.notify(condition, messages.last) end @@ -107,19 +107,19 @@ module God Thread.new do begin metric = self.directory[condition] - + unless metric.nil? watch = metric.watch - + watch.mutex.synchronize do # log messages = self.log(watch, metric, condition, true) - + # notify - if condition.notify + if condition.notify && self.trigger?(metric, true) self.notify(condition, messages.last) end - + # get the destination dest = if condition.transition @@ -129,7 +129,7 @@ module God # regular metric.destination && metric.destination[true] end - + if dest watch.move(dest) end @@ -146,9 +146,13 @@ module God # helpers + def self.trigger?(metric, result) + (metric.destination && metric.destination.keys.size == 2) || result == true + end + def self.log(watch, metric, condition, result) status = - if (metric.destination && metric.destination.keys.size == 2) || result == true + if self.trigger?(metric, result) "[trigger]" else "[ok]" diff --git a/test/helper.rb b/test/helper.rb index df40e83..0f4f43c 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -68,14 +68,6 @@ module God end end - class << self - alias :at_exit_orig :at_exit - end - - def self.at_exit - # disable at_exit - end - def self.reset self.watches = nil self.groups = nil @@ -113,7 +105,7 @@ module Kernel def abort(text) raise SystemExit end - def exit!(code) + def exit(code) raise SystemExit end end @@ -121,9 +113,7 @@ end module Test::Unit::Assertions def assert_abort assert_raise SystemExit do - no_stderr do - yield - end + yield end end end diff --git a/test/test_god.rb b/test/test_god.rb index b002824..cf023a3 100644 --- a/test/test_god.rb +++ b/test/test_god.rb @@ -324,7 +324,7 @@ class TestGod < Test::Unit::TestCase # terminate def test_terminate_should_exit - God.expects(:exit!).with(0) + God.expects(:exit) God.terminate end @@ -417,7 +417,7 @@ class TestGod < Test::Unit::TestCase w = nil no_stdout do - w = God.running_load(code, '/foo/bar.god') + w, e = *God.running_load(code, '/foo/bar.god') end assert_equal 1, w.size assert_equal 'foo', w.first.name @@ -497,7 +497,7 @@ class TestGod < Test::Unit::TestCase def test_at_exit_should_call_start God.expects(:start).once - God.at_exit_orig + God.at_exit end end diff --git a/test/test_hub.rb b/test/test_hub.rb index ec8f7e5..1b4f127 100644 --- a/test/test_hub.rb +++ b/test/test_hub.rb @@ -157,6 +157,40 @@ class TestHub < Test::Unit::TestCase end end + def test_handle_poll_should_notify_if_triggering + c = Conditions::FakePollCondition.new + c.interval = 10 + c.notify = 'tom' + + m = Metric.new(@watch, {true => :up}) + Hub.attach(c, m) + + c.expects(:test).returns(true) + Hub.expects(:notify) + + no_stdout do + t = Hub.handle_poll(c) + t.join + end + end + + def test_handle_poll_should_not_notify_if_not_triggering + c = Conditions::FakePollCondition.new + c.interval = 10 + c.notify = 'tom' + + m = Metric.new(@watch, {true => :up}) + Hub.attach(c, m) + + c.expects(:test).returns(false) + Hub.expects(:notify).never + + no_stdout do + t = Hub.handle_poll(c) + t.join + end + end + # handle_event def test_handle_event_should_move @@ -172,4 +206,33 @@ class TestHub < Test::Unit::TestCase t.join end end + + def test_handle_event_should_notify_if_triggering + c = Conditions::FakeEventCondition.new + c.notify = 'tom' + + m = Metric.new(@watch, {true => :up}) + Hub.attach(c, m) + + Hub.expects(:notify) + + no_stdout do + t = Hub.handle_event(c) + t.join + end + end + + def test_handle_event_should_not_notify_if_no_notify_set + c = Conditions::FakeEventCondition.new + + m = Metric.new(@watch, {true => :up}) + Hub.attach(c, m) + + Hub.expects(:notify).never + + no_stdout do + t = Hub.handle_event(c) + t.join + end + end end \ No newline at end of file -- 2.11.4.GIT