internal: move leakcheck to t.Cleanup (#4989)

This commit is contained in:
Menghan Li 2021-11-17 12:06:04 -08:00 committed by GitHub
parent 23becb71f7
commit 295d7e66be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 2 deletions

View File

@ -97,9 +97,13 @@ func RunSubTests(t *testing.T, x interface{}) {
}
tfunc := getTestFunc(t, xv, methodName)
t.Run(strings.TrimPrefix(methodName, "Test"), func(t *testing.T) {
// Run leakcheck in t.Cleanup() to guarantee it is run even if tfunc
// or setup uses t.Fatal().
//
// Note that a defer would run before t.Cleanup, so if a goroutine
// is closed by a test's t.Cleanup, a deferred leakcheck would fail.
t.Cleanup(func() { teardown(t) })
setup(t)
// defer teardown to guarantee it is run even if tfunc uses t.Fatal()
defer teardown(t)
tfunc(t)
})
}