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

Ensure client includes AgentDescription on Reconnect #63

Merged
merged 2 commits into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
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
35 changes: 19 additions & 16 deletions client/clientimpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,22 +106,6 @@ func (w *client) Start(settings StartSettings) error {
w.requestHeader["Authorization"] = []string{w.settings.AuthorizationHeader}
}

// Prepare the first status report.
w.sender.UpdateNextStatus(
func(statusReport *protobufs.StatusReport) {
statusReport.AgentDescription = w.settings.AgentDescription
},
)

w.sender.UpdateNextMessage(
func(msg *protobufs.AgentToServer) {
if msg.AddonStatuses == nil {
msg.AddonStatuses = &protobufs.AgentAddonStatuses{}
}
msg.AddonStatuses.ServerProvidedAllAddonsHash = w.settings.LastServerProvidedAllAddonsHash
},
)

w.startConnectAndRun()

w.isStarted = true
Expand Down Expand Up @@ -168,6 +152,9 @@ func (w *client) SetAgentDescription(descr *protobufs.AgentDescription) error {
return errAgentDescriptionMissing
}

// store the agent description to send on reconnect
w.settings.AgentDescription = descr

w.sender.UpdateNextStatus(func(statusReport *protobufs.StatusReport) {
statusReport.AgentDescription = descr
})
Expand Down Expand Up @@ -325,6 +312,22 @@ func (w *client) runOneCycle(ctx context.Context) {
// Create a cancellable context for background processors.
procCtx, procCancel := context.WithCancel(ctx)

// Prepare the first status report.
w.sender.UpdateNextStatus(
func(statusReport *protobufs.StatusReport) {
statusReport.AgentDescription = w.settings.AgentDescription
},
)

w.sender.UpdateNextMessage(
func(msg *protobufs.AgentToServer) {
if msg.AddonStatuses == nil {
msg.AddonStatuses = &protobufs.AgentAddonStatuses{}
}
msg.AddonStatuses.ServerProvidedAllAddonsHash = w.settings.LastServerProvidedAllAddonsHash
},
)

// Connected successfully. Start the sender. This will also send the first
// status report.
if err := w.sender.Start(procCtx, w.settings.InstanceUid, w.conn); err != nil {
Expand Down
48 changes: 48 additions & 0 deletions client/clientimpl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,54 @@ func TestFirstStatusReport(t *testing.T) {
assert.NoError(t, err)
}

func TestIncludesDetailsOnReconnect(t *testing.T) {
srv := internal.StartMockServer(t)

var receivedDetails int64
srv.OnMessage = func(msg *protobufs.AgentToServer) *protobufs.ServerToAgent {
// Track when we receive AgentDescription
if msg.StatusReport.AgentDescription != nil {
atomic.AddInt64(&receivedDetails, 1)
}

return &protobufs.ServerToAgent{
InstanceUid: msg.InstanceUid,
}
}

var connected int64
settings := StartSettings{
Callbacks: types.CallbacksStruct{
OnConnectFunc: func() {
atomic.AddInt64(&connected, 1)
},
OnRemoteConfigFunc: func(
ctx context.Context,
config *protobufs.AgentRemoteConfig,
) (effectiveConfig *protobufs.EffectiveConfig, configChanged bool, err error) {
return &protobufs.EffectiveConfig{}, false, nil
},
},
AgentDescription: &protobufs.AgentDescription{},
}

settings.OpAMPServerURL = "ws://" + srv.Endpoint
client := startClient(t, settings)

eventually(t, func() bool { return atomic.LoadInt64(&connected) == 1 })
eventually(t, func() bool { return atomic.LoadInt64(&receivedDetails) == 1 })

// close the agent connection. expect it to reconnect and send details again.
err := client.conn.Close()
assert.NoError(t, err)

eventually(t, func() bool { return atomic.LoadInt64(&connected) == 2 })
eventually(t, func() bool { return atomic.LoadInt64(&receivedDetails) == 2 })

err = client.Stop(context.Background())
assert.NoError(t, err)
}

func TestSetEffectiveConfig(t *testing.T) {
// Start a server.
srv := internal.StartMockServer(t)
Expand Down