From 9161f4a1743d1a8bff8c62432593be76452a574e Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Fri, 12 Aug 2022 23:10:01 +0400 Subject: [PATCH] fix flaky EWMA test (#205) --- p2p/host/peerstore/metrics_test.go | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/p2p/host/peerstore/metrics_test.go b/p2p/host/peerstore/metrics_test.go index 7e5f5a01e3..723baad275 100644 --- a/p2p/host/peerstore/metrics_test.go +++ b/p2p/host/peerstore/metrics_test.go @@ -2,7 +2,6 @@ package peerstore import ( "fmt" - "math" "math/rand" "testing" "time" @@ -44,21 +43,21 @@ func TestLatencyEWMA(t *testing.T) { t.Fatal(err) } - exp := 100.0 - mu := exp - sig := 10.0 - next := func() time.Duration { - mu := (rand.NormFloat64() * sig) + mu - return time.Duration(mu) - } + const exp = 100 + const mu = exp + const sig = 10 + next := func() time.Duration { return time.Duration(rand.Intn(20) - 10 + mu) } for i := 0; i < 10; i++ { - time.Sleep(200 * time.Millisecond) m.RecordLatency(id, next()) } lat := m.LatencyEWMA(id) - if math.Abs(exp-float64(lat)) > sig { - t.Fatal("latency outside of expected range: ", exp, lat, sig) + diff := exp - lat + if diff < 0 { + diff = -diff + } + if diff > sig { + t.Fatalf("latency outside of expected range. expected %d ± %d, got %d", exp, sig, lat) } }