From a1847cfe95e4fb73867cf2aeb5c41e332a7fef1e Mon Sep 17 00:00:00 2001 From: Will Hawkins Date: Thu, 16 Dec 2021 18:03:00 -0500 Subject: Improve robustness Improve robustness of the client in the case(s) where a network goes away in the middle of the test. Report errors properly in these cases and gracefully end the test. --- ma/ma.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'ma') diff --git a/ma/ma.go b/ma/ma.go index 8d06a67..f11dbbe 100644 --- a/ma/ma.go +++ b/ma/ma.go @@ -21,6 +21,7 @@ func NewMovingAverage(intervals int) *MovingAverage { func (ma *MovingAverage) AddMeasurement(measurement float64) { ma.instants[ma.index] = measurement ma.divisor.Add(1) + // Invariant: ma.index always points to the oldest measurement ma.index = (ma.index + 1) % ma.intervals } @@ -40,9 +41,11 @@ func (ma *MovingAverage) AllSequentialIncreasesLessThan(limit float64) bool { return false } - previous := ma.instants[ma.index] + // Invariant: ma.index always points to the oldest (see AddMeasurement above) + oldestIndex := ma.index + previous := ma.instants[oldestIndex] for i := 1; i < ma.intervals; i++ { - currentIndex := (ma.index + i) % ma.intervals + currentIndex := (oldestIndex + i) % ma.intervals current := ma.instants[currentIndex] percentChange := utilities.SignedPercentDifference(current, previous) previous = current -- cgit v1.2.3