Skip to content

Commit 29804ac

Browse files
committed
Only assert a lower bound on sleep times
* There might be arbitrary delays due to GC, other process, load, etc.
1 parent 40fbc36 commit 29804ac

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

spec/ruby/core/kernel/sleep_spec.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,20 @@
66
Kernel.should have_private_instance_method(:sleep)
77
end
88

9+
it "returns an Integer" do
10+
sleep(0.001).should be_kind_of(Integer)
11+
end
12+
913
it "accepts a Float" do
10-
sleep(0.1).should be_close(0, 2)
14+
sleep(0.001).should >= 0
1115
end
1216

1317
it "accepts a Fixnum" do
14-
sleep(0).should be_close(0, 2)
18+
sleep(0).should >= 0
1519
end
1620

1721
it "accepts a Rational" do
18-
sleep(Rational(1, 9)).should be_close(0, 2)
22+
sleep(Rational(1, 999)).should >= 0
1923
end
2024

2125
it "raises an ArgumentError when passed a negative duration" do

spec/ruby/optional/capi/mutex_spec.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,10 @@
7272

7373
it "sleeps when the mutex is locked" do
7474
@m.lock
75-
start = Time.now
76-
@s.rb_mutex_sleep(@m, 0.1)
77-
(Time.now - start).should be_close(0.1, 0.2)
75+
t1 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
76+
@s.rb_mutex_sleep(@m, 0.001)
77+
t2 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
78+
(t2 - t1).should >= 0
7879
@m.locked?.should be_true
7980
end
8081
end

0 commit comments

Comments
 (0)