Skip to content

Commit

Permalink
Notify output handlers when an expectation failed.
Browse files Browse the repository at this point in the history
  • Loading branch information
jellybob committed Jul 24, 2011
1 parent 9e3264f commit 60ad0da
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
7 changes: 6 additions & 1 deletion lib/critical/metric_base.rb
Expand Up @@ -360,7 +360,12 @@ def track(what)
def expect(status_on_failure=nil, &block) def expect(status_on_failure=nil, &block)
status_on_failure ||= :critical status_on_failure ||= :critical
begin begin
update_status(status_on_failure) unless instance_eval(&block) if instance_eval(&block)
report.expectation_succeeded
else
update_status(status_on_failure)
report.expectation_failed
end
rescue Exception => e rescue Exception => e
update_status(status_on_failure) update_status(status_on_failure)
end end
Expand Down
4 changes: 2 additions & 2 deletions lib/critical/output_handler/base.rb
Expand Up @@ -43,10 +43,10 @@ def collection_failed(error, message=nil, trace=nil)
def processing_failed(error, message=nil, trace=nil) def processing_failed(error, message=nil, trace=nil)
end end


def expectation_failed(error, message=nil, trace=nil) def expectation_failed(error=nil, message=nil, trace=nil)
end end


def expectation_succeeded(error, message=nil, trace=nil) def expectation_succeeded(error=nil, message=nil, trace=nil)
end end


def collection_completed(*args) def collection_completed(*args)
Expand Down
20 changes: 18 additions & 2 deletions spec/unit/metric_base_spec.rb
Expand Up @@ -250,7 +250,7 @@ def index
@metric_spec.processing_block = Proc.new { 'here I come' } @metric_spec.processing_block = Proc.new { 'here I come' }
@metric.processing_block.call.should == 'here I come' @metric.processing_block.call.should == 'here I come'
end end

it "passes itself into the handler block if a block with arity 1 is given" do it "passes itself into the handler block if a block with arity 1 is given" do
@metric_class.collects { :some_data } @metric_class.collects { :some_data }
@metric_spec.processing_block = Proc.new { |m| m.snitch= :yoshitoshi} @metric_spec.processing_block = Proc.new { |m| m.snitch= :yoshitoshi}
Expand Down Expand Up @@ -391,14 +391,30 @@ def @metric.string_to_echo
@metric.result @metric.result
end end


describe "reporting on expectations" do

it "notifies the output handler when an expectation fails" do
@output_handler.should_receive(:expectation_failed)
@metric_spec.processing_block = Proc.new { expect {false} }
@metric.collect(@output_handler, @trending_handler)
end

it "notifies the output handler when an expectation succeeds" do
@output_handler.should_receive(:expectation_succeeded)
@metric_spec.processing_block = Proc.new { expect {true} }
@metric.collect(@output_handler, @trending_handler)
end

end

describe "classifying the state of the monitored property" do describe "classifying the state of the monitored property" do


it "reports expectation failures as critical" do it "reports expectation failures as critical" do
@metric_spec.processing_block = Proc.new { expect {false} } @metric_spec.processing_block = Proc.new { expect {false} }
@metric.collect(@output_handler, @trending_handler) @metric.collect(@output_handler, @trending_handler)
@metric.metric_status.should == :critical @metric.metric_status.should == :critical
end end

it "reports expectation failures as warning when given :warning as the argument" do it "reports expectation failures as warning when given :warning as the argument" do
@metric_spec.processing_block = Proc.new { expect(:warning) {false} } @metric_spec.processing_block = Proc.new { expect(:warning) {false} }
@metric.collect(@output_handler, @trending_handler) @metric.collect(@output_handler, @trending_handler)
Expand Down

0 comments on commit 60ad0da

Please sign in to comment.