Skip to content

Commit

Permalink
pkg/window: fix panic in test (#7511)
Browse files Browse the repository at this point in the history
close #7386

Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com>

Co-authored-by: ti-chi-bot[bot] <108142056+ti-chi-bot[bot]@users.noreply.github.com>
  • Loading branch information
CabinfeverB and ti-chi-bot[bot] authored Dec 8, 2023
1 parent ba3776c commit 00674d0
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions pkg/window/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ import (
"github.com/stretchr/testify/require"
)

func GetRollingPolicy() *RollingPolicy {
w := NewWindow(Options{Size: 3})
return NewRollingPolicy(w, RollingPolicyOpts{BucketDuration: 100 * time.Millisecond})
const defaultBucketDuration = 100 * time.Millisecond
const defaultSize = 3

func getRollingPolicy() *RollingPolicy {
w := NewWindow(Options{Size: defaultSize})
return NewRollingPolicy(w, RollingPolicyOpts{BucketDuration: defaultBucketDuration})
}

func TestRollingPolicy_Add(t *testing.T) {
Expand All @@ -45,6 +48,7 @@ func TestRollingPolicy_Add(t *testing.T) {
points: []float64{1, 1},
},
{
// In CI, the actual sleep time may be more than 100 (timeSleep = 94).
timeSleep: []int{94, 250},
offset: []int{0, 0},
points: []float64{1, 1},
Expand All @@ -60,14 +64,25 @@ func TestRollingPolicy_Add(t *testing.T) {
t.Run("test policy add", func(t *testing.T) {
var totalTS, lastOffset int
timeSleep := test.timeSleep
policy := GetRollingPolicy()
beginTime := time.Now()
policy := getRollingPolicy()
points := make([]float64, defaultSize)
asExpected := true
for i, n := range timeSleep {
totalTS += n
time.Sleep(time.Duration(n) * time.Millisecond)
offset, point := test.offset[i], test.points[i]
point := test.points[i]
offset := int(time.Since(beginTime)/defaultBucketDuration) % defaultSize
points[i] += point
policy.Add(point)

re.Less(math.Abs(point-policy.window.buckets[offset].Points[0]), 1e-6,
if offset != test.offset[i] {
asExpected = false
}
if asExpected {
re.Less(math.Abs(point-policy.window.buckets[offset].Points[0]), 1e-6,
fmt.Sprintf("error, time since last append: %vms, last offset: %v", totalTS, lastOffset))
}
re.Less(math.Abs(points[i]-policy.window.buckets[offset].Points[0]), 1e-6,
fmt.Sprintf("error, time since last append: %vms, last offset: %v", totalTS, lastOffset))
lastOffset = offset
}
Expand All @@ -78,7 +93,7 @@ func TestRollingPolicy_Add(t *testing.T) {
func TestRollingPolicy_AddWithTimespan(t *testing.T) {
re := require.New(t)
t.Run("timespan < bucket number", func(t *testing.T) {
policy := GetRollingPolicy()
policy := getRollingPolicy()
// bucket 0
policy.Add(0)
// bucket 1
Expand All @@ -102,7 +117,7 @@ func TestRollingPolicy_AddWithTimespan(t *testing.T) {
})

t.Run("timespan > bucket number", func(t *testing.T) {
policy := GetRollingPolicy()
policy := getRollingPolicy()

// bucket 0
policy.Add(0)
Expand Down

0 comments on commit 00674d0

Please sign in to comment.