Skip to content

Commit

Permalink
Fix the observer helper timer not to raise errors when on time out
Browse files Browse the repository at this point in the history
  • Loading branch information
KUOKA Yusuke committed Nov 12, 2013
1 parent f4b2968 commit a2061e8
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 4 deletions.
11 changes: 11 additions & 0 deletions lib/observed/logging.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'observed/configurable'

module Observed
module Logging
include Observed::Configurable

# !@attribute [r] logger
# @return [Logger]
attribute :logger
end
end
1 change: 1 addition & 0 deletions lib/observed/observer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'observed/configurable'
require 'observed/pluggable'
require 'observed/logging'

module Observed

Expand Down
10 changes: 6 additions & 4 deletions lib/observed/observer_helpers/timer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ def time(options={}, &block)
elapsed_time = after - before
r[:elapsed_time] = elapsed_time
r
rescue Timeout::Error => e1
{ status: :error, error: {message: "#{e2.message}\n#{e2.backtrace}"}, timed_out: true }
rescue => e2
{ status: :error, error: {message: "#{e2.message}\n#{e2.backtrace}"} }
rescue Timeout::Error => e
logger.debug "Handled the error but logging it just for your info: #{e.message}\n#{e.backtrace.join("\n")}" if self.is_a? Logging
{ status: :error, error: {message: 'Timed out.'}, timed_out: true }
rescue => e
logger.error "Handled the error: #{e.message}\n#{e.backtrace.join("\n")}" if self.is_a? Logging
{ status: :error, error: {message: e.message} }
end
end

Expand Down
161 changes: 161 additions & 0 deletions spec/observer_helpers/timer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
require 'spec_helper'

require 'observed/observer'
require 'observed/observer_helpers/timer'
require 'observed/configurable'

describe describe Observed::ObserverHelpers::Timer do

before {
subject.configure system: sys, logger: logger
}

let(:sys) {
sys = stub('system')
sys.stubs(:now).returns(before).then.returns(after)
sys
}

let(:before) { Time.now }

let(:after) { Time.now + 1 }

let(:logger) {
mock('logger')
}

context 'when its logging enabled' do

subject {
klass = Class.new(Observed::Observer) do
include Observed::ObserverHelpers::Timer
include Observed::Configurable
include Observed::Logging
end
klass.new
}

describe 'its `time` method' do
context 'when missing :timeout_in_seconds parameter' do
it 'fails' do

expect {
subject.time({}) do
fail 'This block should not be called'
end
}.to raise_error(/The key `:timeout_in_seconds` must be exist in the options/)

end
end

context 'with correct parameters' do
context 'given the block which does not time out' do
it 'returns the result containing its status, value, elapsed time' do

expect(
subject.time({timeout_in_seconds: 1.0}) do
'test_value'
end
).to eq(status: :success, result: 'test_value', elapsed_time: after - before)

end

context 'given the block which does time out' do
it 'returns the result containing its status, flag, message while logging the error' do

logger.expects(:debug).once
expect(
subject.time(timeout_in_seconds: 1.0) do
raise Timeout::Error
end
).to eq(status: :error, timed_out: true, error: {message: 'Timed out.'})

end
end

context 'given the block which fails' do
it 'returns the result containing its status, message while logging the error' do

logger.expects(:error).once
expect(
subject.time(timeout_in_seconds: 1.0) do
raise RuntimeError, 'The error message'
end
).to eq(status: :error, error: {message: 'The error message'})

end
end
end
end

end
end

context 'when its logging disabled' do

subject {
klass = Class.new(Observed::Observer) do
include Observed::ObserverHelpers::Timer
include Observed::Configurable
end
klass.new
}

describe 'its `time` method' do
context 'with correct parameters' do
context 'given the block which does time out' do
it 'returns the result containing its status, flag, message while not logging the error' do

logger.expects(:debug).never
expect(
subject.time(timeout_in_seconds: 1.0) do
raise Timeout::Error
end
).to eq(status: :error, error: {message: 'Timed out.'}, timed_out: true)
end
end

context 'given the block which fails' do
it 'returns the result containing its status, message while not logging the error' do

logger.expects(:error).never
expect(
subject.time(timeout_in_seconds: 1.0) do
raise StandardError, 'The error message'
end
).to eq(status: :error, error: {message: 'The error message'})

end
end
end
end

describe 'its `time_and_report` method' do
context 'when :tag parameter is not given' do
it 'fails' do

expect {
subject.time_and_report(timeout_in_seconds: 1.0) do
'the result'
end
}.to raise_error

end
end

context 'when :tag parameter is given' do
it 'reports the result of `time`' do

sys.expects(:report).with('test.success', {status: :success, result: 'the result', elapsed_time: after - before })

expect {
subject.time_and_report(tag: 'test', timeout_in_seconds: 1.0) do
'the result'
end
}.to_not raise_error

end
end
end
end
end

0 comments on commit a2061e8

Please sign in to comment.