Skip to content

Commit

Permalink
Raise a more specific error if the websocket connection dies
Browse files Browse the repository at this point in the history
  • Loading branch information
yjukaku committed Dec 2, 2021
1 parent ca86be4 commit b20f5bf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
2 changes: 2 additions & 0 deletions lib/capybara/apparition/driver/chrome_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ def process_messages
puts "Unexpected CDPError: #{e.message}"
end
retry
rescue WebSocketClientError => e
puts "Unexpected websocket connection exception: #{e}: #{e.message}: #{e.backtrace}"
rescue StandardError => e
puts "Unexpected inner loop exception: #{e}: #{e.message}: #{e.backtrace}"
retry
Expand Down
23 changes: 20 additions & 3 deletions lib/capybara/apparition/driver/web_socket_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

module Capybara::Apparition
class WebSocketClient
class WebSocketClientError < StandardError; end;

attr_reader :driver, :messages, :status

def initialize(url)
Expand Down Expand Up @@ -55,27 +57,42 @@ def start_driver
end

def parse_input
@driver.parse(@socket.read)
data = @socket.read
raise WebSocketClientError.new("Received empty data message from websocket, indicating a timeout or dead connection.") if data.nil? || data.empty?
@driver.parse(data)
rescue EOFError, Errno::ECONNRESET => e
raise WebSocketClientError.new("Received a low-level error when reading from websocket: #{e.inspect}")
end
end

require 'socket'

class Socket
CONNECT_TIMEOUT = ENV.fetch("APPARITION_SOCKET_CONNECT_TIMEOUT", 5).to_i
READ_TIMEOUT = ENV.fetch("APPARITION_SOCKET_READ_TIMEOUT", 5).to_i

attr_reader :url

def initialize(url)
@url = url
uri = URI.parse(url)
@io = TCPSocket.new(uri.host, uri.port)
@io = ::Socket.tcp(uri.host, uri.port, connect_timeout: CONNECT_TIMEOUT)
end

def write(data)
@io.print data
end

def read
@io.readpartial(1024)
retries = 0
begin
@io.read_nonblock(1024)
rescue IO::WaitReadable
retries += 1
IO.select([@io], nil, nil, READ_TIMEOUT)
retry if retries <= 1
nil
end
end
end
end

0 comments on commit b20f5bf

Please sign in to comment.