Skip to content

Commit 5041e94

Browse files
committed
Use TIME_TOLERANCE in specs instead of hardcoded numbers
1 parent dfbbfe7 commit 5041e94

File tree

7 files changed

+22
-20
lines changed

7 files changed

+22
-20
lines changed

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/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)