Skip to content

Commit 4832deb

Browse files
authored
test: clean up deadlines set in tests (#6506)
1 parent 9c46304 commit 4832deb

19 files changed

+182
-160
lines changed

test/authority_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"strings"
3030
"sync"
3131
"testing"
32-
"time"
3332

3433
"google.golang.org/grpc"
3534
"google.golang.org/grpc/codes"
@@ -84,7 +83,7 @@ func runUnixTest(t *testing.T, address, target, expectedAuthority string, dialer
8483
t.Fatalf("Error starting endpoint server: %v", err)
8584
}
8685
defer ss.Stop()
87-
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
86+
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
8887
defer cancel()
8988
_, err := ss.Client.EmptyCall(ctx, &testpb.Empty{})
9089
if err != nil {
@@ -202,7 +201,7 @@ func (s) TestColonPortAuthority(t *testing.T) {
202201
t.Fatalf("grpc.Dial(%q) = %v", ss.Target, err)
203202
}
204203
defer cc.Close()
205-
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
204+
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
206205
defer cancel()
207206
_, err = testgrpc.NewTestServiceClient(cc).EmptyCall(ctx, &testpb.Empty{})
208207
if err != nil {

test/balancer_test.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,9 @@ func (s) TestCredsBundleFromBalancer(t *testing.T) {
170170

171171
cc := te.clientConn()
172172
tc := testgrpc.NewTestServiceClient(cc)
173-
if _, err := tc.EmptyCall(context.Background(), &testpb.Empty{}); err != nil {
173+
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
174+
defer cancel()
175+
if _, err := tc.EmptyCall(ctx, &testpb.Empty{}); err != nil {
174176
t.Fatalf("Test failed. Reason: %v", err)
175177
}
176178
}
@@ -244,7 +246,7 @@ func testDoneInfo(t *testing.T, e env) {
244246
cc := te.clientConn()
245247
tc := testgrpc.NewTestServiceClient(cc)
246248

247-
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
249+
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
248250
defer cancel()
249251
wantErr := detailedError
250252
if _, err := tc.EmptyCall(ctx, &testpb.Empty{}); !testutils.StatusErrEqual(err, wantErr) {
@@ -321,7 +323,7 @@ func testDoneLoads(t *testing.T) {
321323

322324
tc := testgrpc.NewTestServiceClient(ss.CC)
323325

324-
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
326+
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
325327
defer cancel()
326328
if _, err := tc.EmptyCall(ctx, &testpb.Empty{}); err != nil {
327329
t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, %v", err, nil)
@@ -438,7 +440,7 @@ func (s) TestAddressAttributesInNewSubConn(t *testing.T) {
438440
t.Log("Created a ClientConn...")
439441

440442
// The first RPC should fail because there's no address.
441-
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
443+
ctx, cancel := context.WithTimeout(context.Background(), defaultTestShortTimeout)
442444
defer cancel()
443445
if _, err := tc.EmptyCall(ctx, &testpb.Empty{}); err == nil || status.Code(err) != codes.DeadlineExceeded {
444446
t.Fatalf("EmptyCall() = _, %v, want _, DeadlineExceeded", err)
@@ -450,7 +452,7 @@ func (s) TestAddressAttributesInNewSubConn(t *testing.T) {
450452
t.Logf("Pushing resolver state update: %v through the manual resolver", state)
451453

452454
// The second RPC should succeed.
453-
ctx, cancel = context.WithTimeout(context.Background(), time.Second)
455+
ctx, cancel = context.WithTimeout(context.Background(), defaultTestTimeout)
454456
defer cancel()
455457
if _, err := tc.EmptyCall(ctx, &testpb.Empty{}); err != nil {
456458
t.Fatalf("EmptyCall() = _, %v, want _, <nil>", err)
@@ -519,7 +521,7 @@ func (s) TestMetadataInAddressAttributes(t *testing.T) {
519521
defer ss.Stop()
520522

521523
// The RPC should succeed with the expected md.
522-
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
524+
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
523525
defer cancel()
524526
if _, err := ss.Client.EmptyCall(ctx, &testpb.Empty{}); err != nil {
525527
t.Fatalf("EmptyCall() = _, %v, want _, <nil>", err)
@@ -536,7 +538,7 @@ func (s) TestMetadataInAddressAttributes(t *testing.T) {
536538
// TestServersSwap creates two servers and verifies the client switches between
537539
// them when the name resolver reports the first and then the second.
538540
func (s) TestServersSwap(t *testing.T) {
539-
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
541+
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
540542
defer cancel()
541543

542544
// Initialize servers
@@ -592,7 +594,7 @@ func (s) TestServersSwap(t *testing.T) {
592594
}
593595

594596
func (s) TestWaitForReady(t *testing.T) {
595-
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
597+
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
596598
defer cancel()
597599

598600
// Initialize server
@@ -1035,7 +1037,7 @@ func (s) TestBalancerProducerHonorsContext(t *testing.T) {
10351037
// rpcErrChan is given to the LB policy to report the status of the
10361038
// producer's one RPC.
10371039
ctxChan := make(chan context.Context, 1)
1038-
ctx, cancel := context.WithCancel(context.Background())
1040+
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
10391041
ctxChan <- ctx
10401042

10411043
rpcErrChan := make(chan error)

test/channelz_test.go

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,9 @@ func (s) TestCZChannelMetrics(t *testing.T) {
523523
cc := te.clientConn(grpc.WithResolvers(r))
524524
defer te.tearDown()
525525
tc := testgrpc.NewTestServiceClient(cc)
526-
if _, err := tc.EmptyCall(context.Background(), &testpb.Empty{}); err != nil {
526+
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
527+
defer cancel()
528+
if _, err := tc.EmptyCall(ctx, &testpb.Empty{}); err != nil {
527529
t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, <nil>", err)
528530
}
529531

@@ -540,11 +542,11 @@ func (s) TestCZChannelMetrics(t *testing.T) {
540542
Payload: largePayload,
541543
}
542544

543-
if _, err := tc.UnaryCall(context.Background(), req); err == nil || status.Code(err) != codes.ResourceExhausted {
545+
if _, err := tc.UnaryCall(ctx, req); err == nil || status.Code(err) != codes.ResourceExhausted {
544546
t.Fatalf("TestService/UnaryCall(_, _) = _, %v, want _, error code: %s", err, codes.ResourceExhausted)
545547
}
546548

547-
stream, err := tc.FullDuplexCall(context.Background())
549+
stream, err := tc.FullDuplexCall(ctx)
548550
if err != nil {
549551
t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
550552
}
@@ -603,7 +605,9 @@ func (s) TestCZServerMetrics(t *testing.T) {
603605
defer te.tearDown()
604606
cc := te.clientConn()
605607
tc := testgrpc.NewTestServiceClient(cc)
606-
if _, err := tc.EmptyCall(context.Background(), &testpb.Empty{}); err != nil {
608+
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
609+
defer cancel()
610+
if _, err := tc.EmptyCall(ctx, &testpb.Empty{}); err != nil {
607611
t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, <nil>", err)
608612
}
609613

@@ -619,11 +623,11 @@ func (s) TestCZServerMetrics(t *testing.T) {
619623
ResponseSize: int32(smallSize),
620624
Payload: largePayload,
621625
}
622-
if _, err := tc.UnaryCall(context.Background(), req); err == nil || status.Code(err) != codes.ResourceExhausted {
626+
if _, err := tc.UnaryCall(ctx, req); err == nil || status.Code(err) != codes.ResourceExhausted {
623627
t.Fatalf("TestService/UnaryCall(_, _) = _, %v, want _, error code: %s", err, codes.ResourceExhausted)
624628
}
625629

626-
stream, err := tc.FullDuplexCall(context.Background())
630+
stream, err := tc.FullDuplexCall(ctx)
627631
if err != nil {
628632
t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
629633
}
@@ -746,7 +750,7 @@ func doServerSideFailedUnaryCall(tc testgrpc.TestServiceClient, t *testing.T) {
746750
}
747751

748752
func doClientSideInitiatedFailedStream(tc testgrpc.TestServiceClient, t *testing.T) {
749-
ctx, cancel := context.WithCancel(context.Background())
753+
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
750754
stream, err := tc.FullDuplexCall(ctx)
751755
if err != nil {
752756
t.Fatalf("TestService/FullDuplexCall(_) = _, %v, want <nil>", err)
@@ -779,7 +783,9 @@ func doClientSideInitiatedFailedStream(tc testgrpc.TestServiceClient, t *testing
779783

780784
// This func is to be used to test client side counting of failed streams.
781785
func doServerSideInitiatedFailedStreamWithRSTStream(tc testgrpc.TestServiceClient, t *testing.T, l *listenerWrapper) {
782-
stream, err := tc.FullDuplexCall(context.Background())
786+
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
787+
defer cancel()
788+
stream, err := tc.FullDuplexCall(ctx)
783789
if err != nil {
784790
t.Fatalf("TestService/FullDuplexCall(_) = _, %v, want <nil>", err)
785791
}
@@ -816,10 +822,10 @@ func doServerSideInitiatedFailedStreamWithRSTStream(tc testgrpc.TestServiceClien
816822
}
817823

818824
// this func is to be used to test client side counting of failed streams.
819-
func doServerSideInitiatedFailedStreamWithGoAway(tc testgrpc.TestServiceClient, t *testing.T, l *listenerWrapper) {
825+
func doServerSideInitiatedFailedStreamWithGoAway(ctx context.Context, tc testgrpc.TestServiceClient, t *testing.T, l *listenerWrapper) {
820826
// This call is just to keep the transport from shutting down (socket will be deleted
821827
// in this case, and we will not be able to get metrics).
822-
s, err := tc.FullDuplexCall(context.Background())
828+
s, err := tc.FullDuplexCall(ctx)
823829
if err != nil {
824830
t.Fatalf("TestService/FullDuplexCall(_) = _, %v, want <nil>", err)
825831
}
@@ -834,7 +840,7 @@ func doServerSideInitiatedFailedStreamWithGoAway(tc testgrpc.TestServiceClient,
834840
t.Fatalf("s.Recv() failed with error: %v", err)
835841
}
836842

837-
s, err = tc.FullDuplexCall(context.Background())
843+
s, err = tc.FullDuplexCall(ctx)
838844
if err != nil {
839845
t.Fatalf("TestService/FullDuplexCall(_) = _, %v, want <nil>", err)
840846
}
@@ -859,7 +865,7 @@ func doServerSideInitiatedFailedStreamWithGoAway(tc testgrpc.TestServiceClient,
859865
}
860866

861867
func doIdleCallToInvokeKeepAlive(tc testgrpc.TestServiceClient, t *testing.T) {
862-
ctx, cancel := context.WithCancel(context.Background())
868+
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
863869
_, err := tc.FullDuplexCall(ctx)
864870
if err != nil {
865871
t.Fatalf("TestService/FullDuplexCall(_) = _, %v, want <nil>", err)
@@ -870,6 +876,9 @@ func doIdleCallToInvokeKeepAlive(tc testgrpc.TestServiceClient, t *testing.T) {
870876
}
871877

872878
func (s) TestCZClientSocketMetricsStreamsAndMessagesCount(t *testing.T) {
879+
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
880+
defer cancel()
881+
873882
czCleanup := channelz.NewChannelzStorageForTesting()
874883
defer czCleanupWrapper(czCleanup, t)
875884
e := tcpClearRREnv
@@ -951,7 +960,7 @@ func (s) TestCZClientSocketMetricsStreamsAndMessagesCount(t *testing.T) {
951960
t.Fatal(err)
952961
}
953962

954-
doServerSideInitiatedFailedStreamWithGoAway(tc, t, rcw)
963+
doServerSideInitiatedFailedStreamWithGoAway(ctx, tc, t, rcw)
955964
if err := verifyResultWithDelay(func() (bool, error) {
956965
skt := channelz.GetSocket(skID)
957966
sktData := skt.SocketData
@@ -988,7 +997,7 @@ func (s) TestCZClientAndServerSocketMetricsStreamsCountFlowControlRSTStream(t *t
988997
cc, dw := te.clientConnWithConnControl()
989998
tc := &testServiceClientWrapper{TestServiceClient: testgrpc.NewTestServiceClient(cc)}
990999

991-
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1000+
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
9921001
stream, err := tc.FullDuplexCall(ctx)
9931002
if err != nil {
9941003
t.Fatalf("TestService/FullDuplexCall(_) = _, %v, want <nil>", err)
@@ -1534,7 +1543,7 @@ func (s) TestCZSubChannelTraceCreationDeletion(t *testing.T) {
15341543
t.Fatal(err)
15351544
}
15361545

1537-
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1546+
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
15381547
defer cancel()
15391548
awaitState(ctx, t, te.cc, connectivity.Ready)
15401549
r.UpdateState(resolver.State{Addresses: []resolver.Address{{Addr: "fake address"}}})
@@ -1694,7 +1703,7 @@ func (s) TestCZSubChannelPickedNewAddress(t *testing.T) {
16941703
defer te.tearDown()
16951704
tc := testgrpc.NewTestServiceClient(cc)
16961705
// make sure the connection is up
1697-
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
1706+
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
16981707
defer cancel()
16991708
if _, err := tc.EmptyCall(ctx, &testpb.Empty{}); err != nil {
17001709
t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, <nil>", err)
@@ -1707,9 +1716,7 @@ func (s) TestCZSubChannelPickedNewAddress(t *testing.T) {
17071716
defer close(done)
17081717
go func() {
17091718
for {
1710-
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
17111719
tc.EmptyCall(ctx, &testpb.Empty{})
1712-
cancel()
17131720
select {
17141721
case <-time.After(10 * time.Millisecond):
17151722
case <-done:
@@ -1763,7 +1770,7 @@ func (s) TestCZSubChannelConnectivityState(t *testing.T) {
17631770
defer te.tearDown()
17641771
tc := testgrpc.NewTestServiceClient(cc)
17651772
// make sure the connection is up
1766-
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
1773+
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
17671774
defer cancel()
17681775
if _, err := tc.EmptyCall(ctx, &testpb.Empty{}); err != nil {
17691776
t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, <nil>", err)
@@ -1862,7 +1869,7 @@ func (s) TestCZChannelConnectivityState(t *testing.T) {
18621869
defer te.tearDown()
18631870
tc := testgrpc.NewTestServiceClient(cc)
18641871
// make sure the connection is up
1865-
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
1872+
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
18661873
defer cancel()
18671874
if _, err := tc.EmptyCall(ctx, &testpb.Empty{}); err != nil {
18681875
t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, <nil>", err)
@@ -2010,7 +2017,7 @@ func (s) TestCZTraceOverwriteSubChannelDeletion(t *testing.T) {
20102017
t.Fatal(err)
20112018
}
20122019

2013-
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
2020+
ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
20142021
defer cancel()
20152022
awaitState(ctx, t, te.cc, connectivity.Ready)
20162023
r.UpdateState(resolver.State{Addresses: []resolver.Address{{Addr: "fake address"}}})

0 commit comments

Comments
 (0)