Skip to content

Commit

Permalink
Fix TimeWindow not resetting buckets when re-using them.
Browse files Browse the repository at this point in the history
  • Loading branch information
MathiasGr committed Nov 11, 2021
1 parent fe8c9d1 commit 975d3d0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
6 changes: 5 additions & 1 deletion time.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
18 changes: 18 additions & 0 deletions time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 975d3d0

Please sign in to comment.