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

Optional clock gettime 2 #427

Merged
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
7 changes: 6 additions & 1 deletion History.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 9 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
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
2 changes: 1 addition & 1 deletion lib/timecop/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class Timecop
VERSION = "0.9.8"
VERSION = "0.9.9"
end
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