Skip to content

Commit

Permalink
Add #local_host and #local_port to Connection and SslConnection
Browse files Browse the repository at this point in the history
So that you can figure out which interface and port the connection is using.
  • Loading branch information
iconara committed Apr 29, 2016
1 parent 15e47f1 commit 7c7ea0f
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/ione/io/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Io
# from and writing to the socket.
# @since v1.0.0
class Connection < BaseConnection
attr_reader :connection_timeout
attr_reader :connection_timeout, :local_host, :local_port

# @private
def initialize(host, port, connection_timeout, unblocker, clock, socket_impl=Socket)
Expand Down Expand Up @@ -37,6 +37,7 @@ def connect
end
rescue Errno::EISCONN
@state = CONNECTED_STATE
@local_host, @local_port = @io.local_address.ip_unpack
@connected_promise.fulfill(self)
rescue Errno::EINPROGRESS, Errno::EALREADY
if @clock.now - @connection_started_at > @connection_timeout
Expand Down
3 changes: 3 additions & 0 deletions lib/ione/io/ssl_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ module Ione
module Io
# @private
class SslConnection < BaseConnection
attr_reader :local_host, :local_port

def initialize(host, port, io, unblocker, ssl_context=nil, socket_impl=OpenSSL::SSL::SSLSocket)
super(host, port, unblocker)
@socket_impl = socket_impl
Expand All @@ -23,6 +25,7 @@ def connect
@io = @socket_impl.new(@raw_io)
end
@io.connect_nonblock
@local_host, @local_port = @io.local_address.ip_unpack
@state = CONNECTED_STATE
@connected_promise.fulfill(self)
@connected_promise.future
Expand Down
43 changes: 43 additions & 0 deletions spec/ione/io/connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ module Io
double(:socket)
end

let :local_address do
double(:local_address, ip_unpack: ['1.2.3.4', 65432])
end

before do
socket_impl.stub(:getaddrinfo)
.with('example.com', 55555, nil, Socket::SOCK_STREAM)
Expand All @@ -42,6 +46,7 @@ module Io
before do
socket.stub(:connect_nonblock)
socket.stub(:close)
socket.stub(:local_address).and_return(local_address)
end

it_behaves_like 'a connection' do
Expand Down Expand Up @@ -324,6 +329,44 @@ module Io
end
end
end

describe '#local_host' do
context 'when not yet connected' do
it 'returns nil' do
handler.local_host.should be_nil
end
end

context 'when connected' do
before do
socket.stub(:connect_nonblock).and_raise(Errno::EISCONN)
end

it 'returns the hostname of the interface the socket is using' do
handler.connect
handler.local_host.should eq('1.2.3.4')
end
end
end

describe '#local_port' do
context 'when not yet connected' do
it 'returns nil' do
handler.local_host.should be_nil
end
end

context 'when connected' do
before do
socket.stub(:connect_nonblock).and_raise(Errno::EISCONN)
end

it 'returns the local port the socket is using' do
handler.connect
handler.local_port.should eq(65432)
end
end
end
end
end
end
35 changes: 35 additions & 0 deletions spec/ione/io/ssl_connection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,18 @@ module Io
double(:ssl_context)
end

let :local_addres do
double(:local_addres, ip_unpack: ['1.2.3.4', 65432])
end

before do
socket_impl.stub(:new).with(raw_socket, ssl_context).and_return(ssl_socket)
end

before do
ssl_socket.stub(:connect_nonblock)
ssl_socket.stub(:close)
ssl_socket.stub(:local_address).and_return(local_addres)
end

it_behaves_like 'a connection', skip_read: true do
Expand Down Expand Up @@ -178,6 +183,36 @@ module Io
handler.should be_closed
end
end

describe '#local_host' do
context 'when not yet connected' do
it 'returns nil' do
handler.local_host.should be_nil
end
end

context 'when connected' do
it 'returns the hostname of the interface the socket is using' do
handler.connect
handler.local_host.should eq('1.2.3.4')
end
end
end

describe '#local_port' do
context 'when not yet connected' do
it 'returns nil' do
handler.local_host.should be_nil
end
end

context 'when connected' do
it 'returns the local port the socket is using' do
handler.connect
handler.local_port.should eq(65432)
end
end
end
end
end
end

0 comments on commit 7c7ea0f

Please sign in to comment.