diff --git a/test/utils/ktesting/contexthelper_test.go b/test/utils/ktesting/contexthelper_test.go index 6ed9c5ce52c..5d785628912 100644 --- a/test/utils/ktesting/contexthelper_test.go +++ b/test/utils/ktesting/contexthelper_test.go @@ -23,6 +23,7 @@ import ( "testing/synctest" "time" + "github.com/onsi/gomega" "github.com/stretchr/testify/assert" ) @@ -132,3 +133,40 @@ func TestCause(t *testing.T) { }) } } + +// TestCancel checks how cancellation propagates or doesn't propagate +// when setting up child contexts through WithCancel or WithoutCancel. +func TestCancel(t *testing.T) { + tCtx := Init(t) + tCtx2 := tCtx.WithoutCancel() + tCtx3 := tCtx.WithoutCancel() + tCtx4 := tCtx.WithCancel() + tCtx5 := tCtx.WithCancel() + + tCtx.AssertNoError(tCtx.Err()) + tCtx.AssertNoError(tCtx2.Err()) + tCtx.AssertNoError(tCtx3.Err()) + tCtx.AssertNoError(tCtx4.Err()) + tCtx.AssertNoError(tCtx5.Err()) + + tCtx2.Cancel("cancel 2") + tCtx.AssertNoError(tCtx.Err()) + tCtx.Assert(context.Cause(tCtx2)).To(gomega.MatchError(gomega.ContainSubstring("cancel 2"))) + tCtx.AssertNoError(tCtx3.Err()) + tCtx.AssertNoError(tCtx4.Err()) + tCtx.AssertNoError(tCtx5.Err()) + + tCtx4.Cancel("cancel 4") + tCtx.AssertNoError(tCtx.Err()) + tCtx.Assert(context.Cause(tCtx2)).To(gomega.MatchError(gomega.ContainSubstring("cancel 2"))) + tCtx.AssertNoError(tCtx3.Err()) + tCtx.Assert(context.Cause(tCtx4)).To(gomega.MatchError(gomega.ContainSubstring("cancel 4"))) + tCtx.AssertNoError(tCtx5.Err()) + + tCtx.Cancel("cancel root") + tCtx.Assert(context.Cause(tCtx)).To(gomega.MatchError(gomega.ContainSubstring("cancel root"))) + tCtx.Assert(context.Cause(tCtx2)).To(gomega.MatchError(gomega.ContainSubstring("cancel 2"))) + tCtx.AssertNoError(tCtx3.Err()) + tCtx.Assert(context.Cause(tCtx4)).To(gomega.MatchError(gomega.ContainSubstring("cancel 4"))) + tCtx.Assert(context.Cause(tCtx5)).To(gomega.MatchError(gomega.ContainSubstring("cancel root"))) +} diff --git a/test/utils/ktesting/withcontext.go b/test/utils/ktesting/withcontext.go index 7a192748fd7..adfb7a13a8c 100644 --- a/test/utils/ktesting/withcontext.go +++ b/test/utils/ktesting/withcontext.go @@ -41,13 +41,13 @@ func (tCtx TContext) WithCancel() TContext { } // WithoutCancel causes the returned context to ignore cancellation of its parent. -// Calling Cancel will not cancel the parent either. +// Calling Cancel will only cancel the new context. // This matches [context.WithoutCancel]. func (tCtx TContext) WithoutCancel() TContext { ctx := context.WithoutCancel(tCtx) tCtx.Context = ctx - tCtx.cancel = nil + tCtx = tCtx.WithCancel() // Re-create a cancelable TContext. return tCtx }