Class: TIMEx::Clock::VirtualClock
- Inherits:
-
Object
- Object
- TIMEx::Clock::VirtualClock
- Defined in:
- lib/timex/clock.rb
Overview
Mutable monotonic/wall pair used in tests to advance time without sleeping.
Instance Attribute Summary collapse
-
#monotonic_ns ⇒ Object
Returns the value of attribute monotonic_ns.
-
#wall_ns ⇒ Object
Returns the value of attribute wall_ns.
Instance Method Summary collapse
-
#advance(seconds) ⇒ self
Advances both monotonic and wall by
seconds(no sleep). -
#initialize(monotonic_ns: 0, wall_ns: Process.clock_gettime(Process::CLOCK_REALTIME, :nanosecond)) ⇒ VirtualClock
constructor
A new instance of VirtualClock.
-
#sleep(seconds) ⇒ self
For chaining.
Constructor Details
#initialize(monotonic_ns: 0, wall_ns: Process.clock_gettime(Process::CLOCK_REALTIME, :nanosecond)) ⇒ VirtualClock
Returns a new instance of VirtualClock.
88 89 90 91 |
# File 'lib/timex/clock.rb', line 88 def initialize(monotonic_ns: 0, wall_ns: Process.clock_gettime(Process::CLOCK_REALTIME, :nanosecond)) @monotonic_ns = monotonic_ns @wall_ns = wall_ns end |
Instance Attribute Details
#monotonic_ns ⇒ Object
Returns the value of attribute monotonic_ns.
84 85 86 |
# File 'lib/timex/clock.rb', line 84 def monotonic_ns @monotonic_ns end |
#wall_ns ⇒ Object
Returns the value of attribute wall_ns.
84 85 86 |
# File 'lib/timex/clock.rb', line 84 def wall_ns @wall_ns end |
Instance Method Details
#advance(seconds) ⇒ self
Advances both monotonic and wall by seconds (no sleep).
97 98 99 100 101 102 |
# File 'lib/timex/clock.rb', line 97 def advance(seconds) delta = (seconds * Clock::NS_PER_SECOND).to_i @monotonic_ns += delta @wall_ns += delta self end |
#sleep(seconds) ⇒ self
Returns for chaining.
106 107 108 |
# File 'lib/timex/clock.rb', line 106 def sleep(seconds) advance(seconds) end |