From 964cd0b049e67ca10bd37b67c4b14ccd37064511 Mon Sep 17 00:00:00 2001 From: Azul Date: Fri, 16 Sep 2016 14:32:55 +0200 Subject: deal with network issues in nicknym source This is a first step. In case the suspected nicknym server cannot be reached we will now move on and try other sources. It's robably not what we want in the long run. In order to know wether no key exists or we just failed to connect to some servers a different http response code would be nice if network errors occured. This simplifies testing such scenarios in the unit test and makes the remote tests skip on network failure. --- test/unit/nicknym/source_test.rb | 58 ++++++++++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 17 deletions(-) (limited to 'test/unit') diff --git a/test/unit/nicknym/source_test.rb b/test/unit/nicknym/source_test.rb index 76337d4..cddb7c2 100644 --- a/test/unit/nicknym/source_test.rb +++ b/test/unit/nicknym/source_test.rb @@ -8,31 +8,55 @@ class NicknymSourceTest < Minitest::Test assert source end - def test_available_for_domain - adapter.expect :get, [200, '{"services": ["mx"]}'], - ['https://leap_powered.tld/provider.json'] - assert source.available_for?('leap_powered.tld') - adapter.verify + def test_available_for_domain_with_service_mx + assert available_on?(200, '{"services": ["mx"]}') end - def test_not_available_for_domain - adapter.expect :get, [404, nil], - ['https://remote.tld/provider.json'] - assert !source.available_for?('remote.tld') - adapter.verify + def test_no_provider_json_means_no_nicknym + refute available_on?(404, 'blablabla') + end + + def test_invalid_provider_json_means_no_nicknym + refute available_on?(200, 'blablabla') end - def test_successful_query - adapter.expect :get, [200, 'dummy body'], - ['https://nicknym.leap_powered.tld:6425', - {query: {address: email_stub.to_s}}] + def test_failing_network_means_no_nicknym + failing_network + refute source.available_for?('remote.tld') + end + + def test_proxy_successful_query + assert proxies_query_response?(200, 'dummy body') + end + + def test_proxy_query_not_found + assert proxies_query_response?(404, 'dummy body') + end + + protected + + def proxies_query_response?(status = 0, body = nil) + adapter.expect :get, [status, body], + ['https://nicknym.leap_powered.tld:6425', query: {address: email_stub.to_s}] response = source.query(email_stub) - assert_equal 200, response.status - assert_equal 'dummy body', response.content + assert_equal status, response.status + assert_equal body, response.content adapter.verify end - protected + def available_on?(status = 0, body = nil) + adapter.expect :get, [status, body], + ['https://remote.tld/provider.json'] + available = source.available_for?('remote.tld') + adapter.verify + return available + end + + def failing_network + def adapter.get(*args) + raise HTTP::ConnectionError + end + end def source Nickserver::Nicknym::Source.new(adapter) -- cgit v1.2.3 From a1c7d68b05f142322a190b450971d27c076310a9 Mon Sep 17 00:00:00 2001 From: Azul Date: Mon, 19 Sep 2016 09:51:38 +0200 Subject: refactor: separate handler chain from dispatcher Handler Chain is of handlers that respond to call. Invoking handle(*args) on the chain will call the handlers with the given args until one of them returns a result that is truethy (i.e. not false or nil). Extracted from the dispatcher so we can also handle exceptions there in the future. (So that if one of the network connections to the request_handlers fails we can continue while still tracking the failed exception.) --- test/unit/handler_chain_test.rb | 45 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 test/unit/handler_chain_test.rb (limited to 'test/unit') diff --git a/test/unit/handler_chain_test.rb b/test/unit/handler_chain_test.rb new file mode 100644 index 0000000..067f11e --- /dev/null +++ b/test/unit/handler_chain_test.rb @@ -0,0 +1,45 @@ +require 'test_helper' +require 'nickserver/handler_chain' + +class HandlerChainTest < Minitest::Test + + def test_initialization + assert chain + end + + def test_noop + assert_nil chain.handle + end + + def test_triggering_handlers + handler_mock.expect :call, nil, [:a, :b] + chain handler_mock + chain.handle :a, :b + handler_mock.verify + end + + def test_returns_handler_result + chain handler_with_nil, handler_with_result + assert_equal :result, chain.handle + end + + + protected + + def chain(*handlers) + @chain ||= Nickserver::HandlerChain.new(*handlers) + end + + def handler_mock + @handler ||= Minitest::Mock.new + end + + def handler_with_nil + Proc.new {} + end + + def handler_with_result + Proc.new { :result } + end + +end -- cgit v1.2.3 From dd71240fe4f4f968b9b687917cb6d7ad5812ba48 Mon Sep 17 00:00:00 2001 From: Azul Date: Tue, 20 Sep 2016 09:14:43 +0200 Subject: rescue and track exceptions in handler chain --- test/unit/handler_chain_test.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'test/unit') diff --git a/test/unit/handler_chain_test.rb b/test/unit/handler_chain_test.rb index 067f11e..fae0418 100644 --- a/test/unit/handler_chain_test.rb +++ b/test/unit/handler_chain_test.rb @@ -23,6 +23,26 @@ class HandlerChainTest < Minitest::Test assert_equal :result, chain.handle end + def test_raise_exception + chain handler_raising, handler_with_result + assert_raises RuntimeError do + chain.handle + end + end + + def test_continue_on_exception + chain handler_raising, handler_with_result + chain.continue_on(RuntimeError) + assert_equal :result, chain.handle + assert_equal [RuntimeError], chain.rescued_exceptions.map(&:class) + end + + def test_continue_on_exception_with_nil + chain handler_raising, handler_with_nil + chain.continue_on(RuntimeError) + assert_nil chain.handle + assert_equal [RuntimeError], chain.rescued_exceptions.map(&:class) + end protected @@ -42,4 +62,7 @@ class HandlerChainTest < Minitest::Test Proc.new { :result } end + def handler_raising(exception = RuntimeError) + Proc.new { raise exception } + end end -- cgit v1.2.3 From 1202e3b03c3bb88cd3a63dae3866167564f1d25d Mon Sep 17 00:00:00 2001 From: Azul Date: Thu, 22 Sep 2016 11:31:01 +0200 Subject: hand on connection errors to dispatcher so it can handle it --- test/unit/nicknym/source_test.rb | 11 ----------- 1 file changed, 11 deletions(-) (limited to 'test/unit') diff --git a/test/unit/nicknym/source_test.rb b/test/unit/nicknym/source_test.rb index cddb7c2..f8c9b60 100644 --- a/test/unit/nicknym/source_test.rb +++ b/test/unit/nicknym/source_test.rb @@ -20,11 +20,6 @@ class NicknymSourceTest < Minitest::Test refute available_on?(200, 'blablabla') end - def test_failing_network_means_no_nicknym - failing_network - refute source.available_for?('remote.tld') - end - def test_proxy_successful_query assert proxies_query_response?(200, 'dummy body') end @@ -52,12 +47,6 @@ class NicknymSourceTest < Minitest::Test return available end - def failing_network - def adapter.get(*args) - raise HTTP::ConnectionError - end - end - def source Nickserver::Nicknym::Source.new(adapter) end -- cgit v1.2.3