Skip to content

Add VerifyCleanup to support leak detection with t.Cleanup - #159

Open
AdamMagued wants to merge 1 commit into
uber-go:masterfrom
AdamMagued:verify-cleanup
Open

AdamMagued wants to merge 1 commit into
uber-go:masterfrom
AdamMagued:verify-cleanup

Conversation

@AdamMagued

Copy link
Copy Markdown

Summary

Adds VerifyCleanup(t TestingTB, options ...Option) to enable goroutine leak detection using testing.TB.Cleanup.

Fixes #53

Problem & Motivation

The standard recommendation for per-test leak detection is defer goleak.VerifyNone(t). However, when test setup is abstracted into helper functions, using defer goleak.VerifyNone(t) inside the helper causes verification to run immediately when the helper returns, rather than at the end of the test. As a result, goroutines spawned during the main test execution are never checked.

In addition, callers who previously attempted to wrap VerifyNone in t.Cleanup with options like IgnoreCurrent() would capture goroutines at cleanup time rather than test setup time.

Solution

  1. Define TestingTB as the minimal interface required for cleanup-based verification (TestingT + Cleanup(func())).
  2. Add VerifyCleanup(t TestingTB, options ...Option):
    • Evaluates options (such as IgnoreCurrent()) immediately at call time to establish the baseline state.
    • Registers a cleanup function via t.Cleanup that invokes VerifyNone(t, opts) when the test finishes.
    • Marks the helper frame via t.Helper() so failures report the test call site.
  3. Fix test synchronization in utils_test.go and options_test.go to ensure background test goroutines are cleanly joined across test iterations.

Verification

All existing and newly added tests pass with 100% statement coverage:

  • Unit tests verifying VerifyCleanup detects leaks, passes on clean runs, works across test helper boundaries, and correctly captures IgnoreCurrent() at setup time.
  • go test -v -race ./... (100% pass)
  • go test -race -count=5 ./... (100% pass)
  • go test -cover ./... (100.0% coverage across all packages)

Add VerifyCleanup which registers a cleanup function using t.Cleanup to
verify that no goroutines leaked after test execution.

Unlike VerifyNone, which relies on defer and executes when the enclosing
function returns, VerifyCleanup can be called inside test helper functions
where defer would execute prematurely before the test body runs.

Fixes uber-go#53
@CLAassistant

CLAassistant commented Oct 2, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consider use of t.Cleanup in goleak

2 participants