aboutsummaryrefslogtreecommitdiffstats
path: root/activeresource
diff options
context:
space:
mode:
authorCheah Chu Yeow <chuyeow@gmail.com>2008-04-16 18:39:19 +0800
committerMichael Koziarski <michael@koziarski.com>2008-04-22 09:30:56 +1200
commitcf32baf915442ffe153ec0e4d8148f147776c30a (patch)
tree9405205f0ab6ddfceb49043206aa034f33a53cc1 /activeresource
parent105910429d5873dce677ef32eef5f705e0625d86 (diff)
downloadrails-cf32baf915442ffe153ec0e4d8148f147776c30a.tar.gz
rails-cf32baf915442ffe153ec0e4d8148f147776c30a.tar.bz2
rails-cf32baf915442ffe153ec0e4d8148f147776c30a.zip
Rescue from Timeout::Error in ActiveResource::Connection.
Signed-off-by: Michael Koziarski <michael@koziarski.com>
Diffstat (limited to 'activeresource')
-rw-r--r--activeresource/lib/active_resource/connection.rb10
-rw-r--r--activeresource/test/abstract_unit.rb14
-rw-r--r--activeresource/test/connection_test.rb9
3 files changed, 32 insertions, 1 deletions
diff --git a/activeresource/lib/active_resource/connection.rb b/activeresource/lib/active_resource/connection.rb
index 3b61009f43..98b3f87167 100644
--- a/activeresource/lib/active_resource/connection.rb
+++ b/activeresource/lib/active_resource/connection.rb
@@ -18,6 +18,14 @@ module ActiveResource
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
@@ -134,6 +142,8 @@ module ActiveResource
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.
diff --git a/activeresource/test/abstract_unit.rb b/activeresource/test/abstract_unit.rb
index db1e0b9535..615a6d9222 100644
--- a/activeresource/test/abstract_unit.rb
+++ b/activeresource/test/abstract_unit.rb
@@ -7,4 +7,16 @@ require 'active_resource/http_mock'
$:.unshift "#{File.dirname(__FILE__)}/../test"
require 'setter_trap'
-ActiveResource::Base.logger = Logger.new("#{File.dirname(__FILE__)}/debug.log") \ No newline at end of file
+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 \ No newline at end of file
diff --git a/activeresource/test/connection_test.rb b/activeresource/test/connection_test.rb
index 6c907614e7..8e43e451ff 100644
--- a/activeresource/test/connection_test.rb
+++ b/activeresource/test/connection_test.rb
@@ -168,6 +168,15 @@ class ConnectionTest < Test::Unit::TestCase
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