|
| 1 | +// SPDX-FileCopyrightText: Copyright 2025 go-swagger maintainers |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package assertions |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + |
| 9 | + "github.com/go-openapi/testify/v2/internal/leak" |
| 10 | +) |
| 11 | + |
| 12 | +// NoGoRoutineLeak ensures that no goroutine did leak from inside the tested function. |
| 13 | +// |
| 14 | +// NOTE: only the go routines spawned from inside the tested function are checked for leaks. |
| 15 | +// No filter or configuration is needed to exclude "known go routines". |
| 16 | +// |
| 17 | +// Resource cleanup should be done inside the tested function, and not using [testing.T.Cleanup], |
| 18 | +// as t.Cleanup is called after the leak check. |
| 19 | +// |
| 20 | +// # Edge cases |
| 21 | +// |
| 22 | +// - if the tested function panics leaving behind leaked goroutines, these are detected. |
| 23 | +// - if the tested function calls runtime.Goexit (e.g. from [testing.T.FailNow]) leaving behind leaked goroutines, |
| 24 | +// these are detected. |
| 25 | +// - if a panic occurs in one of the leaked go routines, it cannot be recovered with certainty and |
| 26 | +// the calling program will usually panic. |
| 27 | +// |
| 28 | +// # Concurrency |
| 29 | +// |
| 30 | +// [NoGoRoutineLeak] may be used safely in parallel tests. |
| 31 | +// |
| 32 | +// # Usage |
| 33 | +// |
| 34 | +// NoGoRoutineLeak(t, func() { |
| 35 | +// ... |
| 36 | +// }, |
| 37 | +// "should not leak any go routine", |
| 38 | +// ) |
| 39 | +// |
| 40 | +// # Examples |
| 41 | +// |
| 42 | +// success: func() {} |
| 43 | +func NoGoRoutineLeak(t T, tested func(), msgAndArgs ...any) bool { |
| 44 | + // Domain: safety |
| 45 | + if h, ok := t.(H); ok { |
| 46 | + h.Helper() |
| 47 | + } |
| 48 | + |
| 49 | + var ctx context.Context |
| 50 | + c, ok := t.(contextualizer) |
| 51 | + if ok { |
| 52 | + ctx = c.Context() |
| 53 | + } |
| 54 | + if ctx == nil { |
| 55 | + ctx = context.Background() |
| 56 | + } |
| 57 | + |
| 58 | + signature := leak.Leaked(ctx, tested) |
| 59 | + if signature == "" { |
| 60 | + return true |
| 61 | + } |
| 62 | + |
| 63 | + return Fail(t, "found leaked go routines: "+signature, msgAndArgs...) |
| 64 | +} |
0 commit comments