From 0b40f1a1e537cb33b4afa6db2d53717cc58a1620 Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Thu, 26 Mar 2020 13:41:28 +0100 Subject: [PATCH] metrics: make flawed test less flawed --- metrics/timer_test.go | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/metrics/timer_test.go b/metrics/timer_test.go index 330de609bcd4..903e8e8d496e 100644 --- a/metrics/timer_test.go +++ b/metrics/timer_test.go @@ -45,10 +45,23 @@ func TestTimerStop(t *testing.T) { } func TestTimerFunc(t *testing.T) { - tm := NewTimer() - tm.Time(func() { time.Sleep(50e6) }) - if max := tm.Max(); 35e6 > max || max > 145e6 { - t.Errorf("tm.Max(): 35e6 > %v || %v > 145e6\n", max, max) + var ( + tm = NewTimer() + testStart = time.Now() + 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) } }