From 975d3d048074ead5a01263090d746df4bde122ba Mon Sep 17 00:00:00 2001 From: Mathias Griffe Date: Thu, 11 Nov 2021 10:10:36 +0400 Subject: [PATCH] Fix TimeWindow not resetting buckets when re-using them. --- time.go | 6 +++++- time_test.go | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/time.go b/time.go index da4910e..273eeb3 100644 --- a/time.go +++ b/time.go @@ -79,7 +79,11 @@ func (w *TimePolicy) Append(value float64) { var adjustedTime, windowOffset = w.selectBucket(time.Now()) w.keepConsistent(adjustedTime, windowOffset) - w.window[windowOffset] = append(w.window[windowOffset], value) + if w.lastWindowOffset != windowOffset { + w.window[windowOffset] = []float64{value} + } else { + w.window[windowOffset] = append(w.window[windowOffset], value) + } w.lastWindowTime = adjustedTime w.lastWindowOffset = windowOffset } diff --git a/time_test.go b/time_test.go index c3d23de..b6bf73c 100644 --- a/time_test.go +++ b/time_test.go @@ -28,6 +28,24 @@ func TestTimeWindow(t *testing.T) { if final != float64(numberBuckets) { t.Fatal(final) } + + for x := 0; x < numberBuckets; x = x + 1 { + p.Append(2) + time.Sleep(bucketSize) + } + + final = p.Reduce(func(w Window) float64 { + var result float64 + for _, bucket := range w { + for _, point := range bucket { + result = result + point + } + } + return result + }) + if final != 2*float64(numberBuckets) { + t.Fatal("got", final, "expected", 2*float64(numberBuckets)) + } } func TestTimeWindowSelectBucket(t *testing.T) {