mirror of
https://github.com/kubernetes/kubernetes.git
synced 2026-05-23 10:29:27 -04:00
The TestCause tests were already unreliable in the CI. The others failed under stress. As synctest we have to be more careful how to construct and clean up the parent context for TestCause (must happen inside bubble), but once that's handled we can reliably measure the (fake) time and compare exactly against expected results.
130 lines
3.6 KiB
Go
130 lines
3.6 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()")
|
|
}
|
|
}
|
|
time.Sleep(tt.sleep)
|
|
actualErr := ctx.Err()
|
|
actualCause := context.Cause(ctx)
|
|
assert.Equal(t, tt.expectErr, actualErr, "ctx.Err()")
|
|
assert.Equal(t, tt.expectCause, actualCause, "context.Cause()")
|
|
})
|
|
})
|
|
}
|
|
}
|