-
Notifications
You must be signed in to change notification settings - Fork 38.7k
/
withcontext.go
123 lines (100 loc) · 3.08 KB
/
withcontext.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
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"
"time"
"github.com/onsi/gomega"
"k8s.io/klog/v2"
)
// WithCancel sets up cancellation in a [TContext.Cleanup] callback and
// constructs a new TContext where [TContext.Cancel] cancels only the new
// context.
func WithCancel(tCtx TContext) TContext {
ctx, cancel := context.WithCancelCause(tCtx)
tCtx.Cleanup(func() {
cancel(cleanupErr(tCtx.Name()))
})
return withContext{
TContext: tCtx,
Context: ctx,
cancel: func(cause string) {
var cancelCause error
if cause != "" {
cancelCause = canceledError(cause)
}
cancel(cancelCause)
},
}
}
// WithTimeout sets up new context with a timeout. Canceling the timeout gets
// registered in a [TContext.Cleanup] callback. [TContext.Cancel] cancels only
// the new context. The cause is used as reason why the context is canceled
// once the timeout is reached. It may be empty, in which case the usual
// "context canceled" error is used.
func WithTimeout(tCtx TContext, timeout time.Duration, timeoutCause string) TContext {
tCtx.Helper()
ctx, cancel := withTimeout(tCtx, tCtx.TB(), timeout, timeoutCause)
return withContext{
TContext: tCtx,
Context: ctx,
cancel: cancel,
}
}
// WithLogger constructs a new context with a different logger.
func WithLogger(tCtx TContext, logger klog.Logger) TContext {
ctx := klog.NewContext(tCtx, logger)
return withContext{
TContext: tCtx,
Context: ctx,
cancel: tCtx.Cancel,
}
}
// withContext combines some TContext with a new [context.Context] derived
// from it. Because both provide the [context.Context] interface, methods must
// be defined which pick the newer one.
type withContext struct {
TContext
context.Context
cancel func(cause string)
}
func (wCtx withContext) Cancel(cause string) {
wCtx.cancel(cause)
}
func (wCtx withContext) CleanupCtx(cb func(TContext)) {
wCtx.Helper()
cleanupCtx(wCtx, cb)
}
func (wCtx withContext) Expect(actual interface{}, extra ...interface{}) gomega.Assertion {
wCtx.Helper()
return expect(wCtx, actual, extra...)
}
func (wCtx withContext) ExpectNoError(err error, explain ...interface{}) {
wCtx.Helper()
expectNoError(wCtx, err, explain...)
}
func (wCtx withContext) Logger() klog.Logger {
return klog.FromContext(wCtx)
}
func (wCtx withContext) Deadline() (time.Time, bool) {
return wCtx.Context.Deadline()
}
func (wCtx withContext) Done() <-chan struct{} {
return wCtx.Context.Done()
}
func (wCtx withContext) Err() error {
return wCtx.Context.Err()
}
func (wCtx withContext) Value(key any) any {
return wCtx.Context.Value(key)
}