Skip to content

Commit b62c806

Browse files
committed
[rb] allow setting socket timeout and interval from http client
1 parent 0606401 commit b62c806

File tree

8 files changed

+41
-20
lines changed

8 files changed

+41
-20
lines changed

rb/lib/selenium/webdriver/bidi.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ class BiDi
3131
autoload :InterceptedAuth, 'selenium/webdriver/bidi/network/intercepted_auth'
3232
autoload :InterceptedItem, 'selenium/webdriver/bidi/network/intercepted_item'
3333

34-
def initialize(url:)
35-
@ws = WebSocketConnection.new(url: url)
34+
def initialize(url:, http:)
35+
@ws = WebSocketConnection.new(url: url, http: http)
3636
end
3737

3838
def close

rb/lib/selenium/webdriver/common/websocket_connection.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,23 @@
2121

2222
module Selenium
2323
module WebDriver
24+
#
25+
# WebSocketConnection manages lifecycle of sockets for Devtools and BiDi implementations.
26+
# @api private
27+
#
28+
2429
class WebSocketConnection
2530
CONNECTION_ERRORS = [
2631
Errno::ECONNRESET, # connection is aborted (browser process was killed)
2732
Errno::EPIPE # broken pipe (browser process was killed)
2833
].freeze
2934

30-
RESPONSE_WAIT_TIMEOUT = 30
31-
RESPONSE_WAIT_INTERVAL = 0.1
32-
3335
MAX_LOG_MESSAGE_SIZE = 9999
3436

35-
def initialize(url:)
37+
def initialize(url:, http: nil)
3638
@callback_threads = ThreadGroup.new
39+
@socket_timeout = http&.socket_timeout || 30
40+
@socket_interval = http&.socket_interval || 0.1
3741

3842
@session_id = nil
3943
@url = url
@@ -147,7 +151,7 @@ def callback_thread(params)
147151
end
148152

149153
def wait
150-
@wait ||= Wait.new(timeout: RESPONSE_WAIT_TIMEOUT, interval: RESPONSE_WAIT_INTERVAL)
154+
@wait ||= Wait.new(timeout: @socket_timeout, interval: @socket_interval)
151155
end
152156

153157
def socket

rb/lib/selenium/webdriver/remote/bidi_bridge.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class BiDiBridge < Bridge
2626
def create_session(capabilities)
2727
super
2828
socket_url = @capabilities[:web_socket_url]
29-
@bidi = Selenium::WebDriver::BiDi.new(url: socket_url)
29+
@bidi = Selenium::WebDriver::BiDi.new(url: socket_url, http: @http)
3030
end
3131

3232
def get(url)

rb/lib/selenium/webdriver/remote/http/default.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,21 @@ module Selenium
2222
module WebDriver
2323
module Remote
2424
module Http
25-
# @api private
2625
class Default < Common
2726
attr_writer :proxy
2827

29-
attr_accessor :open_timeout, :read_timeout
28+
attr_accessor :open_timeout, :read_timeout, :socket_timeout, :socket_interval
3029

3130
# Initializes object.
3231
# Warning: Setting {#open_timeout} to non-nil values will cause a separate thread to spawn.
3332
# Debuggers that freeze the process will not be able to evaluate any operations if that happens.
3433
# @param [Numeric] open_timeout - Open timeout to apply to HTTP client.
3534
# @param [Numeric] read_timeout - Read timeout (seconds) to apply to HTTP client.
36-
def initialize(open_timeout: nil, read_timeout: nil)
35+
def initialize(open_timeout: nil, read_timeout: nil, socket_timeout: 30, socket_interval: 0.1)
3736
@open_timeout = open_timeout
3837
@read_timeout = read_timeout
38+
@socket_timeout = socket_timeout
39+
@socket_interval = socket_interval
3940
super()
4041
end
4142

rb/sig/lib/selenium/webdriver/common/websocket_connection.rbs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ module Selenium
77

88
@session_id: untyped
99

10+
@socket_interval: float
11+
12+
@socket_timeout: int
13+
1014
@url: untyped
1115

1216
@socket_thread: untyped

rb/sig/lib/selenium/webdriver/remote/http/default.rbs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ module Selenium
1818

1919
attr_accessor read_timeout: untyped
2020

21+
attr_accessor socket_timeout: int
22+
23+
attr_accessor socket_interval: float
24+
2125
def initialize: (?open_timeout: untyped?, ?read_timeout: untyped?) -> void
2226

2327
def close: () -> untyped

rb/spec/integration/selenium/webdriver/spec_support/test_environment.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def initialize
3131
WebDriver.logger.ignore(:logger_info)
3232
SeleniumManager.bin_path = root.join('bazel-bin/rb/bin').to_s if File.exist?(root.join('bazel-bin/rb/bin'))
3333

34+
ENV['WEBDRIVER_BIDI'] = 'true'
3435
@driver = ENV.fetch('WD_SPEC_DRIVER', 'chrome').tr('-', '_').to_sym
3536
@driver_instance = nil
3637
@remote_server = nil
@@ -168,14 +169,17 @@ def root
168169
@root ||= Pathname.new('../../../../../../../').realpath(__FILE__)
169170
end
170171

171-
def create_driver!(listener: nil, **, &block)
172+
def create_driver!(listener: nil, **opts, &block)
172173
check_for_previous_error
173174

175+
socket_timeout = opts.delete(:socket_timeout)
176+
http_client = WebDriver::Remote::Http::Default.new(socket_timeout: socket_timeout) if socket_timeout
177+
174178
method = :"#{driver}_driver"
175179
instance = if private_methods.include?(method)
176-
send(method, listener: listener, options: build_options(**))
180+
send(method, listener: listener, http_client: http_client, options: build_options(**opts))
177181
else
178-
WebDriver::Driver.for(driver, listener: listener, options: build_options(**))
182+
WebDriver::Driver.for(driver, listener: listener, options: build_options(**opts))
179183
end
180184
@create_driver_error_count -= 1 unless @create_driver_error_count.zero?
181185
if block

rb/spec/unit/selenium/webdriver/remote/http/default_spec.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,21 @@ module Http
3636

3737
expect(http.open_timeout).to eq 60
3838
expect(http.read_timeout).to eq 60
39+
expect(http.socket_timeout).to eq 30
40+
expect(http.socket_interval).to eq 0.1
3941
end
4042

4143
describe '#initialize' do
42-
let(:client) { described_class.new(read_timeout: 22, open_timeout: 23) }
43-
44-
it 'accepts read timeout options' do
44+
it 'constructs values' do
45+
client = described_class.new(read_timeout: 22,
46+
open_timeout: 23,
47+
socket_timeout: 4,
48+
socket_interval: 0.5)
4549
expect(client.open_timeout).to eq 23
46-
end
47-
48-
it 'accepts open timeout options' do
4950
expect(client.read_timeout).to eq 22
51+
expect(client.socket_timeout).to eq 4
52+
expect(client.socket_interval).to eq 0.5
53+
5054
end
5155
end
5256

0 commit comments

Comments
 (0)