Skip to content

Commit

Permalink
Fix test TestOfferUpdatedVersion (#279)
Browse files Browse the repository at this point in the history
The test sometimes fails on my machine. The EventuallyExpect function
needs a condition that returns false instead of failing when the
expected message is not received, since a different message may be
sent by the client.

This fixes the test. I run it 100 times and it passes:
`go test -run TestOfferUpdatedVersion/ws -v -race -count 100`
  • Loading branch information
tigrannajaryan committed May 27, 2024
1 parent f299bc3 commit 3a82923
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions client/clientimpl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ func TestSetEffectiveConfig(t *testing.T) {
t,
func() bool {
return rcvConfig.Load() != nil &&
proto.Equal(sendConfig, rcvConfig.Load().(*protobufs.EffectiveConfig))
proto.Equal(sendConfig, rcvConfig.Load().(*protobufs.EffectiveConfig))
},
)

Expand All @@ -546,7 +546,7 @@ func TestSetEffectiveConfig(t *testing.T) {
t,
func() bool {
return rcvConfig.Load() != nil &&
proto.Equal(sendConfig, rcvConfig.Load().(*protobufs.EffectiveConfig))
proto.Equal(sendConfig, rcvConfig.Load().(*protobufs.EffectiveConfig))
},
)

Expand Down Expand Up @@ -765,18 +765,18 @@ func TestServerOfferConnectionSettings(t *testing.T) {
},

OnOpampConnectionSettingsFunc: func(
ctx context.Context, settings *protobufs.OpAMPConnectionSettings,
ctx context.Context, settings *protobufs.OpAMPConnectionSettings,
) error {
assert.True(t, proto.Equal(opampSettings, settings))
atomic.AddInt64(&gotOpampSettings, 1)
return nil
},
},
Capabilities: protobufs.AgentCapabilities_AgentCapabilities_ReportsOwnTraces |
protobufs.AgentCapabilities_AgentCapabilities_ReportsOwnMetrics |
protobufs.AgentCapabilities_AgentCapabilities_ReportsOwnLogs |
protobufs.AgentCapabilities_AgentCapabilities_AcceptsOtherConnectionSettings |
protobufs.AgentCapabilities_AgentCapabilities_AcceptsOpAMPConnectionSettings,
protobufs.AgentCapabilities_AgentCapabilities_ReportsOwnMetrics |
protobufs.AgentCapabilities_AgentCapabilities_ReportsOwnLogs |
protobufs.AgentCapabilities_AgentCapabilities_AcceptsOtherConnectionSettings |
protobufs.AgentCapabilities_AgentCapabilities_AcceptsOpAMPConnectionSettings,
}
settings.OpAMPServerURL = "ws://" + srv.Endpoint
prepareClient(t, &settings, client)
Expand Down Expand Up @@ -823,7 +823,7 @@ func TestClientRequestConnectionSettings(t *testing.T) {
settings := types.StartSettings{
Callbacks: types.CallbacksStruct{
OnOpampConnectionSettingsFunc: func(
ctx context.Context, settings *protobufs.OpAMPConnectionSettings,
ctx context.Context, settings *protobufs.OpAMPConnectionSettings,
) error {
assert.True(t, proto.Equal(opampSettings, settings))
atomic.AddInt64(&clientGotOpampSettings, 1)
Expand Down Expand Up @@ -929,7 +929,7 @@ func TestReportAgentHealth(t *testing.T) {
settings := types.StartSettings{
OpAMPServerURL: "ws://" + srv.Endpoint,
Capabilities: protobufs.AgentCapabilities_AgentCapabilities_ReportsEffectiveConfig |
protobufs.AgentCapabilities_AgentCapabilities_ReportsHealth,
protobufs.AgentCapabilities_AgentCapabilities_ReportsHealth,
}
prepareClient(t, &settings, client)

Expand Down Expand Up @@ -1090,7 +1090,7 @@ func verifyRemoteConfigUpdate(t *testing.T, successCase bool, expectStatus *prot
},
},
Capabilities: protobufs.AgentCapabilities_AgentCapabilities_AcceptsRemoteConfig |
protobufs.AgentCapabilities_AgentCapabilities_ReportsRemoteConfig,
protobufs.AgentCapabilities_AgentCapabilities_ReportsRemoteConfig,
}
prepareClient(t, &settings, client)

Expand Down Expand Up @@ -1208,12 +1208,15 @@ type packageTestCase struct {
const packageUpdateErrorMsg = "cannot update packages"

func assertPackageStatus(t *testing.T,
testCase packageTestCase,
msg *protobufs.AgentToServer) (*protobufs.ServerToAgent, bool) {
testCase packageTestCase,
msg *protobufs.AgentToServer) (*protobufs.ServerToAgent, bool) {
expectedStatusReceived := false

status := msg.PackageStatuses
require.NotNil(t, status)
if status == nil {
// PackageStatuses is not yet reported, keep waiting.
return nil, false
}
assert.EqualValues(t, testCase.expectedStatus.ServerProvidedAllPackagesHash, status.ServerProvidedAllPackagesHash)

if testCase.expectedError != "" {
Expand Down Expand Up @@ -1286,7 +1289,7 @@ func verifyUpdatePackages(t *testing.T, testCase packageTestCase) {
},
PackagesStateProvider: localPackageState,
Capabilities: protobufs.AgentCapabilities_AgentCapabilities_AcceptsPackages |
protobufs.AgentCapabilities_AgentCapabilities_ReportsPackageStatuses,
protobufs.AgentCapabilities_AgentCapabilities_ReportsPackageStatuses,
}
prepareClient(t, &settings, client)

Expand All @@ -1309,7 +1312,7 @@ func verifyUpdatePackages(t *testing.T, testCase packageTestCase) {
// ---> Server
// Wait for the expected package statuses to be received.
srv.EventuallyExpect("full PackageStatuses", func(msg *protobufs.AgentToServer) (*protobufs.ServerToAgent,
bool) {
bool) {
return assertPackageStatus(t, testCase, msg)
})

Expand Down Expand Up @@ -1470,7 +1473,7 @@ func TestMissingCapabilities(t *testing.T) {
assert.Nil(t, msg.PackagesAvailable)
},
OnOpampConnectionSettingsFunc: func(
ctx context.Context, settings *protobufs.OpAMPConnectionSettings,
ctx context.Context, settings *protobufs.OpAMPConnectionSettings,
) error {
assert.Fail(t, "should not be called since capability is not set to accept it")
return nil
Expand Down Expand Up @@ -1532,7 +1535,7 @@ func TestMissingPackagesStateProvider(t *testing.T) {
settings := types.StartSettings{
Callbacks: types.CallbacksStruct{},
Capabilities: protobufs.AgentCapabilities_AgentCapabilities_AcceptsPackages |
protobufs.AgentCapabilities_AgentCapabilities_ReportsPackageStatuses,
protobufs.AgentCapabilities_AgentCapabilities_ReportsPackageStatuses,
}
prepareClient(t, &settings, client)

Expand Down Expand Up @@ -1590,7 +1593,7 @@ func TestOfferUpdatedVersion(t *testing.T) {
},
PackagesStateProvider: localPackageState,
Capabilities: protobufs.AgentCapabilities_AgentCapabilities_AcceptsPackages |
protobufs.AgentCapabilities_AgentCapabilities_ReportsPackageStatuses,
protobufs.AgentCapabilities_AgentCapabilities_ReportsPackageStatuses,
}
prepareClient(t, &settings, client)

Expand All @@ -1612,7 +1615,7 @@ func TestOfferUpdatedVersion(t *testing.T) {
// ---> Server
// Wait for the expected package statuses to be received.
srv.EventuallyExpect("full PackageStatuses", func(msg *protobufs.AgentToServer) (*protobufs.ServerToAgent,
bool) {
bool) {
return assertPackageStatus(t, testCase, msg)
})

Expand All @@ -1639,7 +1642,7 @@ func TestOfferUpdatedVersion(t *testing.T) {
// ---> Server
// Wait for the expected package statuses to be received.
srv.EventuallyExpect("full PackageStatuses updated version", func(msg *protobufs.AgentToServer) (*protobufs.ServerToAgent,
bool) {
bool) {
return assertPackageStatus(t, testCase, msg)
})

Expand Down

0 comments on commit 3a82923

Please sign in to comment.