Repository navigation
Add VerifyCleanup to support leak detection with t.Cleanup - #159
Open
AdamMagued wants to merge 1 commit into
Open
AdamMagued wants to merge 1 commit into
AdamMagued wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
VerifyCleanup(t TestingTB, options ...Option)to enable goroutine leak detection usingtesting.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, usingdefer 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
VerifyNoneint.Cleanupwith options likeIgnoreCurrent()would capture goroutines at cleanup time rather than test setup time.Solution
TestingTBas the minimal interface required for cleanup-based verification (TestingT+Cleanup(func())).VerifyCleanup(t TestingTB, options ...Option):IgnoreCurrent()) immediately at call time to establish the baseline state.t.Cleanupthat invokesVerifyNone(t, opts)when the test finishes.t.Helper()so failures report the test call site.utils_test.goandoptions_test.goto ensure background test goroutines are cleanly joined across test iterations.Verification
All existing and newly added tests pass with 100% statement coverage:
VerifyCleanupdetects leaks, passes on clean runs, works across test helper boundaries, and correctly capturesIgnoreCurrent()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)