Skip to content

Commit d7fa4ab

Browse files
committed
test.Guard: make timeout configurable
1 parent be69653 commit d7fa4ab

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

test/timeout.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,34 @@ import (
99
"github.com/fortytw2/leaktest"
1010
)
1111

12+
// GuardConfig stores options for Guard function.
13+
type GuardConfig struct {
14+
timeout time.Duration
15+
}
16+
17+
// GuardOption is an option for Guard function.
18+
type GuardOption func(*GuardConfig)
19+
20+
// WithGuardTimeout sets timeout for the guard. Default is 5s.
21+
func WithGuardTimeout(timeout time.Duration) GuardOption {
22+
return func(c *GuardConfig) {
23+
c.timeout = timeout
24+
}
25+
}
26+
1227
// Guard implements a test level timeout.
13-
func Guard(t *testing.T) func() {
28+
func Guard(t *testing.T, opts ...GuardOption) func() {
29+
cfg := GuardConfig{
30+
timeout: 5 * time.Second,
31+
}
32+
for _, opt := range opts {
33+
opt(&cfg)
34+
}
35+
1436
done := make(chan struct{})
1537
go func() {
1638
select {
17-
case <-time.After(5 * time.Second):
39+
case <-time.After(cfg.timeout):
1840
err := pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
1941
if err != nil {
2042
panic(err)

0 commit comments

Comments
 (0)