Skip to content

Commit e36503a

Browse files
authored
test(codersdk/agentsdk): fix context cancel flush test (#10560)
This change tests that the patch request is cancelled instead of hoping that there's no race between context cancellations leading to patch never being called.
1 parent b0aa91b commit e36503a

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

codersdk/agentsdk/logs_test.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,12 @@ func TestStartupLogsSender(t *testing.T) {
338338
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
339339
defer cancel()
340340

341-
var want, got []agentsdk.Log
342-
patchLogs := func(_ context.Context, req agentsdk.PatchLogs) error {
343-
got = append(got, req.Logs...)
341+
patchStart := make(chan struct{})
342+
patchDone := make(chan struct{})
343+
patchLogs := func(ctx context.Context, _ agentsdk.PatchLogs) error {
344+
close(patchStart)
345+
<-ctx.Done()
346+
close(patchDone)
344347
return nil
345348
}
346349

@@ -358,10 +361,14 @@ func TestStartupLogsSender(t *testing.T) {
358361
})
359362
require.NoError(t, err)
360363

361-
cancel()
364+
go func() {
365+
<-patchStart
366+
cancel()
367+
}()
362368
err = flushAndClose(ctx)
363369
require.Error(t, err)
364370

365-
require.Equal(t, want, got)
371+
<-patchDone
372+
// The patch request should have been canceled if it was active.
366373
})
367374
}

0 commit comments

Comments
 (0)