free_mutant/spec/unit/mutant/clock_monotonic_spec.rb
2018-11-25 21:43:35 +00:00

52 lines
1 KiB
Ruby

# frozen_string_literal: true
RSpec.describe Mutant::Timer do
let(:events) { [] }
let(:times) { [1.0, 2.0] }
before do
allow(Process).to receive(:clock_gettime) do |argument|
expect(argument).to be(Process::CLOCK_MONOTONIC)
events << :clock_gettime
times.fetch(events.count(:clock_gettime).pred)
end
end
describe '.elapsed' do
def apply
described_class.elapsed { events << :yield }
end
it 'executes events in expected sequence' do
expect { apply }
.to change(events, :to_a)
.from([])
.to(%i[clock_gettime yield clock_gettime])
end
it 'returns elapsed time' do
expect(apply).to be(1.0)
end
end
describe '.now' do
def apply
described_class.now
end
it 'returns current monotonic time' do
expect(apply).to be(1.0)
expect(apply).to be(2.0)
end
it 'calls expected system API' do
expect { apply }
.to change(events, :to_a)
.from([])
.to(%i[clock_gettime])
end
end
end