Class: FiberAudit::Runtime::Clock

Inherits:
Object
  • Object
show all
Defined in:
lib/fiber_audit/runtime/clock.rb

Constant Summary collapse

MAX_NANOSECONDS =
9_223_372_036_854_775_807
WALL_SOURCE =
-> { Time.now }.freeze
MONOTONIC_SOURCE =
lambda {
  Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond)
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(wall: WALL_SOURCE, monotonic: MONOTONIC_SOURCE) ⇒ Clock

Returns a new instance of Clock.



14
15
16
17
18
19
20
21
# File 'lib/fiber_audit/runtime/clock.rb', line 14

def initialize(wall: WALL_SOURCE, monotonic: MONOTONIC_SOURCE)
  raise RuntimeContractError, 'wall clock source must respond to call' unless wall.respond_to?(:call)
  raise RuntimeContractError, 'monotonic clock source must respond to call' unless monotonic.respond_to?(:call)

  @wall = wall
  @monotonic = monotonic
  freeze
end

Instance Method Details

#monotonic_nsObject

Raises:



27
28
29
30
31
32
# File 'lib/fiber_audit/runtime/clock.rb', line 27

def monotonic_ns
  value = Validation.integer(@monotonic.call, 'monotonic clock result')
  raise RuntimeSafetyError, 'monotonic clock result exceeds signed 64-bit range' if value > MAX_NANOSECONDS

  value
end

#wall_timeObject



23
24
25
# File 'lib/fiber_audit/runtime/clock.rb', line 23

def wall_time
  Validation.utc_time(@wall.call, 'wall clock result')
end