From b3e67d37cee7008e65f201674af00475a214bae8 Mon Sep 17 00:00:00 2001 From: Tatsuya Sato Date: Thu, 27 Aug 2015 11:57:55 +0900 Subject: [PATCH 1/4] Raise Net::LDAP::ConnectionRefusedError when new connection is refused. Now Net::LDAP::Connection.new raises Net::LDAP::Error even if the connection refused. It's hard for some application to reconnect it only when refused. --- lib/net/ldap/connection.rb | 2 +- test/test_ldap_connection.rb | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/net/ldap/connection.rb b/lib/net/ldap/connection.rb index b51bcc10..8e0e8c18 100644 --- a/lib/net/ldap/connection.rb +++ b/lib/net/ldap/connection.rb @@ -14,7 +14,7 @@ def initialize(server) rescue SocketError raise Net::LDAP::Error, "No such address or other socket error." rescue Errno::ECONNREFUSED - raise Net::LDAP::Error, "Server #{server[:host]} refused connection on port #{server[:port]}." + raise Net::LDAP::ConnectionRefusedError, "Server #{server[:host]} refused connection on port #{server[:port]}." rescue Errno::EHOSTUNREACH => error raise Net::LDAP::Error, "Host #{server[:host]} was unreachable (#{error.message})" rescue Errno::ETIMEDOUT diff --git a/test/test_ldap_connection.rb b/test/test_ldap_connection.rb index 96b542ac..5b90ae22 100644 --- a/test/test_ldap_connection.rb +++ b/test/test_ldap_connection.rb @@ -14,6 +14,13 @@ def test_blocked_port end end + def test_connection_refused + flexmock(TCPSocket).should_receive(:new).and_raise(Errno::ECONNREFUSED) + assert_raise Net::LDAP::ConnectionRefusedError do + Net::LDAP::Connection.new(:host => 'test.mocked.com', :port => 636) + end + end + def test_raises_unknown_exceptions error = Class.new(StandardError) flexmock(TCPSocket).should_receive(:new).and_raise(error) From 4f0f4b2efe059c0bd56a2aa5427a9cb5793e64e7 Mon Sep 17 00:00:00 2001 From: Tatsuya Sato Date: Fri, 28 Aug 2015 14:12:36 +0900 Subject: [PATCH 2/4] Raising Net::LDAP::ConnectionRefusedError, shows deprecation warning. --- lib/net/ldap/error.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/net/ldap/error.rb b/lib/net/ldap/error.rb index c9a25f90..1b24b5a3 100644 --- a/lib/net/ldap/error.rb +++ b/lib/net/ldap/error.rb @@ -9,7 +9,21 @@ class Error < StandardError; end class AlreadyOpenedError < Error; end class SocketError < Error; end - class ConnectionRefusedError < Error; end + class ConnectionRefusedError < Error; + def initialize(*args) + warn warning_message + super + end + + def message + warning_message + super + end + + private + def warning_message + "Deprecation warning: Net::LDAP::ConnectionRefused will be deprecated. Use Errno::ECONNREFUSED instead. \n" + end + end class NoOpenSSLError < Error; end class NoStartTLSResultError < Error; end class NoSearchBaseError < Error; end From d20ee69c7e936a152f73dc58a1660286dc266cff Mon Sep 17 00:00:00 2001 From: Tatsuya Sato Date: Fri, 28 Aug 2015 16:02:45 +0900 Subject: [PATCH 3/4] ConnectionRefusedError does not change the original error message --- lib/net/ldap/error.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/net/ldap/error.rb b/lib/net/ldap/error.rb index 1b24b5a3..38b4a4a5 100644 --- a/lib/net/ldap/error.rb +++ b/lib/net/ldap/error.rb @@ -11,17 +11,18 @@ class AlreadyOpenedError < Error; end class SocketError < Error; end class ConnectionRefusedError < Error; def initialize(*args) - warn warning_message + warn_deprecation_message super end def message - warning_message + super + warn_deprecation_message + super end private - def warning_message - "Deprecation warning: Net::LDAP::ConnectionRefused will be deprecated. Use Errno::ECONNREFUSED instead. \n" + def warn_deprecation_message + warn "Deprecation warning: Net::LDAP::ConnectionRefused will be deprecated. Use Errno::ECONNREFUSED instead." end end class NoOpenSSLError < Error; end From b4a3bd2ddc90c0decd664c69a51023820e1548a7 Mon Sep 17 00:00:00 2001 From: Tatsuya Sato Date: Fri, 18 Sep 2015 17:54:07 +0900 Subject: [PATCH 4/4] Capture the result of stderr to check the warning message. --- test/test_ldap_connection.rb | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/test/test_ldap_connection.rb b/test/test_ldap_connection.rb index 5b90ae22..4fa7b22d 100644 --- a/test/test_ldap_connection.rb +++ b/test/test_ldap_connection.rb @@ -1,6 +1,14 @@ require_relative 'test_helper' class TestLDAPConnection < Test::Unit::TestCase + def capture_stderr + stderr, $stderr = $stderr, StringIO.new + yield + $stderr.string + ensure + $stderr = stderr + end + def test_unresponsive_host assert_raise Net::LDAP::Error do Net::LDAP::Connection.new(:host => 'test.mocked.com', :port => 636) @@ -16,9 +24,12 @@ def test_blocked_port def test_connection_refused flexmock(TCPSocket).should_receive(:new).and_raise(Errno::ECONNREFUSED) - assert_raise Net::LDAP::ConnectionRefusedError do - Net::LDAP::Connection.new(:host => 'test.mocked.com', :port => 636) + stderr = capture_stderr do + assert_raise Net::LDAP::ConnectionRefusedError do + Net::LDAP::Connection.new(:host => 'test.mocked.com', :port => 636) + end end + assert_equal("Deprecation warning: Net::LDAP::ConnectionRefused will be deprecated. Use Errno::ECONNREFUSED instead.\n", stderr) end def test_raises_unknown_exceptions