Skip to content

Commit 70c5c47

Browse files
authored
fix: stop blocking fake Agent API channel writes after context expires (coder#13908)
1 parent 1f24ace commit 70c5c47

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

agent/agenttest/client.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,12 @@ func (f *FakeAgentAPI) UpdateStats(ctx context.Context, req *agentproto.UpdateSt
210210
f.logger.Debug(ctx, "update stats called", slog.F("req", req))
211211
// empty request is sent to get the interval; but our tests don't want empty stats requests
212212
if req.Stats != nil {
213-
f.statsCh <- req.Stats
213+
select {
214+
case <-ctx.Done():
215+
return nil, ctx.Err()
216+
case f.statsCh <- req.Stats:
217+
// OK!
218+
}
214219
}
215220
return &agentproto.UpdateStatsResponse{ReportInterval: durationpb.New(statsInterval)}, nil
216221
}
@@ -233,17 +238,25 @@ func (f *FakeAgentAPI) UpdateLifecycle(_ context.Context, req *agentproto.Update
233238

234239
func (f *FakeAgentAPI) BatchUpdateAppHealths(ctx context.Context, req *agentproto.BatchUpdateAppHealthRequest) (*agentproto.BatchUpdateAppHealthResponse, error) {
235240
f.logger.Debug(ctx, "batch update app health", slog.F("req", req))
236-
f.appHealthCh <- req
237-
return &agentproto.BatchUpdateAppHealthResponse{}, nil
241+
select {
242+
case <-ctx.Done():
243+
return nil, ctx.Err()
244+
case f.appHealthCh <- req:
245+
return &agentproto.BatchUpdateAppHealthResponse{}, nil
246+
}
238247
}
239248

240249
func (f *FakeAgentAPI) AppHealthCh() <-chan *agentproto.BatchUpdateAppHealthRequest {
241250
return f.appHealthCh
242251
}
243252

244-
func (f *FakeAgentAPI) UpdateStartup(_ context.Context, req *agentproto.UpdateStartupRequest) (*agentproto.Startup, error) {
245-
f.startupCh <- req.GetStartup()
246-
return req.GetStartup(), nil
253+
func (f *FakeAgentAPI) UpdateStartup(ctx context.Context, req *agentproto.UpdateStartupRequest) (*agentproto.Startup, error) {
254+
select {
255+
case <-ctx.Done():
256+
return nil, ctx.Err()
257+
case f.startupCh <- req.GetStartup():
258+
return req.GetStartup(), nil
259+
}
247260
}
248261

249262
func (f *FakeAgentAPI) GetMetadata() map[string]agentsdk.Metadata {

0 commit comments

Comments
 (0)