Skip to content

Commit b5783d8

Browse files
Optional clock gettime 2 (#427)
* Disable Process#clock_gettime mocking by default * prepare release 0.9.9 * update readme --------- Co-authored-by: Alex Watt <[email protected]>
1 parent 69405ac commit b5783d8

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

README.markdown

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,15 @@ Timecop.freeze
129129
# => Timecop::SafeModeException: Safe mode is enabled, only calls passing a block are allowed.
130130
```
131131

132+
### Configuring Mocking Process.clock_gettime
133+
134+
By default Timecop does not mock Process.clock_gettime. You must enable it like this:
135+
136+
``` ruby
137+
# turn on
138+
Timecop.mock_process_clock = true
139+
```
140+
132141
### Rails v Ruby Date/Time libraries
133142

134143
Sometimes [Rails Date/Time methods don't play nicely with Ruby Date/Time methods.](https://rails.lighthouseapp.com/projects/8994/tickets/6410-dateyesterday-datetoday)

lib/timecop/time_extensions.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def clock_gettime_mock_time(clock_id, unit = :float_second)
181181
mock_time_realtime
182182
end
183183

184-
return clock_gettime_without_mock(clock_id, unit) unless mock_time
184+
return clock_gettime_without_mock(clock_id, unit) unless Timecop.mock_process_clock? && mock_time
185185

186186
divisor = case unit
187187
when :float_second

lib/timecop/timecop.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,14 @@ def scaled?
142142
!instance.stack.empty? && instance.stack.last.mock_type == :scale
143143
end
144144

145+
def mock_process_clock=(mock)
146+
@mock_process_clock = mock
147+
end
148+
149+
def mock_process_clock?
150+
@mock_process_clock ||= false
151+
end
152+
145153
private
146154
def send_travel(mock_type, *args, &block)
147155
val = instance.travel(mock_type, *args, &block)

test/timecop_with_process_clock_test.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,24 @@
44
class TestTimecopWithProcessClock < Minitest::Test
55
TIME_EPSILON = 0.001 # seconds - represents enough time for Process.clock_gettime to have advanced if not frozen
66

7+
def setup
8+
Timecop.mock_process_clock = true
9+
end
10+
711
def teardown
812
Timecop.return
13+
Timecop.mock_process_clock = false
914
end
1015

1116
if RUBY_VERSION >= '2.1.0'
17+
def test_process_clock_mock_disabled
18+
Timecop.mock_process_clock = false
19+
20+
Timecop.freeze do
21+
refute_same(*consecutive_monotonic, "CLOCK_MONOTONIC is frozen")
22+
end
23+
end
24+
1225
def test_process_clock_gettime_monotonic
1326
Timecop.freeze do
1427
assert_same(*consecutive_monotonic, "CLOCK_MONOTONIC is not frozen")

0 commit comments

Comments
 (0)