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 /client | |
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 'client')
-rw-r--r-- | client/dial_test.go | 24 | ||||
-rw-r--r-- | client/pool_test.go | 7 |
2 files changed, 8 insertions, 23 deletions
diff --git a/client/dial_test.go b/client/dial_test.go index 9a0f9f660..41e775a21 100644 --- a/client/dial_test.go +++ b/client/dial_test.go @@ -130,9 +130,7 @@ func TestDial(t *testing.T) { if tt.envSSLCertFile != "" { testhelper.ModifyEnvironment(t, gitalyx509.SSLCertFile, tt.envSSLCertFile) } - - ctx, cancel := testhelper.Context() - defer cancel() + ctx := testhelper.Context(t) conn, err := Dial(tt.rawAddress, tt.dialOpts) if tt.expectDialFailure { @@ -220,9 +218,7 @@ func TestDialSidechannel(t *testing.T) { if tt.envSSLCertFile != "" { testhelper.ModifyEnvironment(t, gitalyx509.SSLCertFile, tt.envSSLCertFile) } - - ctx, cancel := testhelper.Context() - defer cancel() + ctx := testhelper.Context(t) conn, err := DialSidechannel(ctx, tt.rawAddress, registry, tt.dialOpts) require.NoError(t, err) @@ -298,9 +294,7 @@ func TestDial_Correlation(t *testing.T) { go func() { assert.NoError(t, grpcServer.Serve(listener)) }() defer grpcServer.Stop() - - ctx, cancel := testhelper.Context() - defer cancel() + ctx := testhelper.Context(t) cc, err := DialContext(ctx, "unix://"+serverSocketPath, nil) require.NoError(t, err) @@ -333,9 +327,7 @@ func TestDial_Correlation(t *testing.T) { go func() { assert.NoError(t, grpcServer.Serve(listener)) }() defer grpcServer.Stop() - - ctx, cancel := testhelper.Context() - defer cancel() + ctx := testhelper.Context(t) cc, err := DialContext(ctx, "unix://"+serverSocketPath, nil) require.NoError(t, err) @@ -396,9 +388,7 @@ func TestDial_Tracing(t *testing.T) { go func() { require.NoError(t, grpcServer.Serve(listener)) }() defer grpcServer.Stop() - - ctx, cancel := testhelper.Context() - defer cancel() + ctx := testhelper.Context(t) t.Run("unary", func(t *testing.T) { reporter := jaeger.NewInMemoryReporter() @@ -627,9 +617,7 @@ func emitProxyWarning() bool { func TestHealthCheckDialer(t *testing.T) { _, addr, cleanup := runServer(t, "token") defer cleanup() - - ctx, cancel := testhelper.Context() - defer cancel() + ctx := testhelper.Context(t) _, err := HealthCheckDialer(DialContext)(ctx, addr, nil) testhelper.RequireGrpcError(t, status.Error(codes.Unauthenticated, "authentication required"), err) diff --git a/client/pool_test.go b/client/pool_test.go index 3b02b81a7..924146fd7 100644 --- a/client/pool_test.go +++ b/client/pool_test.go @@ -163,9 +163,7 @@ func TestPoolDial(t *testing.T) { defer func() { require.NoError(t, pool.Close()) }() - - ctx, cancel := testhelper.Context() - defer cancel() + ctx := testhelper.Context(t) tc.test(t, ctx, pool) }) @@ -227,8 +225,7 @@ func verifyConnection(t *testing.T, ctx context.Context, conn *grpc.ClientConn, } func TestPool_Dial_same_addr_another_token(t *testing.T) { - ctx, cancel := testhelper.Context() - defer cancel() + ctx := testhelper.Context(t) _, addr, stop1 := runServer(t, "") defer func() { stop1() }() |