stackeval: inPromisingTask cancels context when test completes

This is just some extra insurance to reduce the risk of us leaving things
dangling after a test terminates. Although tests should ideally not rely
on this and should terminate background work properly as part of the test,
this should catch some things that might get left behind if a test ends
in an ungraceful way.
pull/34738/head
Martin Atkins 3 years ago
parent 94ca9bc613
commit 1872c9d9be

@ -200,8 +200,18 @@ func assertMatchingDiag(t *testing.T, diags tfdiags.Diagnostics, check func(diag
// halt the test with an error message.
func inPromisingTask(t *testing.T, f func(ctx context.Context, t *testing.T)) {
t.Helper()
_, err := promising.MainTask(context.Background(), func(ctx context.Context) (struct{}, error) {
// We'll introduce an extra cancellable context here just to make
// sure everything descending from this task gets terminated promptly
// after the test is complete.
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(func() {
cancel()
})
_, err := promising.MainTask(ctx, func(ctx context.Context) (struct{}, error) {
t.Helper()
f(ctx, t)
return struct{}{}, nil
})

Loading…
Cancel
Save