Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for mocking Process.clock_gettime via Timecop #220

Closed
chrisbutcher opened this issue Nov 1, 2017 · 6 comments
Closed

Support for mocking Process.clock_gettime via Timecop #220

chrisbutcher opened this issue Nov 1, 2017 · 6 comments
Milestone

Comments

@chrisbutcher
Copy link

chrisbutcher commented Nov 1, 2017

It would be great to be able to freeze and travel time returned by this method.

https://ruby-doc.org/core-2.4.2/Process.html#method-c-clock_gettime

More on monotonic clocks: https://www.softwariness.com/articles/monotonic-clocks-windows-and-posix/

@evan-stripe
Copy link

(It'd be helpful if this worked on CLOCK_REALTIME as well as CLOCK_MONOTONIC - we're moving some particularly performance-sensitive code to use Process.clock_gettime(Process::CLOCK_REALTIME) instead of Time.now to avoid an object allocation)

@joshuacronemeyer
Copy link
Collaborator

i've got to spend some time with @wishdev's PR, play around with it and do some tests but think this will be a nice new feature to have in our next release.

bilbof pushed a commit to alphagov/publishing-api that referenced this issue Jan 7, 2021
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)
bilbof pushed a commit to alphagov/publishing-api that referenced this issue Jan 7, 2021
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)
bilbof pushed a commit to alphagov/publishing-api that referenced this issue Jan 7, 2021
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)
bilbof pushed a commit to alphagov/publishing-api that referenced this issue Jan 7, 2021
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.

I initially updated our test to mock the new call and also
stub the constant Process::CLOCK_MONOTONIC.
But, this is the same dependency on some Statsd internal behaviour, it
just _looked_ more brittle.
Timecop may support this in future: travisjeffery/timecop#220 (comment)

Instead, sleeping for 1 second doesn't also test rely on testing the
internals of the Statsd gem.
fredericfran-gds pushed a commit to alphagov/publishing-api that referenced this issue Apr 29, 2021
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)
@adfoster-r7
Copy link

adfoster-r7 commented May 12, 2022

This functionality would be great to add, and it looks like there's a PR to deliver it as well - let me know if there's anything we can do to help here 👍

@joshuacronemeyer
Copy link
Collaborator

thanks @adfoster-r7 this slipped off my radar. The PR never had passing tests. I haven't played with it either to see how it works.

@ecerulm
Copy link

ecerulm commented Jan 4, 2024

This was closed as completed but still it's not implemented as of timecop 0.9.8 . I checked with

  def test_process_clock_gettime_fail
    time1 = Time.now
    Timecop.freeze(time1) do
      time2 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
      sleep 1
      time3 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
      assert_equal time2, time3
    end
  end

The time obtained via Process.clock_gettime is not influenced by timecop.

@joshuacronemeyer , is this issue closed because it's decided that timecop will never support Process.clock_gettime?

Can I suggest to mention README.md in the homepage directly that Process.clock_gettime is out of the scope?

It provides a unified method to mock Time.now, Date.today, and DateTime.now in a single call (NOTE: Process.clock_gettime is not mocked by timecop)

@joshuacronemeyer
Copy link
Collaborator

@ecerulm thanks for checking in on this. I just closed this because it was old and nobody was working on it. Not because we're not open to this functionality.

I don't know much about it. I see we had this PR that was here before I started working on the project: #258

I think we just need somebody to champion a properly tested, working PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants