From 9bfb1ea511b2f65bfb3f71ab50f15ff12e2fdb03 Mon Sep 17 00:00:00 2001 From: Alex Watt Date: Thu, 13 Jun 2024 17:28:49 -0400 Subject: [PATCH 1/3] Disable Process#clock_gettime mocking by default --- lib/timecop/time_extensions.rb | 2 +- lib/timecop/timecop.rb | 8 ++++++++ test/timecop_with_process_clock_test.rb | 13 +++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/timecop/time_extensions.rb b/lib/timecop/time_extensions.rb index 3e788b5..0de35ca 100644 --- a/lib/timecop/time_extensions.rb +++ b/lib/timecop/time_extensions.rb @@ -181,7 +181,7 @@ def clock_gettime_mock_time(clock_id, unit = :float_second) mock_time_realtime end - return clock_gettime_without_mock(clock_id, unit) unless mock_time + return clock_gettime_without_mock(clock_id, unit) unless Timecop.mock_process_clock? && mock_time divisor = case unit when :float_second diff --git a/lib/timecop/timecop.rb b/lib/timecop/timecop.rb index da4d7cd..eec622b 100644 --- a/lib/timecop/timecop.rb +++ b/lib/timecop/timecop.rb @@ -142,6 +142,14 @@ def scaled? !instance.stack.empty? && instance.stack.last.mock_type == :scale end + def mock_process_clock=(mock) + @mock_process_clock = mock + end + + def mock_process_clock? + @mock_process_clock ||= false + end + private def send_travel(mock_type, *args, &block) val = instance.travel(mock_type, *args, &block) diff --git a/test/timecop_with_process_clock_test.rb b/test/timecop_with_process_clock_test.rb index d1738cf..c131f5b 100644 --- a/test/timecop_with_process_clock_test.rb +++ b/test/timecop_with_process_clock_test.rb @@ -4,11 +4,24 @@ class TestTimecopWithProcessClock < Minitest::Test TIME_EPSILON = 0.001 # seconds - represents enough time for Process.clock_gettime to have advanced if not frozen + def setup + Timecop.mock_process_clock = true + end + def teardown Timecop.return + Timecop.mock_process_clock = false end if RUBY_VERSION >= '2.1.0' + def test_process_clock_mock_disabled + Timecop.mock_process_clock = false + + Timecop.freeze do + refute_same(*consecutive_monotonic, "CLOCK_MONOTONIC is frozen") + end + end + def test_process_clock_gettime_monotonic Timecop.freeze do assert_same(*consecutive_monotonic, "CLOCK_MONOTONIC is not frozen") From 67e0af17e765fe3790a525e12780117be578d17d Mon Sep 17 00:00:00 2001 From: Josh Cronemeyer Date: Sat, 1 Jun 2024 14:52:47 -0700 Subject: [PATCH 2/3] prepare release 0.9.9 --- History.md | 7 ++++++- lib/timecop/version.rb | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/History.md b/History.md index c871870..1aac11c 100644 --- a/History.md +++ b/History.md @@ -2,8 +2,13 @@ ## Unreleased -- Add `travelled?` and `scaled?` methods to allow checking if Timecop is in their respective states ([#414](https://github.com/travisjeffery/timecop/pull/414)) +## v0.9.9 + +- Add `travelled?` and `scaled?` methods to allow checking if Timecop is in their respective states ([#414](https://github.com/travisjeffery/timecop/pull/414)) +- Fix cases with DateTime parse not working right ([#415](https://github.com/travisjeffery/timecop/pull/415)) +- Fix another case where DateTime parse not working right ([#417](https://github.com/travisjeffery/timecop/pull/417)) +- Support travel and freeze for Process.clock_gettime ([#419](https://github.com/travisjeffery/timecop/pull/419)) ## v0.9.8 diff --git a/lib/timecop/version.rb b/lib/timecop/version.rb index 9b056fa..197f031 100644 --- a/lib/timecop/version.rb +++ b/lib/timecop/version.rb @@ -1,3 +1,3 @@ class Timecop - VERSION = "0.9.8" + VERSION = "0.9.9" end From 340a944703ca587398aa5bee8a7f7ca7f6f9e1e6 Mon Sep 17 00:00:00 2001 From: Josh Cronemeyer Date: Fri, 14 Jun 2024 12:20:50 -0700 Subject: [PATCH 3/3] update readme --- README.markdown | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.markdown b/README.markdown index dc197a1..82715ac 100644 --- a/README.markdown +++ b/README.markdown @@ -129,6 +129,15 @@ Timecop.freeze # => Timecop::SafeModeException: Safe mode is enabled, only calls passing a block are allowed. ``` +### Configuring Mocking Process.clock_gettime + +By default Timecop does not mock Process.clock_gettime. You must enable it like this: + +``` ruby +# turn on +Timecop.mock_process_clock = true +``` + ### Rails v Ruby Date/Time libraries Sometimes [Rails Date/Time methods don't play nicely with Ruby Date/Time methods.](https://rails.lighthouseapp.com/projects/8994/tickets/6410-dateyesterday-datetoday)