-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.go
More file actions
36 lines (28 loc) · 1.22 KB
/
context.go
File metadata and controls
36 lines (28 loc) · 1.22 KB
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
package test
import (
"context"
"time"
)
// Context returns a context.Context derived from the test's context.
//
// This function extracts the context from the TestingT and handles timeout management:
// - If the test has a deadline, it adjusts the deadline to allow for clean shutdown
// - It reserves a small portion of time before the test deadline to allow for proper cleanup
// - The returned context will be automatically canceled when the test completes
func Context(t TestingT) context.Context {
ctx := t.Context()
if deadline, isset := t.Deadline(); isset {
// deadline is in the future, timeout duration is expected to be negative
panicTimeout := -time.Since(deadline)
// because test panics after deadline, we anticipate 1% of timeout duration
// to properly quit, it helps to have test failure messages instead of stack traces
// we don't need to reserve much time, one second should be more than enough
cleanDuration := min(time.Duration(int64(float64(panicTimeout)*0.01)), time.Second)
// add negative value, meaning subtract time to deadline
deadline = deadline.Add(-cleanDuration)
var cancel context.CancelFunc
ctx, cancel = context.WithDeadline(ctx, deadline)
t.Cleanup(cancel)
}
return ctx
}