Module: Capybara::Simulated::SleepHook

Defined in:
lib/capybara/simulated/driver.rb

Instance Method Summary collapse

Instance Method Details

#sleep(seconds = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/capybara/simulated/driver.rb', line 22

def sleep(seconds = nil)
  return super if seconds.nil?
  n = super
  if seconds.to_f >= Capybara::Simulated::USER_SLEEP_THRESHOLD_S
    # Broadcast to every Driver constructed on the current
    # thread. Background threads (`MessageBus::TimerThread`,
    # etc.) sleep too, but their Drivers — if any — were
    # registered under a different thread, so they skip; the
    # filter is load-bearing because rusty_racer / quickjs.rb
    # VMs aren't thread-safe. Idle Drivers no-op
    # (`tick_real_time` short-circuits when `@timers_active`
    # is false), so the broadcast is cheap.
    ms = (seconds.to_f * 1000).to_i
    Capybara::Simulated::Driver.each_live_on_thread(Thread.current) {|d|
      d.browser.advance_virtual_clock_ms(ms)
    }
  end
  n
end