From dee426a7a58fd775ce72bb74571b83285eaaedb1 Mon Sep 17 00:00:00 2001 From: Jiquan Long Date: Wed, 7 Dec 2022 16:11:22 +0800 Subject: [PATCH] Fix group checker ut (#21038) Signed-off-by: longjiquan Signed-off-by: longjiquan --- internal/util/timerecord/group_checker.go | 1 + internal/util/timerecord/group_checker_test.go | 13 ++++++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/internal/util/timerecord/group_checker.go b/internal/util/timerecord/group_checker.go index a188b8ef36..a5fd89a28b 100644 --- a/internal/util/timerecord/group_checker.go +++ b/internal/util/timerecord/group_checker.go @@ -51,6 +51,7 @@ func (gc *GroupChecker) init() { // work is the main procedure logic func (gc *GroupChecker) work() { gc.t = time.NewTicker(gc.d) + defer gc.t.Stop() var name string var ts time.Time diff --git a/internal/util/timerecord/group_checker_test.go b/internal/util/timerecord/group_checker_test.go index 68ea432b81..b2256944d6 100644 --- a/internal/util/timerecord/group_checker_test.go +++ b/internal/util/timerecord/group_checker_test.go @@ -28,7 +28,8 @@ func TestGroupChecker(t *testing.T) { signal := make(chan []string, 1) // 10ms period which set before is too short // change 10ms to 500ms to ensure the the group checker schedule after the second value stored - gc1 := GetGroupChecker(groupName, 500*time.Millisecond, func(list []string) { + duration := 500 * time.Millisecond + gc1 := GetGroupChecker(groupName, duration, func(list []string) { signal <- list }) gc1.Check("1") @@ -37,14 +38,16 @@ func TestGroupChecker(t *testing.T) { }) gc2.Check("2") - assert.Equal(t, 500*time.Millisecond, gc2.d) + assert.Equal(t, duration, gc2.d) - list := <-signal - assert.ElementsMatch(t, []string{"1", "2"}, list) + assert.Eventually(t, func() bool { + list := <-signal + return len(list) == 2 + }, duration*3, duration) gc2.Remove("2") - list = <-signal + list := <-signal assert.ElementsMatch(t, []string{"1"}, list) assert.NotPanics(t, func() {