Skip to content

Commit

Permalink
Fix close behaviour of SslConnection
Browse files Browse the repository at this point in the history
  • Loading branch information
iconara committed May 24, 2014
1 parent 303063c commit f59c02b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
12 changes: 11 additions & 1 deletion lib/ione/io/ssl_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def initialize(host, port, io, unblocker, ssl_context=nil, socket_impl=OpenSSL::
@ssl_context = ssl_context
@raw_io = io
@connected_promise = Promise.new
on_closed(&method(:cleanup_on_close))
end

def connect
Expand All @@ -26,7 +27,7 @@ def connect
@connected_promise.future
rescue OpenSSL::SSL::SSLError => e
unless e.message.include?(WOULD_BLOCK_MESSAGE)
@connected_promise.fail(e)
close(e)
end
@connected_promise.future
end
Expand Down Expand Up @@ -64,6 +65,15 @@ def read
private

WOULD_BLOCK_MESSAGE = 'would block'.freeze

def cleanup_on_close(cause)
if cause && !cause.is_a?(IoError)
cause = ConnectionError.new(cause.message)
end
unless @connected_promise.future.completed?
@connected_promise.fail(cause)
end
end
end
end
end
10 changes: 8 additions & 2 deletions spec/ione/io/ssl_connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ module Io
ssl_socket.should have_received(:connect_nonblock)
end

it 'does nothing when the socket raises' do
it 'does nothing when the socket raises a "would block" error' do
ssl_socket.stub(:connect_nonblock).and_raise(OpenSSL::SSL::SSLError.new('would block'))
expect { handler.connect }.to_not raise_error
end
Expand All @@ -89,7 +89,13 @@ module Io

it 'fails when #connect_nonblock raises an error that does not include the words "would block"' do
ssl_socket.stub(:connect_nonblock).and_raise(OpenSSL::SSL::SSLError.new('general bork'))
expect { handler.connect.value }.to raise_error(OpenSSL::SSL::SSLError)
expect { handler.connect.value }.to raise_error(Ione::Io::ConnectionError)
end

it 'is closed when #connect_nonblock raises something that is not a "would block" error' do
ssl_socket.stub(:connect_nonblock).and_raise(OpenSSL::SSL::SSLError.new('general bork'))
handler.connect
handler.should be_closed
end
end

Expand Down

0 comments on commit f59c02b

Please sign in to comment.