From ffce99c4d958d02071cfc8d4f745edabe17f8159 Mon Sep 17 00:00:00 2001 From: haoqixu Date: Sat, 2 Dec 2023 01:07:33 +0800 Subject: [PATCH 1/2] fix flaky TestAgentIdentification/ws --- client/clientimpl_test.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/client/clientimpl_test.go b/client/clientimpl_test.go index 97927fc8..d8dd3dc6 100644 --- a/client/clientimpl_test.go +++ b/client/clientimpl_test.go @@ -626,9 +626,9 @@ func TestAgentIdentification(t *testing.T) { newInstanceUid := ulid.MustNew( ulid.Timestamp(time.Now()), ulid.Monotonic(rand.New(rand.NewSource(0)), 0), ) - var rcvAgentInstanceUid atomic.Value + rcvAgentInstanceUids := make(chan string, 10) srv.OnMessage = func(msg *protobufs.AgentToServer) *protobufs.ServerToAgent { - rcvAgentInstanceUid.Store(msg.InstanceUid) + rcvAgentInstanceUids <- msg.InstanceUid return &protobufs.ServerToAgent{ InstanceUid: msg.InstanceUid, AgentIdentification: &protobufs.AgentIdentification{ @@ -649,11 +649,12 @@ func TestAgentIdentification(t *testing.T) { eventually( t, func() bool { - instanceUid, ok := rcvAgentInstanceUid.Load().(string) - if !ok { - return false + select { + case instanceUid := <-rcvAgentInstanceUids: + return instanceUid == oldInstanceUid + default: } - return instanceUid == oldInstanceUid + return false }, ) @@ -665,11 +666,12 @@ func TestAgentIdentification(t *testing.T) { eventually( t, func() bool { - instanceUid, ok := rcvAgentInstanceUid.Load().(string) - if !ok { - return false + select { + case instanceUid := <-rcvAgentInstanceUids: + return instanceUid == newInstanceUid.String() + default: } - return instanceUid == newInstanceUid.String() + return false }, ) From 71b4d406ac2528bfa37650817873f9603942323f Mon Sep 17 00:00:00 2001 From: haoqixu Date: Sat, 2 Dec 2023 01:29:42 +0800 Subject: [PATCH 2/2] fix flaky TestAgentIdentification/ws second eventually assert --- client/clientimpl_test.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/client/clientimpl_test.go b/client/clientimpl_test.go index d8dd3dc6..522c4186 100644 --- a/client/clientimpl_test.go +++ b/client/clientimpl_test.go @@ -638,7 +638,17 @@ func TestAgentIdentification(t *testing.T) { } // Start a client. - settings := types.StartSettings{} + hasRecvServerResponse := make(chan struct{}, 1) + settings := types.StartSettings{ + Callbacks: types.CallbacksStruct{ + OnMessageFunc: func(_ context.Context, _ *types.MessageData) { + select { + case hasRecvServerResponse <- struct{}{}: + default: + } + }, + }, + } settings.OpAMPServerURL = "ws://" + srv.Endpoint prepareClient(t, &settings, client) @@ -658,6 +668,9 @@ func TestAgentIdentification(t *testing.T) { }, ) + // Wait for the server response. + <-hasRecvServerResponse + // Send a dummy message _ = client.SetAgentDescription(createAgentDescr())