From 183adc0d7a4becafedc3968840882e98ff803556 Mon Sep 17 00:00:00 2001 From: Paul Lorenz Date: Wed, 10 Mar 2021 13:09:57 -0500 Subject: [PATCH] Fix flaky test by clamping times to nearest 100ms --- health/checks.go | 5 ++++- health/health.go | 17 +++++++++++++++++ health/round_test.go | 28 ++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 health/round_test.go diff --git a/health/checks.go b/health/checks.go index 5046ff622..706b737d7 100644 --- a/health/checks.go +++ b/health/checks.go @@ -107,7 +107,7 @@ func (self *actionImpl) Matches(result *health.Result) bool { logger := pfxlog.Logger() logger.WithField("action", self.Description()).Debug("evaluating") - now := time.Now() + now := roundToClosest(time.Now(), timeClamp) if result.IsHealthy() { if self.consectivePasses == 0 { self.passingSince = now @@ -138,6 +138,9 @@ func (self *actionImpl) Matches(result *health.Result) bool { if result.IsHealthy() && now.Sub(self.passingSince) < *self.Duration { return false } + if result.TimeOfFirstFailure != nil { + fmt.Printf("current fail duration: %v, required duration: %v\n", now.Sub(*result.TimeOfFirstFailure), *self.Duration) + } if !result.IsHealthy() && now.Sub(*result.TimeOfFirstFailure) < *self.Duration { return false } diff --git a/health/health.go b/health/health.go index d19879226..7a63ccc72 100644 --- a/health/health.go +++ b/health/health.go @@ -9,6 +9,11 @@ import ( "github.com/openziti/sdk-golang/ziti/edge" "reflect" "sync" + "time" +) + +const ( + timeClamp = 100 * time.Millisecond ) type ServiceUpdater interface { @@ -121,6 +126,10 @@ func (self *manager) handleResults() { } func (self *manager) handleResult(result *result) { + if result.TimeOfFirstFailure != nil { + rounded := roundToClosest(*result.TimeOfFirstFailure, timeClamp) + result.TimeOfFirstFailure = &rounded + } if val, ok := self.checks.Load(result.name); ok { if check, ok := val.(*checkContext); ok { for _, action := range check.actions { @@ -228,3 +237,11 @@ type result struct { name string health.Result } + +func roundToClosest(t time.Time, interval time.Duration) time.Time { + roundDown := t.Truncate(interval) + if t.Sub(roundDown) >= interval/2 { + return roundDown.Add(interval) + } + return roundDown +} diff --git a/health/round_test.go b/health/round_test.go new file mode 100644 index 000000000..0deac1d25 --- /dev/null +++ b/health/round_test.go @@ -0,0 +1,28 @@ +package health + +import ( + "fmt" + "github.com/stretchr/testify/assert" + "testing" + "time" +) + +func TestRound(t *testing.T) { + interval := time.Millisecond * 100 + + for i := 0; i < 100; i++ { + now := time.Now() + val := roundToClosest(now, interval) + lower := now.Truncate(interval) + upper := lower.Add(interval) + lowerDelta := val.Sub(lower) + upperDelta := upper.Sub(val) + if lowerDelta < upperDelta { + assert.Equal(t, val, lower) + } else { + assert.Equal(t, val, upper) + } + fmt.Printf("now: %v rounded: %v\n", now, val) + time.Sleep(25 * time.Millisecond) + } +}