1 // Copyright 2018 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
11 "golang.org/x/tools/internal/stack"
14 //this is only needed to support pre 1.14 when testing.TB did not have Cleanup
15 type withCleanup interface {
19 // the maximum amount of time to wait for goroutines to clean themselves up.
20 const maxWait = time.Second
22 // NoLeak checks that a test (or benchmark) does not leak any goroutines.
23 func NoLeak(t testing.TB) {
24 c, ok := t.(withCleanup)
28 before := stack.Capture()
32 delay := time.Millisecond
34 after := stack.Capture()
35 delta = stack.Diff(before, after)
36 if len(delta.After) == 0 {
40 if time.Since(start) > maxWait {
46 // it's been long enough, and leaks are still present
47 summary := stack.Summarize(delta.After)
48 t.Errorf("goroutine leak detected:\n%+v", summary)