From 2a3dfaa4fd1bb0e0da48801b37dda570763dc1aa Mon Sep 17 00:00:00 2001 From: Shaunak Kashyap Date: Thu, 20 Jul 2023 12:28:21 -0700 Subject: [PATCH] Fix data race in test (#3111) --- .../actions/handlers/handler_action_unenroll_test.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/internal/pkg/agent/application/actions/handlers/handler_action_unenroll_test.go b/internal/pkg/agent/application/actions/handlers/handler_action_unenroll_test.go index 61e4ddfc9bb..e28612e9502 100644 --- a/internal/pkg/agent/application/actions/handlers/handler_action_unenroll_test.go +++ b/internal/pkg/agent/application/actions/handlers/handler_action_unenroll_test.go @@ -8,6 +8,8 @@ import ( "context" "testing" + "github.com/elastic/elastic-agent-libs/atomic" + "github.com/elastic/elastic-agent-client/v7/pkg/client" "github.com/elastic/elastic-agent-client/v7/pkg/proto" "github.com/elastic/elastic-agent/internal/pkg/agent/application/coordinator" @@ -41,7 +43,7 @@ func makeComponentState(name string, proxiedActions []string) runtime.ComponentC type MockActionCoordinator struct { st coordinator.State - performedActions int + performedActions atomic.Int } func (c *MockActionCoordinator) State() coordinator.State { @@ -49,12 +51,12 @@ func (c *MockActionCoordinator) State() coordinator.State { } func (c *MockActionCoordinator) PerformAction(ctx context.Context, comp component.Component, unit component.Unit, name string, params map[string]interface{}) (map[string]interface{}, error) { - c.performedActions++ + c.performedActions.Inc() return nil, nil } func (c *MockActionCoordinator) Clear() { - c.performedActions = 0 + c.performedActions.Store(0) } type MockAcker struct { @@ -205,7 +207,7 @@ func TestActionUnenrollHandler(t *testing.T) { if tc.wantErr == nil { require.Len(t, acker.Acked, 1) } - require.Equal(t, tc.wantPerformedActions, coord.performedActions) + require.Equal(t, tc.wantPerformedActions, coord.performedActions.Load()) }) } }