2
0
mirror of https://github.com/lightninglabs/loop synced 2024-11-09 19:10:47 +00:00
loop/test/timeout.go

57 lines
966 B
Go
Raw Normal View History

2019-03-06 20:13:50 +00:00
package test
import (
"os"
"runtime/pprof"
"testing"
"time"
"github.com/fortytw2/leaktest"
)
2024-07-31 17:07:51 +00:00
// GuardConfig stores options for Guard function.
type GuardConfig struct {
timeout time.Duration
}
// GuardOption is an option for Guard function.
type GuardOption func(*GuardConfig)
// WithGuardTimeout sets timeout for the guard. Default is 5s.
func WithGuardTimeout(timeout time.Duration) GuardOption {
return func(c *GuardConfig) {
c.timeout = timeout
}
}
2019-03-06 20:13:50 +00:00
// Guard implements a test level timeout.
2024-07-31 17:07:51 +00:00
func Guard(t *testing.T, opts ...GuardOption) func() {
cfg := GuardConfig{
timeout: 5 * time.Second,
}
for _, opt := range opts {
opt(&cfg)
}
2019-03-06 20:13:50 +00:00
done := make(chan struct{})
go func() {
select {
2024-07-31 17:07:51 +00:00
case <-time.After(cfg.timeout):
2019-10-07 15:29:24 +00:00
err := pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
if err != nil {
panic(err)
}
2019-03-06 20:13:50 +00:00
panic("test timeout")
case <-done:
}
}()
fn := leaktest.Check(t)
return func() {
close(done)
fn()
}
}