98
}
99
100
>
func getContextState(tb testing.TB, timeout time.Duration) *contextState {
context.go
101
>
tb.Helper()
102
>
103
>
testContexts.Lock()
104
>
defer testContexts.Unlock()
105
>
106
>
if st, ok := testContexts.byTest[tb]; ok {
107
return st
108
}
109
110
>
ctx, cancel := context.WithTimeout(tb.Context(), timeout)
context.go
111
>
112
>
// Annotate gRPC requests with the test name for OTEL tracing.
113
>
ctx = metadata.AppendToOutgoingContext(ctx, testNameMetadataKey, tb.Name())
114
>
115
>
st := &contextState{
116
>
ctx: ctx,
117
>
cancel: cancel,
118
>
timeout: timeout,
119
>
decorators: make(map[any]struct{}),
120
>
}
121
>
testContexts.byTest[tb] = st
122
>
123
>
tb.Cleanup(func() {
124
>
err := st.err()
125
>
st.cancel()
126
>
testContexts.Lock()
127
>
delete(testContexts.byTest, tb)
128
>
testContexts.Unlock()
129
>
if err == context.DeadlineExceeded {
130
tb.Errorf("test exceeded timeout of %v", st.timeout)
131
}
133
})
135
}
136
137
>
func (s *contextState) configure(tb testing.TB, cfg config) {
context.go
138
>
tb.Helper()
139
>
140
>
s.mu.Lock()
141
>
defer s.mu.Unlock()
142
>
143
>
if cfg.timeoutSet && cfg.timeout != s.timeout {
144
tb.Fatalf("testcontext: test context already exists with timeout %v; cannot change it to %v", s.timeout, cfg.timeout)
145
}