From 1872c9d9bea747ff4873ba43ff78efb39db1a150 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Fri, 10 Nov 2023 17:32:53 -0800 Subject: [PATCH] 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. --- .../stackruntime/internal/stackeval/testing_test.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/internal/stacks/stackruntime/internal/stackeval/testing_test.go b/internal/stacks/stackruntime/internal/stackeval/testing_test.go index b391419acd..8390b0f8d9 100644 --- a/internal/stacks/stackruntime/internal/stackeval/testing_test.go +++ b/internal/stacks/stackruntime/internal/stackeval/testing_test.go @@ -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 })