Files
kubernetes/test/utils/ktesting/contexthelper_test.go
Patrick Ohly 3b63fe83a0 ktesting: support cancellation after WithoutCancel
Not canceling the parent context made sense, but the new context should
be cancelable like any other TContext. Found when passing tCtx.WithoutCancel()
to StartTestServer and the tear-down function got stuck because it couldn't
cancel the context.
2026-03-17 07:50:18 +01:00

173 lines
5.2 KiB
Go

/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package ktesting
import (
"context"
"errors"
"testing"
"testing/synctest"
"time"
"github.com/onsi/gomega"
"github.com/stretchr/testify/assert"
)
func TestCleanupErr(t *testing.T) {
actual := cleanupErr(t.Name())
if !errors.Is(actual, context.Canceled) {
t.Errorf("cleanupErr %T should be a %T", actual, context.Canceled)
}
}
func TestCause(t *testing.T) {
timeoutCause := canceledError("I timed out")
parentCause := errors.New("parent canceled")
contextBackground := func(t *testing.T) context.Context {
return context.Background()
}
for name, tt := range map[string]struct {
parentCtx func(t *testing.T) context.Context
timeout time.Duration
sleep time.Duration
cancelCause string
expectErr, expectCause error
expectDeadline time.Duration
}{
"nothing": {
parentCtx: contextBackground,
timeout: 5 * time.Millisecond,
sleep: time.Millisecond,
},
"timeout": {
parentCtx: contextBackground,
timeout: time.Millisecond,
sleep: 5 * time.Millisecond,
expectErr: context.Canceled,
expectCause: canceledError(timeoutCause),
},
"parent-canceled": {
parentCtx: func(t *testing.T) context.Context {
ctx, cancel := context.WithCancel(context.Background())
cancel()
return ctx
},
timeout: time.Millisecond,
sleep: 5 * time.Millisecond,
expectErr: context.Canceled,
expectCause: context.Canceled,
},
"parent-cause": {
parentCtx: func(t *testing.T) context.Context {
ctx, cancel := context.WithCancelCause(context.Background())
cancel(parentCause)
return ctx
},
timeout: time.Millisecond,
sleep: 5 * time.Millisecond,
expectErr: context.Canceled,
expectCause: parentCause,
},
"deadline-no-parent": {
parentCtx: contextBackground,
timeout: time.Minute,
expectDeadline: time.Minute,
},
"deadline-parent": {
parentCtx: func(t *testing.T) context.Context {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
t.Cleanup(cancel)
return ctx
},
timeout: 2 * time.Minute,
expectDeadline: time.Minute,
},
"deadline-child": {
parentCtx: func(t *testing.T) context.Context {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
t.Cleanup(cancel)
return ctx
},
timeout: time.Minute,
expectDeadline: time.Minute,
},
} {
t.Run(name, func(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
ctx, cancel := withTimeout(tt.parentCtx(t), t, tt.timeout, timeoutCause.Error())
if tt.cancelCause != "" {
cancel(tt.cancelCause)
}
if tt.expectDeadline != 0 {
actualDeadline, ok := ctx.Deadline()
if assert.True(t, ok, "should have had a deadline") {
assert.Equal(t, tt.expectDeadline, time.Until(actualDeadline), "remaining time till Deadline()")
}
}
// Unblock background goroutines.
time.Sleep(tt.sleep)
// Wait for them to do their work.
synctest.Wait()
// Now check.
actualErr := ctx.Err()
actualCause := context.Cause(ctx)
assert.Equal(t, tt.expectErr, actualErr, "ctx.Err()")
assert.Equal(t, tt.expectCause, actualCause, "context.Cause()")
})
})
}
}
// 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")))
}