diff --git a/lib/ferrum/browser/process.rb b/lib/ferrum/browser/process.rb index 7ca321ef..079f0439 100644 --- a/lib/ferrum/browser/process.rb +++ b/lib/ferrum/browser/process.rb @@ -16,7 +16,6 @@ module Ferrum class Browser class Process KILL_TIMEOUT = 2 - WAIT_KILLED = 0.05 attr_reader :host, :port, :ws_url, :pid, :command, :default_user_agent, :browser_version, :protocol_version, @@ -38,7 +37,7 @@ def self.process_killer(pid) ::Process.kill("USR1", pid) start = Utils::ElapsedTime.monotonic_time while ::Process.wait(pid, ::Process::WNOHANG).nil? - sleep(WAIT_KILLED) + ::Thread.pass next unless Utils::ElapsedTime.timeout?(start, KILL_TIMEOUT) ::Process.kill("KILL", pid) diff --git a/lib/ferrum/network.rb b/lib/ferrum/network.rb index 6d8c2c31..b88ec0fa 100644 --- a/lib/ferrum/network.rb +++ b/lib/ferrum/network.rb @@ -46,9 +46,6 @@ def initialize(page) # @param [Integer] connections # how many connections are allowed for network to be idling, # - # @param [Float] duration - # Sleep for given amount of time and check again. - # # @param [Float] timeout # During what time we try to check idle. # @@ -59,13 +56,13 @@ def initialize(page) # browser.at_xpath("//a[text() = 'No UI changes button']").click # browser.network.wait_for_idle # => false # - def wait_for_idle(connections: 0, duration: 0.05, timeout: @page.timeout) + def wait_for_idle(connections: 0, timeout: @page.timeout) start = Utils::ElapsedTime.monotonic_time until idle?(connections) return false if Utils::ElapsedTime.timeout?(start, timeout) - sleep(duration) + ::Thread.pass end true diff --git a/lib/ferrum/utils/attempt.rb b/lib/ferrum/utils/attempt.rb index 334b7cc6..e969ad47 100644 --- a/lib/ferrum/utils/attempt.rb +++ b/lib/ferrum/utils/attempt.rb @@ -13,6 +13,8 @@ def with_retry(errors:, max:, wait:) attempts += 1 sleep(wait) + + ::Thread.pass retry end end diff --git a/lib/ferrum/utils/event.rb b/lib/ferrum/utils/event.rb index cbd5bcea..d65fc35b 100644 --- a/lib/ferrum/utils/event.rb +++ b/lib/ferrum/utils/event.rb @@ -14,6 +14,20 @@ def reset @iteration end end + + def wait(timeout) + synchronize do + unless @set + iteration = @iteration + ns_wait_until(timeout) do + iteration < @iteration || @set + ::Thread.pass + end + else + true + end + end + end end end end