From 5711050874110b10f244195549b803ef7f184e08 Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Sat, 24 Dec 2022 22:00:55 +0800 Subject: [PATCH 1/2] util: fix data race in the cpu test Signed-off-by: Weizhen Wang --- util/cpu/BUILD.bazel | 1 + util/cpu/cpu_test.go | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/util/cpu/BUILD.bazel b/util/cpu/BUILD.bazel index 08893520caaa0..b831ba544947b 100644 --- a/util/cpu/BUILD.bazel +++ b/util/cpu/BUILD.bazel @@ -21,5 +21,6 @@ go_test( srcs = ["cpu_test.go"], embed = [":cpu"], flaky = True, + race = "on", deps = ["@com_github_stretchr_testify//require"], ) diff --git a/util/cpu/cpu_test.go b/util/cpu/cpu_test.go index cd330a11e5196..cefa1073402b5 100644 --- a/util/cpu/cpu_test.go +++ b/util/cpu/cpu_test.go @@ -25,7 +25,6 @@ import ( func TestCPUValue(t *testing.T) { Observer := NewCPUObserver() - Observer.Start() exit := make(chan struct{}) var wg sync.WaitGroup for i := 0; i < 10; i++ { From 012d237a930fe349286e95ffae7ce909ad6eeb4e Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Mon, 26 Dec 2022 19:01:10 +0800 Subject: [PATCH 2/2] improve code Signed-off-by: Weizhen Wang --- util/cpu/cpu_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/util/cpu/cpu_test.go b/util/cpu/cpu_test.go index cefa1073402b5..a191227b72f78 100644 --- a/util/cpu/cpu_test.go +++ b/util/cpu/cpu_test.go @@ -24,7 +24,7 @@ import ( ) func TestCPUValue(t *testing.T) { - Observer := NewCPUObserver() + observer := NewCPUObserver() exit := make(chan struct{}) var wg sync.WaitGroup for i := 0; i < 10; i++ { @@ -41,11 +41,11 @@ func TestCPUValue(t *testing.T) { } }() } - Observer.Start() + observer.Start() time.Sleep(5 * time.Second) require.GreaterOrEqual(t, GetCPUUsage(), 0.0) require.Less(t, GetCPUUsage(), 1.0) - Observer.Stop() + observer.Stop() close(exit) wg.Wait() }