Skip to content

Commit

Permalink
Only test that sleeps were long enough
Browse files Browse the repository at this point in the history
This spec was originally added to confirm that sub-millisecond
sleeps actually did sleep; on JRuby and TruffleRuby before fixes
a sleep of 0.0001s would immediately return, because we both used
a sleep function with a minimum resolution of 0.001s. Ideally 100
sleeps of 0.0001s should not exceed 0.03s, but since that's not
the goal of this spec and since it makes the spec flaky under load
or on slower systems, it seems best to remove this check.

The remaining check just confirms that 100x sleep of 0.0001s does
actually sleep for at least 0.01s. Any Ruby failing the spec now
would indicate they are not actually sleeping for 0.0001s and they
need a fix.
  • Loading branch information
headius authored and eregon committed Nov 7, 2024
1 parent 54c391e commit bbcd077
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions core/kernel/sleep_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,15 @@ def o.divmod(*); [0, 0.001]; end
t.value.should == 5
end

platform_is_not :darwin do
it "sleeps with nanosecond precision" do
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
100.times do
sleep(0.0001)
end
end_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)

actual_duration = end_time - start_time
(actual_duration > 0.01).should == true # 100 * 0.0001 => 0.01
(actual_duration < 0.03).should == true
it "sleeps with nanosecond precision" do
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
100.times do
sleep(0.0001)
end
end_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)

actual_duration = end_time - start_time
actual_duration.should > 0.01 # 100 * 0.0001 => 0.01
end

ruby_version_is ""..."3.3" do
Expand Down

0 comments on commit bbcd077

Please sign in to comment.