From 1fe464867e742a474d387431053aab78e172926e Mon Sep 17 00:00:00 2001 From: Cabinfever_B Date: Fri, 8 Dec 2023 15:25:43 +0800 Subject: [PATCH 1/3] fix panic in CI Signed-off-by: Cabinfever_B --- pkg/window/policy_test.go | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/pkg/window/policy_test.go b/pkg/window/policy_test.go index 14b3b326192..e6e6c005b20 100644 --- a/pkg/window/policy_test.go +++ b/pkg/window/policy_test.go @@ -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) { @@ -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}, @@ -60,16 +64,23 @@ 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) 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] { + 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)) + lastOffset = offset + } + 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 } }) } @@ -78,7 +89,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 @@ -102,7 +113,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) From 11bb973e844110cd025e730de2929e9d9026b852 Mon Sep 17 00:00:00 2001 From: Cabinfever_B Date: Fri, 8 Dec 2023 15:51:54 +0800 Subject: [PATCH 2/3] fix panic in CI Signed-off-by: Cabinfever_B --- pkg/window/policy_test.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pkg/window/policy_test.go b/pkg/window/policy_test.go index e6e6c005b20..ee2ab3161c0 100644 --- a/pkg/window/policy_test.go +++ b/pkg/window/policy_test.go @@ -67,6 +67,7 @@ func TestRollingPolicy_Add(t *testing.T) { 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) @@ -74,7 +75,10 @@ func TestRollingPolicy_Add(t *testing.T) { offset := int(time.Since(beginTime)/defaultBucketDuration) % defaultSize points[i] += point policy.Add(point) - if offset == test.offset[i] { + 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)) lastOffset = offset From 753a2877543af63030476b655f7a33fdd59a8a56 Mon Sep 17 00:00:00 2001 From: Cabinfever_B Date: Fri, 8 Dec 2023 15:52:24 +0800 Subject: [PATCH 3/3] fix panic in CI Signed-off-by: Cabinfever_B --- pkg/window/policy_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/window/policy_test.go b/pkg/window/policy_test.go index ee2ab3161c0..489c8428c9a 100644 --- a/pkg/window/policy_test.go +++ b/pkg/window/policy_test.go @@ -81,10 +81,10 @@ func TestRollingPolicy_Add(t *testing.T) { 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)) - lastOffset = offset } 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 } }) }