diff options
author | Sami Hiltunen <shiltunen@gitlab.com> | 2022-01-24 18:45:53 +0300 |
---|---|---|
committer | Sami Hiltunen <shiltunen@gitlab.com> | 2022-01-27 15:51:08 +0300 |
commit | 6c91c8e8b0917567fdcd6b14b075c8bdf20d2c5f (patch) | |
tree | 38f87248a6ee7993e137aa74580c3860558511a0 /internal/sidechannel | |
parent | b4a1fffaa707d5f0dc19af21f06947d0274db878 (diff) |
Automatically clean up testhelper.Context
testhelper.Context() currently return a cancellation function as a
second return value. Majority of the tests do not need to explicitly
cancel the context but they simply defer its cancellation to clean up
after the test. Given this, we can reduce the test verbosity and make
testhelper.Context easier to compose by removing the unnecessary second
return value. This adds a t.Cleanup function to automatically cancel the
context at the end of the tests and omits the returned cancellation
function.
Tests which simply `defer cancel()` have had the extra call removed. Some
tests explicitly call the cancellation, and these tests have been modified
to add context.WithCancel around the testhelper.Context call. There are a
few loctions where testing.TB was passed down to test helpers that create the
context.
Diffstat (limited to 'internal/sidechannel')
-rw-r--r-- | internal/sidechannel/proxy_test.go | 8 | ||||
-rw-r--r-- | internal/sidechannel/sidechannel_test.go | 6 |
2 files changed, 4 insertions, 10 deletions
diff --git a/internal/sidechannel/proxy_test.go b/internal/sidechannel/proxy_test.go index 103ecfd63..136f469ee 100644 --- a/internal/sidechannel/proxy_test.go +++ b/internal/sidechannel/proxy_test.go @@ -103,9 +103,7 @@ func testUnaryProxy(t *testing.T, closeWrite bool) { return healthpb.NewHealthClient(conn).Check(ctxOut, request) }, ) - - ctx, cancel := testhelper.Context() - defer cancel() + ctx := testhelper.Context(t) conn, registry := dial(t, proxyAddr) require.NoError(t, call(ctx, conn, registry, testProxyClient(closeWrite))) @@ -165,9 +163,7 @@ func testStreamProxy(t *testing.T, closeWrite bool) { return nil }, ) - - ctx, cancel := testhelper.Context() - defer cancel() + ctx := testhelper.Context(t) conn, registry := dial(t, proxyAddr) ctx, waiter := RegisterSidechannel(ctx, registry, testProxyClient(closeWrite)) diff --git a/internal/sidechannel/sidechannel_test.go b/internal/sidechannel/sidechannel_test.go index e5c153f1a..6fd4628f6 100644 --- a/internal/sidechannel/sidechannel_test.go +++ b/internal/sidechannel/sidechannel_test.go @@ -19,8 +19,7 @@ import ( ) func TestSidechannel(t *testing.T) { - ctx, cancel := testhelper.Context() - defer cancel() + ctx := testhelper.Context(t) const blobSize = 1024 * 1024 @@ -72,8 +71,7 @@ func TestSidechannel(t *testing.T) { // Conduct multiple requests with sidechannel included on the same grpc // connection. func TestSidechannelConcurrency(t *testing.T) { - ctx, cancel := testhelper.Context() - defer cancel() + ctx := testhelper.Context(t) const concurrency = 10 const blobSize = 1024 * 1024 |