From 105910429d5873dce677ef32eef5f705e0625d86 Mon Sep 17 00:00:00 2001 From: Cheah Chu Yeow Date: Sun, 13 Apr 2008 14:40:30 +0800 Subject: Introduce ActiveResource::Base.timeout. This allows a timeout to be set on the internal Net::HTTP instance ARes uses (default is 60 seconds). Setting a low timeout allows ARes clients to fail-fast in the event of a unresponsive/crashed server, rather than cause cascading failures in your application. Signed-off-by: Michael Koziarski --- activeresource/test/base_test.rb | 66 ++++++++++++++++++++++++++++++++++ activeresource/test/connection_test.rb | 5 +++ 2 files changed, 71 insertions(+) (limited to 'activeresource/test') diff --git a/activeresource/test/base_test.rb b/activeresource/test/base_test.rb index 9caca803b7..9e2f6c1831 100644 --- a/activeresource/test/base_test.rb +++ b/activeresource/test/base_test.rb @@ -88,6 +88,12 @@ class BaseTest < Test::Unit::TestCase assert_equal('test123', Forum.connection.password) end + def test_should_accept_setting_timeout + Forum.timeout = 5 + assert_equal(5, Forum.timeout) + assert_equal(5, Forum.connection.timeout) + end + def test_user_variable_can_be_reset actor = Class.new(ActiveResource::Base) actor.site = 'http://cinema' @@ -108,6 +114,16 @@ class BaseTest < Test::Unit::TestCase assert_nil actor.connection.password end + def test_timeout_variable_can_be_reset + actor = Class.new(ActiveResource::Base) + actor.site = 'http://cinema' + assert_nil actor.timeout + actor.timeout = 5 + actor.timeout = nil + assert_nil actor.timeout + assert_nil actor.connection.timeout + end + def test_credentials_from_site_are_decoded actor = Class.new(ActiveResource::Base) actor.site = 'http://my%40email.com:%31%32%33@cinema' @@ -232,6 +248,40 @@ class BaseTest < Test::Unit::TestCase assert_equal fruit.password, apple.password, 'subclass did not adopt changes from parent class' end + def test_timeout_reader_uses_superclass_timeout_until_written + # Superclass is Object so returns nil. + assert_nil ActiveResource::Base.timeout + assert_nil Class.new(ActiveResource::Base).timeout + Person.timeout = 5 + + # Subclass uses superclass timeout. + actor = Class.new(Person) + assert_equal Person.timeout, actor.timeout + + # Changing subclass timeout doesn't change superclass timeout. + actor.timeout = 10 + assert_not_equal Person.timeout, actor.timeout + + # Changing superclass timeout doesn't overwrite subclass timeout. + Person.timeout = 15 + assert_not_equal Person.timeout, actor.timeout + + # Changing superclass timeout after subclassing changes subclass timeout. + jester = Class.new(actor) + actor.timeout = 20 + assert_equal actor.timeout, jester.timeout + + # Subclasses are always equal to superclass timeout when not overridden. + fruit = Class.new(ActiveResource::Base) + apple = Class.new(fruit) + + fruit.timeout = 25 + assert_equal fruit.timeout, apple.timeout, 'subclass did not adopt changes from parent class' + + fruit.timeout = 30 + assert_equal fruit.timeout, apple.timeout, 'subclass did not adopt changes from parent class' + end + def test_updating_baseclass_site_object_wipes_descendent_cached_connection_objects # Subclasses are always equal to superclass site when not overridden fruit = Class.new(ActiveResource::Base) @@ -279,6 +329,22 @@ class BaseTest < Test::Unit::TestCase assert_not_equal(first_connection, second_connection, 'Connection should be re-created') end + def test_updating_baseclass_timeout_wipes_descendent_cached_connection_objects + # Subclasses are always equal to superclass timeout when not overridden + fruit = Class.new(ActiveResource::Base) + apple = Class.new(fruit) + fruit.site = 'http://market' + + fruit.timeout = 5 + assert_equal fruit.connection.timeout, apple.connection.timeout + first_connection = apple.connection.object_id + + fruit.timeout = 10 + assert_equal fruit.connection.timeout, apple.connection.timeout + second_connection = apple.connection.object_id + assert_not_equal(first_connection, second_connection, 'Connection should be re-created') + end + def test_collection_name assert_equal "people", Person.collection_name end diff --git a/activeresource/test/connection_test.rb b/activeresource/test/connection_test.rb index 38fdbd3b2f..6c907614e7 100644 --- a/activeresource/test/connection_test.rb +++ b/activeresource/test/connection_test.rb @@ -101,6 +101,11 @@ class ConnectionTest < Test::Unit::TestCase assert_equal site, @conn.site end + def test_timeout_accessor + @conn.timeout = 5 + assert_equal 5, @conn.timeout + end + def test_get matz = @conn.get("/people/1.xml") assert_equal "Matz", matz["name"] -- cgit v1.2.3