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

Disable Process#clock_gettime mocking by default #426

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/timecop/time_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions lib/timecop/timecop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions test/timecop_with_process_clock_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down