| 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. |
| 4 | |
| 5 | package stacktestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack/stacktest`/ |
| 6 | |
| 7 | import ( |
| 8 | "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/" |
| 9 | "timescip-go gomod github.com/golang/go/src go1.25.0 time/" |
| 10 | |
| 11 | "golang.org/x/tools/internal/jsonrpc2/stackscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/" |
| 12 | ) |
| 13 | |
| 14 | // this is only needed to support pre 1.14 when testing.TB did not have Cleanup |
| 15 | type withCleanupscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack/stacktest`/withCleanup# interface { |
| 16 | Cleanupscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack/stacktest`/withCleanup#Cleanup.(func()) |
| 17 | } |
| 18 | |
| 19 | // the maximum amount of time to wait for goroutines to clean themselves up. |
| 20 | const maxWaitscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack/stacktest`/maxWait. = timescip-go gomod github.com/golang/go/src go1.25.0 time/.Secondscip-go gomod github.com/golang/go/src go1.25.0 time/Second. |
| 21 | |
| 22 | // NoLeak checks that a test (or benchmark) does not leak any goroutines. |
| 23 | func NoLeakscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack/stacktest`/NoLeak().(t testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.TBscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#) { |
| 24 | c, ok := t.(withCleanupscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack/stacktest`/withCleanup#) |
| 25 | if !ok { |
| 26 | return |
| 27 | } |
| 28 | before := stackscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/.Capturescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Capture().() |
| 29 | c.Cleanupscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack/stacktest`/withCleanup#Cleanup.(func() { |
| 30 | var delta stackscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/.Deltascip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Delta# |
| 31 | start := timescip-go gomod github.com/golang/go/src go1.25.0 time/.Nowscip-go gomod github.com/golang/go/src go1.25.0 time/Now().() |
| 32 | delay := timescip-go gomod github.com/golang/go/src go1.25.0 time/.Millisecondscip-go gomod github.com/golang/go/src go1.25.0 time/Millisecond. |
| 33 | for { |
| 34 | after := stackscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/.Capturescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Capture().() |
| 35 | delta = stackscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/.Diffscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Diff().(before, after) |
| 36 | if len(delta.Afterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Delta#After.) == 0 { |
| 37 | // no leaks |
| 38 | return |
| 39 | } |
| 40 | if timescip-go gomod github.com/golang/go/src go1.25.0 time/.Sincescip-go gomod github.com/golang/go/src go1.25.0 time/Since().(start) > maxWaitscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack/stacktest`/maxWait. { |
| 41 | break |
| 42 | } |
| 43 | timescip-go gomod github.com/golang/go/src go1.25.0 time/.Sleepscip-go gomod github.com/golang/go/src go1.25.0 time/Sleep().(delay) |
| 44 | delay *= 2 |
| 45 | } |
| 46 | // it's been long enough, and leaks are still present |
| 47 | summary := stackscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/.Summarizescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Summarize().(delta.Afterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2/stack`/Delta#After.) |
| 48 | t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/TB#Errorf().("goroutine leak detected:\n%+v", summary) |
| 49 | }) |
| 50 | } |