From 738c1772fa3f32017812c0f1219d420b9d8db4a1 Mon Sep 17 00:00:00 2001 From: Aditya Vyas Date: Mon, 13 Jan 2025 16:49:48 -0500 Subject: [PATCH] fix data race - 2 --- internal/services/rpc_service_test.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/internal/services/rpc_service_test.go b/internal/services/rpc_service_test.go index 584f138..5e36e09 100644 --- a/internal/services/rpc_service_test.go +++ b/internal/services/rpc_service_test.go @@ -8,7 +8,6 @@ import ( "fmt" "io" "net/http" - "os" "strings" "testing" "time" @@ -420,9 +419,7 @@ func TestTrackRPCServiceHealth_HealthyService(t *testing.T) { } func TestTrackRPCServiceHealth_UnhealthyService(t *testing.T) { - var logBuffer bytes.Buffer - log.DefaultLogger.SetOutput(&logBuffer) - defer log.DefaultLogger.SetOutput(os.Stderr) + getLogs := log.DefaultLogger.StartTest(log.WarnLevel) mockHTTPClient := &utils.MockHTTPClient{} rpcURL := "http://test-url-track-rpc-service-health" @@ -450,8 +447,14 @@ func TestTrackRPCServiceHealth_UnhealthyService(t *testing.T) { // Wait long enough for warning to trigger time.Sleep(65 * time.Second) - logOutput := logBuffer.String() - assert.Contains(t, logOutput, "rpc service unhealthy for over 1m0s") + entries := getLogs() + testFailed := true + for _, entry := range entries { + if strings.Contains(entry.Message, "rpc service unhealthy for over 1m0s") { + testFailed = false + } + } + assert.False(t, testFailed) mockHTTPClient.AssertExpectations(t) } @@ -465,9 +468,6 @@ func TestTrackRPCService_ContextCancelled(t *testing.T) { go rpcService.TrackRPCServiceHealth(ctx) - // Cancel context immediately - cancel() - // Verify channel is closed after context cancellation time.Sleep(100 * time.Millisecond) _, ok := <-rpcService.GetHeartbeatChannel()