From f5fdb7ceefdc4124361b7a4cb0554dded6e855ac Mon Sep 17 00:00:00 2001 From: Manan Gupta Date: Wed, 19 Feb 2025 19:21:11 +0530 Subject: [PATCH] feat: fix data race in the test Signed-off-by: Manan Gupta --- go/vt/discovery/healthcheck_test.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/go/vt/discovery/healthcheck_test.go b/go/vt/discovery/healthcheck_test.go index e46919c7c87..83df1718884 100644 --- a/go/vt/discovery/healthcheck_test.go +++ b/go/vt/discovery/healthcheck_test.go @@ -23,6 +23,7 @@ import ( "io" "strings" "sync" + "sync/atomic" "testing" "time" @@ -1490,10 +1491,10 @@ func TestConcurrentUpdates(t *testing.T) { // Subscribe to the healthcheck // Make the receiver keep track of the updates received. ch := hc.Subscribe() - totalCount := 0 + var totalCount atomic.Int32 go func() { for range ch { - totalCount++ + totalCount.Add(1) // Simulate a somewhat slow consumer. time.Sleep(100 * time.Millisecond) } @@ -1510,7 +1511,7 @@ func TestConcurrentUpdates(t *testing.T) { hc.Unsubscribe(ch) defer close(ch) require.Eventuallyf(t, func() bool { - return totalUpdates == totalCount + return totalUpdates == int(totalCount.Load()) }, 5*time.Second, 100*time.Millisecond, "expected all updates to be processed") }