Skip to content

Commit

Permalink
Improve SocketPoller unit tests to actually use real sockets
Browse files Browse the repository at this point in the history
  • Loading branch information
p0deje committed Apr 25, 2019
1 parent e0dd6a8 commit 74d1a91
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 32 deletions.
4 changes: 2 additions & 2 deletions rb/lib/selenium/webdriver/common/socket_poller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ def closed?
}.freeze

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
# we use a plain TCPSocket here since JRuby has issues closing socket
# see https://github.com/jruby/jruby/issues/5709
def listening?
TCPSocket.new(@host, @port).close
true
Expand Down
47 changes: 17 additions & 30 deletions rb/spec/unit/selenium/webdriver/socket_poller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,62 +22,49 @@
module Selenium
module WebDriver
describe SocketPoller do
let(:poller) { Selenium::WebDriver::SocketPoller.new('localhost', 1234, 5, 0.05) }
let(:socket) { instance_double Socket, close: true }

def setup_connect(*states)
# 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
around do |example|
server_thread = Thread.new do
server = TCPServer.open(9250)
Thread.current.thread_variable_set(:server, server)
loop { server.accept.close }
end
server_thread.report_on_exception = false
example.call
ensure
server_thread.thread_variable_get(:server).close
end

def poller(port)
described_class.new('localhost', port, 5, 0.05)
end

describe '#connected?' do
it 'returns true when the socket is listening' do
setup_connect false, true
expect(poller).to be_connected
expect(poller(9250)).to be_connected
end

it 'returns false if the socket is not listening after the given timeout' do
setup_connect false

start = Time.parse('2010-01-01 00:00:00')
wait = Time.parse('2010-01-01 00:00:04')
stop = Time.parse('2010-01-01 00:00:06')

expect(Process).to receive(:clock_gettime).and_return(start, wait, stop)
expect(poller).not_to be_connected
expect(poller(9251)).not_to be_connected
end
end

describe '#closed?' do
it 'returns true when the socket is closed' do
setup_connect true, true, false

expect(poller).to be_closed
expect(poller(9251)).to be_closed
end

it 'returns false if the socket is still listening after the given timeout' do
setup_connect true

start = Time.parse('2010-01-01 00:00:00').to_f
wait = Time.parse('2010-01-01 00:00:04').to_f
stop = Time.parse('2010-01-01 00:00:06').to_f

expect(Process).to receive(:clock_gettime).and_return(start, wait, stop)
expect(poller).not_to be_closed
expect(poller(9250)).not_to be_closed
end
end
end
Expand Down

0 comments on commit 74d1a91

Please sign in to comment.