Skip to content

Commit 4674e25

Browse files
committed
[GR-14654] Fix timing specs.
PullRequest: truffleruby/758
2 parents 41f1695 + 3de4d2a commit 4674e25

File tree

11 files changed

+27
-38
lines changed

11 files changed

+27
-38
lines changed

spec/mspec/lib/mspec/matchers/be_close.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
TOLERANCE = 0.00003 unless Object.const_defined?(:TOLERANCE)
2+
# To account for GC, context switches, other processes, load, etc.
3+
TIME_TOLERANCE = 20.0 unless Object.const_defined?(:TIME_TOLERANCE)
24

35
class BeCloseMatcher
46
def initialize(expected, tolerance)

spec/ruby/core/file/mtime_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
it "returns the modification Time of the file" do
1414
File.mtime(@filename).should be_kind_of(Time)
15-
File.mtime(@filename).should be_close(@mtime, 60.0)
15+
File.mtime(@filename).should be_close(@mtime, TIME_TOLERANCE)
1616
end
1717

1818
guard -> { platform_is :linux or (platform_is :windows and ruby_version_is '2.5') } do

spec/ruby/core/file/utime_spec.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
File.atime(@file2).should be_close(@atime, 0.0001)
2828
File.mtime(@file2).should be_close(@mtime, 0.0001)
2929
else
30-
File.atime(@file1).to_i.should be_close(@atime.to_i, 2)
31-
File.mtime(@file1).to_i.should be_close(@mtime.to_i, 2)
32-
File.atime(@file2).to_i.should be_close(@atime.to_i, 2)
33-
File.mtime(@file2).to_i.should be_close(@mtime.to_i, 2)
30+
File.atime(@file1).to_i.should be_close(@atime.to_i, TIME_TOLERANCE)
31+
File.mtime(@file1).to_i.should be_close(@mtime.to_i, TIME_TOLERANCE)
32+
File.atime(@file2).to_i.should be_close(@atime.to_i, TIME_TOLERANCE)
33+
File.mtime(@file2).to_i.should be_close(@mtime.to_i, TIME_TOLERANCE)
3434
end
3535
end
3636

@@ -43,10 +43,10 @@
4343
File.atime(@file2).should be_close(tn, 0.050)
4444
File.mtime(@file2).should be_close(tn, 0.050)
4545
else
46-
File.atime(@file1).to_i.should be_close(Time.now.to_i, 2)
47-
File.mtime(@file1).to_i.should be_close(Time.now.to_i, 2)
48-
File.atime(@file2).to_i.should be_close(Time.now.to_i, 2)
49-
File.mtime(@file2).to_i.should be_close(Time.now.to_i, 2)
46+
File.atime(@file1).to_i.should be_close(Time.now.to_i, TIME_TOLERANCE)
47+
File.mtime(@file1).to_i.should be_close(Time.now.to_i, TIME_TOLERANCE)
48+
File.atime(@file2).to_i.should be_close(Time.now.to_i, TIME_TOLERANCE)
49+
File.mtime(@file2).to_i.should be_close(Time.now.to_i, TIME_TOLERANCE)
5050
end
5151
end
5252

@@ -63,10 +63,10 @@
6363
File.mtime(@file2).should be_close(@mtime, 0.0001)
6464
else
6565
File.utime(@atime.to_i, @mtime.to_i, @file1, @file2)
66-
File.atime(@file1).to_i.should be_close(@atime.to_i, 2)
67-
File.mtime(@file1).to_i.should be_close(@mtime.to_i, 2)
68-
File.atime(@file2).to_i.should be_close(@atime.to_i, 2)
69-
File.mtime(@file2).to_i.should be_close(@mtime.to_i, 2)
66+
File.atime(@file1).to_i.should be_close(@atime.to_i, TIME_TOLERANCE)
67+
File.mtime(@file1).to_i.should be_close(@mtime.to_i, TIME_TOLERANCE)
68+
File.atime(@file2).to_i.should be_close(@atime.to_i, TIME_TOLERANCE)
69+
File.mtime(@file2).to_i.should be_close(@mtime.to_i, TIME_TOLERANCE)
7070
end
7171
end
7272

spec/ruby/core/process/clock_gettime_spec.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,17 @@
3636
t2 = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_second)
3737

3838
t1.should be_an_instance_of(Float)
39-
t2.should be_close(t1, 2.0) # 2.0 is chosen arbitrarily to allow for time skew without admitting failure cases, which would be off by an order of magnitude.
39+
t2.should be_an_instance_of(Float)
40+
t2.should be_close(t1, TIME_TOLERANCE)
4041
end
4142

4243
it 'uses the default time unit (:float_second) when passed nil' do
4344
t1 = Process.clock_gettime(Process::CLOCK_MONOTONIC, nil)
4445
t2 = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_second)
4546

4647
t1.should be_an_instance_of(Float)
47-
t2.should be_close(t1, 2.0) # 2.0 is chosen arbitrarily to allow for time skew without admitting failure cases, which would be off by an order of magnitude.
48+
t2.should be_an_instance_of(Float)
49+
t2.should be_close(t1, TIME_TOLERANCE)
4850
end
4951
end
5052
end

spec/ruby/core/time/shared/now.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
it "sets the current time" do
1010
now = TimeSpecs::MethodHolder.send(@method)
11-
now.to_f.should be_close(Process.clock_gettime(Process::CLOCK_REALTIME), 10.0)
11+
now.to_f.should be_close(Process.clock_gettime(Process::CLOCK_REALTIME), TIME_TOLERANCE)
1212
end
1313

1414
it "uses the local timezone" do

spec/ruby/library/datetime/now_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
end
88

99
it "sets the current date" do
10-
(DateTime.now - Date.today).to_f.should be_close(0.0, 2.0)
10+
(DateTime.now - Date.today).to_f.should be_close(0.0, TIME_TOLERANCE)
1111
end
1212

1313
it "sets the current time" do
1414
dt = DateTime.now
1515
now = Time.now
16-
(dt.to_time - now).should be_close(0.0, 10.0)
16+
(dt.to_time - now).should be_close(0.0, TIME_TOLERANCE)
1717
end
1818

1919
it "grabs the local timezone" do

spec/ruby/library/socket/fixtures/classes.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ def self.each_ip_protocol
7373

7474
def self.loop_with_timeout(timeout = 5)
7575
require 'timeout'
76-
time = Time.now
76+
time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
7777

7878
loop do
79-
if Time.now - time >= timeout
79+
if Process.clock_gettime(Process::CLOCK_MONOTONIC) - time >= timeout
8080
raise TimeoutError, "Did not succeed within #{timeout} seconds"
8181
end
8282

@@ -85,7 +85,7 @@ def self.loop_with_timeout(timeout = 5)
8585
end
8686
end
8787

88-
def self.wait_until_success(timeout = 5)
88+
def self.wait_until_success(timeout = 10)
8989
loop_with_timeout(timeout) do
9090
begin
9191
return yield

spec/ruby/library/timeout/timeout_spec.rb

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,6 @@
1818
end.should raise_error(StandardError)
1919
end
2020

21-
it "does not wait too long" do
22-
before_time = Time.now
23-
lambda do
24-
Timeout.timeout(1, StandardError) do
25-
sleep 3
26-
end
27-
end.should raise_error(StandardError)
28-
29-
(Time.now - before_time).should be_close(1.0, 0.5)
30-
end
31-
3221
it "returns back the last value in the block" do
3322
Timeout.timeout(1) do
3423
42

spec/ruby/optional/capi/thread_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def call_capi_rb_thread_wakeup
2424
it "sleeps the current thread for the give amount of time" do
2525
start = Time.now
2626
@t.rb_thread_wait_for(0, 100_000)
27-
(Time.now - start).should be_close(0.1, 0.2)
27+
(Time.now - start).should be_close(0.1, TIME_TOLERANCE)
2828
end
2929
end
3030

spec/ruby/optional/capi/time_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@
294294
now = Time.now
295295
time = @s.rb_time_from_timespec(now.utc_offset)
296296
time.should be_an_instance_of(Time)
297-
(time - now).should be_close(0, 10)
297+
(time - now).should be_close(0, TIME_TOLERANCE)
298298
end
299299
end
300300
end

0 commit comments

Comments
 (0)