Skip to content

Commit

Permalink
Replace Timecop with Process.clock_gettime in spec
Browse files Browse the repository at this point in the history
This test mocks the internals of Statsd (time sending a message to timing)

Statsd no longer uses Time.now to determine the time, instead
using Process.clock_gettime in order to compute monotonic time.
More details: reinh/statsd#70.

We therefore must update our test to mock the new call and also
stub the constant Process::CLOCK_MONOTONIC.

This is the same dependency on some Statsd internal behaviour, it
just _looks_ more brittle.
Timecop may support this in future: travisjeffery/timecop#220 (comment)
  • Loading branch information
Bill Franklin committed Jan 7, 2021
1 parent 714b03f commit b3a1d30
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions spec/commands/base_command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ def call

class Commands::SlowCommand < Commands::BaseCommand
def call
Timecop.travel Time.zone.now + 1
:foo
end
end
Expand Down Expand Up @@ -52,9 +51,15 @@ def call

describe "timing" do
it "sends a command's duration to statsd" do
stub_const("Process::CLOCK_MONOTONIC", 6)
allow(Process).to receive(:clock_gettime)
allow(Process).to receive(:clock_gettime)
.with(Process::CLOCK_MONOTONIC, :nanosecond)
.and_return(1472243573027000, 1472246723347000)

expect(PublishingAPI.service(:statsd)).to receive(:timing) do |name, time, sample_rate|
expect(name).to eq "Commands.SlowCommand"
expect(time).to be_within(10).of(1000)
expect(time).to eq(3150)
expect(sample_rate).to eq 1
end

Expand Down

0 comments on commit b3a1d30

Please sign in to comment.