Skip to content
This repository has been archived by the owner on Aug 19, 2022. It is now read-only.

fix flaky EWMA test #205

Merged
merged 1 commit into from
Aug 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package peerstore

import (
"fmt"
"math"
"math/rand"
"testing"
"time"
Expand Down Expand Up @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the problem. This would generate values outside of the range [90, 110] due to float overflows.

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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally useless sleep making this test run for 2s.

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