Unverified Commit 32d31c31 authored by Martin Holst Swende's avatar Martin Holst Swende Committed by GitHub

metrics: improve TestTimerFunc (#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.
parent 3b69c14f
...@@ -45,10 +45,23 @@ func TestTimerStop(t *testing.T) { ...@@ -45,10 +45,23 @@ func TestTimerStop(t *testing.T) {
} }
func TestTimerFunc(t *testing.T) { func TestTimerFunc(t *testing.T) {
tm := NewTimer() var (
tm.Time(func() { time.Sleep(50e6) }) tm = NewTimer()
if max := tm.Max(); 35e6 > max || max > 145e6 { testStart = time.Now()
t.Errorf("tm.Max(): 35e6 > %v || %v > 145e6\n", max, max) actualTime time.Duration
)
tm.Time(func() {
time.Sleep(50 * time.Millisecond)
actualTime = time.Since(testStart)
})
var (
drift = time.Millisecond * 2
measured = time.Duration(tm.Max())
ceil = actualTime + drift
floor = actualTime - drift
)
if measured > ceil || measured < floor {
t.Errorf("tm.Max(): %v > %v || %v > %v\n", measured, ceil, measured, floor)
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment