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