From 295d7e66becc3af3b81d2116bf3706954731a01d Mon Sep 17 00:00:00 2001 From: Menghan Li Date: Wed, 17 Nov 2021 12:06:04 -0800 Subject: [PATCH] internal: move leakcheck to t.Cleanup (#4989) --- internal/grpctest/grpctest.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/internal/grpctest/grpctest.go b/internal/grpctest/grpctest.go index 53d1c24f4..a4b49d5f9 100644 --- a/internal/grpctest/grpctest.go +++ b/internal/grpctest/grpctest.go @@ -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) }) }