Skip to content

Commit

Permalink
Revert removal of JRuby-specific SocketPoller#listening?
Browse files Browse the repository at this point in the history
This reverts commit 287dbde.
  • Loading branch information
p0deje committed Apr 18, 2019
1 parent 1cde7d1 commit e1c181d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 23 deletions.
49 changes: 30 additions & 19 deletions rb/lib/selenium/webdriver/common/socket_poller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,37 @@ def closed?
arr << Errno::EALREADY if Platform.wsl?
}.freeze

def listening?
addr = Socket.getaddrinfo(@host, @port, Socket::AF_INET, Socket::SOCK_STREAM)
sock = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
sockaddr = Socket.pack_sockaddr_in(@port, addr[0][3])

begin
sock.connect_nonblock sockaddr
rescue Errno::EINPROGRESS
retry if socket_writable?(sock) && conn_completed?(sock)
raise Errno::ECONNREFUSED
rescue *CONNECTED_ERRORS
# yay!
if Platform.jruby?
# we use a plain TCPSocket here since JRuby has issues select()ing on a connecting socket
# see http://jira.codehaus.org/browse/JRUBY-5165
def listening?
TCPSocket.new(@host, @port).close
true
rescue *NOT_CONNECTED_ERRORS
false
end
else
def listening?
addr = Socket.getaddrinfo(@host, @port, Socket::AF_INET, Socket::SOCK_STREAM)
sock = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
sockaddr = Socket.pack_sockaddr_in(@port, addr[0][3])

begin
sock.connect_nonblock sockaddr
rescue Errno::EINPROGRESS
retry if socket_writable?(sock) && conn_completed?(sock)
raise Errno::ECONNREFUSED
rescue *CONNECTED_ERRORS
# yay!
end

sock.close
true
rescue *NOT_CONNECTED_ERRORS
sock.close if sock
WebDriver.logger.debug("polling for socket on #{[@host, @port].inspect}")
false
end

sock.close
true
rescue *NOT_CONNECTED_ERRORS
sock&.close
WebDriver.logger.debug("polling for socket on #{[@host, @port].inspect}")
false
end

def socket_writable?(sock)
Expand Down
19 changes: 15 additions & 4 deletions rb/spec/unit/selenium/webdriver/socket_poller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,21 @@ module WebDriver
let(:socket) { instance_double Socket, close: true }

def setup_connect(*states)
allow(Socket).to receive(:new).and_return socket
states.each do |state|
expect(socket).to receive(:connect_nonblock)
.and_raise(state ? Errno::EISCONN.new('connection in progress') : Errno::ECONNREFUSED.new('connection refused'))
# TODO(jari): find a cleaner way to solve the platform-specific collaborators
if Platform.jruby?
states.each do |state|
if state
expect(TCPSocket).to receive(:new).and_return socket
else
expect(TCPSocket).to receive(:new).and_raise Errno::ECONNREFUSED
end
end
else
allow(Socket).to receive(:new).and_return socket
states.each do |state|
expect(socket).to receive(:connect_nonblock)
.and_raise(state ? Errno::EISCONN.new('connection in progress') : Errno::ECONNREFUSED.new('connection refused'))
end
end
end

Expand Down

0 comments on commit e1c181d

Please sign in to comment.