Skip to content

Commit

Permalink
Rescue from Timeout::Error in ActiveResource::Connection.
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Koziarski <michael@koziarski.com>
  • Loading branch information
chuyeow authored and NZKoz committed Apr 21, 2008
1 parent 1059104 commit cf32baf
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
10 changes: 10 additions & 0 deletions activeresource/lib/active_resource/connection.rb
Expand Up @@ -18,6 +18,14 @@ def to_s
end
end

# Raised when a Timeout::Error occurs.
class TimeoutError < ConnectionError
def initialize(message)
@message = message
end
def to_s; @message ;end
end

# 3xx Redirection
class Redirection < ConnectionError # :nodoc:
def to_s; response['Location'] ? "#{super} => #{response['Location']}" : super; end
Expand Down Expand Up @@ -134,6 +142,8 @@ def request(method, path, *arguments)
time = Benchmark.realtime { result = http.send(method, path, *arguments) }
logger.info "--> #{result.code} #{result.message} (#{result.body ? result.body : 0}b %.2fs)" % time if logger
handle_response(result)
rescue Timeout::Error => e
raise TimeoutError.new(e.message)
end

# Handles response and error codes from remote service.
Expand Down
14 changes: 13 additions & 1 deletion activeresource/test/abstract_unit.rb
Expand Up @@ -7,4 +7,16 @@
$:.unshift "#{File.dirname(__FILE__)}/../test"
require 'setter_trap'

ActiveResource::Base.logger = Logger.new("#{File.dirname(__FILE__)}/debug.log")
ActiveResource::Base.logger = Logger.new("#{File.dirname(__FILE__)}/debug.log")

# Wrap tests that use Mocha and skip if unavailable.
def uses_mocha(test_name)
unless Object.const_defined?(:Mocha)
require 'mocha'
require 'stubba'
end
yield
rescue LoadError => load_error
raise unless load_error.message =~ /mocha/i
$stderr.puts "Skipping #{test_name} tests. `gem install mocha` and try again."
end
9 changes: 9 additions & 0 deletions activeresource/test/connection_test.rb
Expand Up @@ -168,6 +168,15 @@ def test_delete_with_header
assert_equal 200, response.code
end

uses_mocha('test_timeout') do
def test_timeout
@http = mock('new Net::HTTP')
@conn.expects(:http).returns(@http)
@http.expects(:get).raises(Timeout::Error, 'execution expired')
assert_raises(ActiveResource::TimeoutError) { @conn.get('/people_timeout.xml') }
end
end

protected
def assert_response_raises(klass, code)
assert_raise(klass, "Expected response code #{code} to raise #{klass}") do
Expand Down

0 comments on commit cf32baf

Please sign in to comment.