-
Notifications
You must be signed in to change notification settings - Fork 226
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
Add Process.clock_gettime
support
#419
Changes from 8 commits
350a4cd
27e92d7
9d1209c
317c18b
b443315
231750a
de109f7
b332e4c
c95628f
a59afa0
e9489b6
62f51e8
e550a95
94c5734
466fc75
5afef57
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -284,7 +284,7 @@ def test_scaling_returns_now_if_no_block_given | |
t = Time.local(2008, 10, 10, 10, 10, 10) | ||
assert_times_effectively_equal t, Timecop.scale(4, t) | ||
end | ||
|
||
def test_scaling_returns_now_if_nil_supplied | ||
assert_times_effectively_equal Time.now, Timecop.scale(nil) | ||
end | ||
|
@@ -421,7 +421,7 @@ def test_return_temporarily_returns_to_current_time_in_given_block | |
end | ||
assert_times_effectively_equal(time_after_travel, Time.now) | ||
end | ||
|
||
def test_travel_returns_now_if_nil_supplied | ||
assert_times_effectively_equal Time.now, Timecop.travel(nil) | ||
end | ||
|
@@ -433,7 +433,7 @@ def test_travel_time_with_block_returns_the_value_of_the_block | |
|
||
assert_equal expected, actual | ||
end | ||
|
||
def test_travel_raises_when_empty_string_supplied | ||
err = assert_raises(ArgumentError) do | ||
Timecop.travel("") | ||
|
@@ -466,7 +466,7 @@ def test_freeze_without_params | |
end | ||
end | ||
end | ||
|
||
def test_freeze_returns_now_if_nil_supplied | ||
assert_times_effectively_equal Time.now, Timecop.freeze(nil) | ||
end | ||
|
@@ -689,8 +689,105 @@ def test_thread_safe_timecop_returns_after_block | |
Timecop.thread_safe = false | ||
end | ||
|
||
if RUBY_VERSION >= '2.1.0' | ||
alexcwatt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def test_process_clock_gettime_monotonic | ||
Timecop.freeze do | ||
alexcwatt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert_same monotonic, monotonic, "CLOCK_MONOTONIC is not frozen" | ||
end | ||
|
||
initial_time = monotonic | ||
Timecop.freeze(-0.5) do | ||
assert_operator(monotonic, :<, initial_time, "CLOCK_MONOTONIC is not traveling back in time") | ||
end | ||
end | ||
|
||
def test_process_clock_gettime_monotonic_nested | ||
Timecop.freeze do | ||
parent = monotonic | ||
|
||
sleep(0.01) | ||
|
||
delta = 0.5 | ||
Timecop.freeze(delta) do | ||
child = monotonic | ||
assert_equal(child, parent + delta, "Nested freeze not working for monotonic time") | ||
end | ||
end | ||
end | ||
|
||
def test_process_clock_gettime_monotonic_travel | ||
initial_time = monotonic | ||
Timecop.travel do | ||
refute_same monotonic, monotonic, "CLOCK_MONOTONIC is frozen" | ||
assert_operator(monotonic, :>, initial_time, "CLOCK_MONOTONIC is not moving forward") | ||
end | ||
|
||
Timecop.travel(-0.5) do | ||
refute_same monotonic, monotonic, "CLOCK_MONOTONIC is frozen" | ||
alexcwatt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert_operator(monotonic, :<, initial_time, "CLOCK_MONOTONIC is not traveling properly") | ||
end | ||
end | ||
|
||
def test_process_clock_gettime_monotonic_scale | ||
scale = 4 | ||
sleep_length = 0.25 | ||
Timecop.scale(scale) do | ||
initial_time = monotonic | ||
sleep(sleep_length) | ||
expected_time = initial_time + (scale * sleep_length) | ||
assert_times_effectively_equal expected_time, monotonic, 0.1, "CLOCK_MONOTONIC is not scaling" | ||
end | ||
end | ||
|
||
def test_process_clock_gettime_realtime | ||
Timecop.freeze do | ||
assert_same realtime, realtime, "CLOCK_REALTIME is not frozen" | ||
end | ||
|
||
initial_time = realtime | ||
Timecop.freeze(-20) do | ||
alexcwatt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert_operator(realtime, :<, initial_time, "CLOCK_REALTIME is not traveling back in time") | ||
end | ||
end | ||
|
||
def test_process_clock_gettime_realtime_travel | ||
initial_time = realtime | ||
Timecop.travel do | ||
refute_equal realtime, realtime, "CLOCK_REALTIME is frozen" | ||
assert_operator(realtime, :>, initial_time, "CLOCK_REALTIME is not moving forward") | ||
end | ||
|
||
Timecop.travel(Time.now - 0.1) do | ||
refute_equal realtime, realtime, "CLOCK_REALTIME is frozen" | ||
assert_operator(realtime, :<, initial_time, "CLOCK_REALTIME is not traveling properly") | ||
sleep 0.1 | ||
assert_operator(realtime, :>, initial_time, "CLOCK_REALTIME is not traveling properly") | ||
end | ||
end | ||
|
||
def test_process_clock_gettime_realtime_scale | ||
scale = 4 | ||
sleep_length = 0.25 | ||
Timecop.scale(scale) do | ||
initial_time = realtime | ||
sleep(sleep_length) | ||
assert_operator(initial_time + scale * sleep_length, :<, realtime, "CLOCK_REALTIME is not scaling") | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
if RUBY_VERSION >= '2.1.0' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know this is just following the convention of this codebase, so this isn't a comment on your PR per se: Ruby 2.1.0 is 11 years old and has been EOL for 7 years. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to update the minimum Ruby version in the gemspec; right now it's at 1.9.2. |
||
def monotonic | ||
Process.clock_gettime(Process::CLOCK_MONOTONIC) | ||
alexcwatt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
|
||
def realtime | ||
Process.clock_gettime(Process::CLOCK_REALTIME) | ||
end | ||
end | ||
|
||
def with_safe_mode(enabled=true) | ||
mode = Timecop.safe_mode? | ||
Timecop.safe_mode = enabled | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about
Process::CLOCK_THREAD_CPUTIME_ID
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I could see adding that one too. I will focus on the bug fix first and then see if I can include this scope in the PR; if not, it should be a simpler follow-up after this lands.