Skip to content

Commit 26ac766

Browse files
holimanenriquefynn
authored andcommitted
metrics: improve TestTimerFunc (ethereum#20818)
The test failed due to what appears to be fluctuations in time.Sleep, which is not the actual method under test. This change modifies it so we compare the metered Max to the actual time instead of the desired time.
1 parent 00f0dd5 commit 26ac766

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

metrics/timer_test.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,23 @@ func TestTimerStop(t *testing.T) {
4545
}
4646

4747
func TestTimerFunc(t *testing.T) {
48-
tm := NewTimer()
49-
tm.Time(func() { time.Sleep(50e6) })
50-
if max := tm.Max(); 35e6 > max || max > 145e6 {
51-
t.Errorf("tm.Max(): 35e6 > %v || %v > 145e6\n", max, max)
48+
var (
49+
tm = NewTimer()
50+
testStart = time.Now()
51+
actualTime time.Duration
52+
)
53+
tm.Time(func() {
54+
time.Sleep(50 * time.Millisecond)
55+
actualTime = time.Since(testStart)
56+
})
57+
var (
58+
drift = time.Millisecond * 2
59+
measured = time.Duration(tm.Max())
60+
ceil = actualTime + drift
61+
floor = actualTime - drift
62+
)
63+
if measured > ceil || measured < floor {
64+
t.Errorf("tm.Max(): %v > %v || %v > %v\n", measured, ceil, measured, floor)
5265
}
5366
}
5467

0 commit comments

Comments
 (0)