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.
This commit is contained in:
Mathias Fredriksson 2023-11-07 16:47:23 +02:00 committed by GitHub
parent b0aa91bf27
commit e36503afd2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 5 deletions

View File

@ -338,9 +338,12 @@ func TestStartupLogsSender(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
defer cancel()
var want, got []agentsdk.Log
patchLogs := func(_ context.Context, req agentsdk.PatchLogs) error {
got = append(got, req.Logs...)
patchStart := make(chan struct{})
patchDone := make(chan struct{})
patchLogs := func(ctx context.Context, _ agentsdk.PatchLogs) error {
close(patchStart)
<-ctx.Done()
close(patchDone)
return nil
}
@ -358,10 +361,14 @@ func TestStartupLogsSender(t *testing.T) {
})
require.NoError(t, err)
cancel()
go func() {
<-patchStart
cancel()
}()
err = flushAndClose(ctx)
require.Error(t, err)
require.Equal(t, want, got)
<-patchDone
// The patch request should have been canceled if it was active.
})
}