kubernetes/test/utils/ktesting/contexthelper_test.go
Patrick Ohly 620c1b6305 ktesting: fix potential unit test flake
I've not been able to trigger the flake, but it could happen:
- time.Sleep unblocks some background goroutines inside the synctest bubble.
- Those goroutines do not actually run yet.
- The main test checks for the result of those goroutines.

Adding a `synctest.Wait` ensures that all background processing is complete
because it waits for all goroutines to be durably blocked.
2026-02-26 08:45:35 +01:00

134 lines
3.7 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/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()")
})
})
}
}