Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix race detector errors in TestCoordinatorDiagnosticHooks #2830

Merged
merged 1 commit into from
Jun 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions internal/pkg/agent/application/coordinator/diagnostics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"gopkg.in/yaml.v2"

"github.com/elastic/elastic-agent-client/v7/pkg/client"
"github.com/elastic/elastic-agent-libs/atomic"
"github.com/elastic/elastic-agent-libs/logp"
"github.com/elastic/elastic-agent-libs/mapstr"
"github.com/elastic/elastic-agent/internal/pkg/agent/application/coordinator"
Expand Down Expand Up @@ -248,13 +249,30 @@ func TestCoordinatorDiagnosticHooks(t *testing.T) {

initialConf := config.MustNewConfigFrom(configBytes)

// These flags are set in callbacks from the Coordinator goroutine
// when the mocked functions are invoked, so we can tell in the
// assertions below when it's safe to advance to the next stage of
// the test.
configCalled := atomic.NewBool(false)
ackCalled := atomic.NewBool(false)

initialConfChange := mocks.NewConfigChange(t)
initialConfChange.EXPECT().Config().Return(initialConf)
initialConfChange.EXPECT().Ack().Return(nil).Times(1)
initialConfChange.EXPECT().Config().RunAndReturn(func() *config.Config {
configCalled.Store(true)
return initialConf
})
initialConfChange.EXPECT().Ack().RunAndReturn(func() error {
ackCalled.Store(true)
return nil
}).Times(1)
mustWriteToChannelBeforeTimeout[coordinator.ConfigChange](t, initialConfChange, helper.configChangeChannel, 100*time.Millisecond)

assert.Eventually(t, func() bool { return sut.State().State == cproto.State_HEALTHY }, 1*time.Second, 50*time.Millisecond)
assert.Eventually(t, func() bool { return len(initialConfChange.Calls) > 1 /*both Config and Ack have been called)*/ }, 1*time.Second, 50*time.Millisecond)
assert.Eventually(t, func() bool {
return sut.State().State == cproto.State_HEALTHY
}, 1*time.Second, 50*time.Millisecond)
assert.Eventually(t, func() bool {
return configCalled.Load() && ackCalled.Load()
}, 1*time.Second, 50*time.Millisecond)
t.Logf("Agent state: %s", sut.State().State)

// Send runtime component state
Expand Down