@@ -696,8 +696,9 @@ func testTimeoutOnDeadServer(t *testing.T, e env) {
696696 t .Fatalf ("TestService/EmptyCall(_, _) = _, %v, want _, <nil>" , err )
697697 }
698698 te .srv .Stop ()
699- ctx , _ := context .WithTimeout (context .Background (), time .Millisecond )
699+ ctx , cancel := context .WithTimeout (context .Background (), time .Millisecond )
700700 _ , err := tc .EmptyCall (ctx , & testpb.Empty {}, grpc .FailFast (false ))
701+ cancel ()
701702 if e .balancer && grpc .Code (err ) != codes .DeadlineExceeded {
702703 // If e.balancer == nil, the ac will stop reconnecting because the dialer returns non-temp error,
703704 // the error will be an internal error.
@@ -756,11 +757,12 @@ func testServerGoAway(t *testing.T, e env) {
756757 }()
757758 // Loop until the server side GoAway signal is propagated to the client.
758759 for {
759- ctx , _ := context .WithTimeout (context .Background (), 10 * time .Millisecond )
760- if _ , err := tc .EmptyCall (ctx , & testpb.Empty {}); err == nil || grpc .Code (err ) == codes .DeadlineExceeded {
761- continue
760+ ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Millisecond )
761+ if _ , err := tc .EmptyCall (ctx , & testpb.Empty {}); err != nil && grpc .Code (err ) != codes .DeadlineExceeded {
762+ cancel ()
763+ break
762764 }
763- break
765+ cancel ()
764766 }
765767 // A new RPC should fail.
766768 if _ , err := tc .EmptyCall (context .Background (), & testpb.Empty {}); grpc .Code (err ) != codes .Unavailable && grpc .Code (err ) != codes .Internal {
@@ -809,11 +811,12 @@ func testServerGoAwayPendingRPC(t *testing.T, e env) {
809811 }()
810812 // Loop until the server side GoAway signal is propagated to the client.
811813 for {
812- ctx , _ := context .WithTimeout (context .Background (), 10 * time .Millisecond )
813- if _ , err := tc .EmptyCall (ctx , & testpb.Empty {}, grpc .FailFast (false )); err == nil {
814- continue
814+ ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Millisecond )
815+ if _ , err := tc .EmptyCall (ctx , & testpb.Empty {}, grpc .FailFast (false )); err != nil {
816+ cancel ()
817+ break
815818 }
816- break
819+ cancel ()
817820 }
818821 respParam := []* testpb.ResponseParameters {
819822 {
@@ -885,11 +888,12 @@ func testServerMultipleGoAwayPendingRPC(t *testing.T, e env) {
885888 }()
886889 // Loop until the server side GoAway signal is propagated to the client.
887890 for {
888- ctx , _ := context .WithTimeout (context .Background (), 10 * time .Millisecond )
889- if _ , err := tc .EmptyCall (ctx , & testpb.Empty {}, grpc .FailFast (false )); err == nil {
890- continue
891+ ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Millisecond )
892+ if _ , err := tc .EmptyCall (ctx , & testpb.Empty {}, grpc .FailFast (false )); err != nil {
893+ cancel ()
894+ break
891895 }
892- break
896+ cancel ()
893897 }
894898 select {
895899 case <- ch1 :
@@ -1004,11 +1008,12 @@ func testConcurrentServerStopAndGoAway(t *testing.T, e env) {
10041008 }()
10051009 // Loop until the server side GoAway signal is propagated to the client.
10061010 for {
1007- ctx , _ := context .WithTimeout (context .Background (), 10 * time .Millisecond )
1008- if _ , err := tc .EmptyCall (ctx , & testpb.Empty {}, grpc .FailFast (false )); err == nil {
1009- continue
1011+ ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Millisecond )
1012+ if _ , err := tc .EmptyCall (ctx , & testpb.Empty {}, grpc .FailFast (false )); err != nil {
1013+ cancel ()
1014+ break
10101015 }
1011- break
1016+ cancel ()
10121017 }
10131018 // Stop the server and close all the connections.
10141019 te .srv .Stop ()
@@ -1285,14 +1290,16 @@ func testServiceConfigTimeout(t *testing.T, e env) {
12851290 cc := te .clientConn ()
12861291 tc := testpb .NewTestServiceClient (cc )
12871292 // The following RPCs are expected to become non-fail-fast ones with 1ns deadline.
1288- ctx , _ := context .WithTimeout (context .Background (), time .Nanosecond )
1293+ ctx , cancel := context .WithTimeout (context .Background (), time .Nanosecond )
12891294 if _ , err := tc .EmptyCall (ctx , & testpb.Empty {}, grpc .FailFast (false )); grpc .Code (err ) != codes .DeadlineExceeded {
12901295 t .Fatalf ("TestService/EmptyCall(_, _) = _, %v, want _, %s" , err , codes .DeadlineExceeded )
12911296 }
1292- ctx , _ = context .WithTimeout (context .Background (), time .Nanosecond )
1297+ cancel ()
1298+ ctx , cancel = context .WithTimeout (context .Background (), time .Nanosecond )
12931299 if _ , err := tc .FullDuplexCall (ctx , grpc .FailFast (false )); grpc .Code (err ) != codes .DeadlineExceeded {
12941300 t .Fatalf ("TestService/FullDuplexCall(_) = _, %v, want %s" , err , codes .DeadlineExceeded )
12951301 }
1302+ cancel ()
12961303
12971304 // Generate a service config update.
12981305 // Case2: Client API sets timeout to be 1hr and ServiceConfig sets timeout to be 1ns. Timeout should be 1ns (min of 1ns and 1hr) and the rpc will wait until deadline exceeds.
@@ -1316,15 +1323,17 @@ func testServiceConfigTimeout(t *testing.T, e env) {
13161323 break
13171324 }
13181325
1319- ctx , _ = context .WithTimeout (context .Background (), time .Hour )
1326+ ctx , cancel = context .WithTimeout (context .Background (), time .Hour )
13201327 if _ , err := tc .EmptyCall (ctx , & testpb.Empty {}, grpc .FailFast (false )); grpc .Code (err ) != codes .DeadlineExceeded {
13211328 t .Fatalf ("TestService/EmptyCall(_, _) = _, %v, want _, %s" , err , codes .DeadlineExceeded )
13221329 }
1330+ cancel ()
13231331
1324- ctx , _ = context .WithTimeout (context .Background (), time .Hour )
1332+ ctx , cancel = context .WithTimeout (context .Background (), time .Hour )
13251333 if _ , err := tc .FullDuplexCall (ctx , grpc .FailFast (false )); grpc .Code (err ) != codes .DeadlineExceeded {
13261334 t .Fatalf ("TestService/FullDuplexCall(_) = _, %v, want %s" , err , codes .DeadlineExceeded )
13271335 }
1336+ cancel ()
13281337}
13291338
13301339func TestServiceConfigMaxMsgSize (t * testing.T ) {
@@ -1846,7 +1855,8 @@ func testTap(t *testing.T, e env) {
18461855}
18471856
18481857func healthCheck (d time.Duration , cc * grpc.ClientConn , serviceName string ) (* healthpb.HealthCheckResponse , error ) {
1849- ctx , _ := context .WithTimeout (context .Background (), d )
1858+ ctx , cancel := context .WithTimeout (context .Background (), d )
1859+ defer cancel ()
18501860 hc := healthpb .NewHealthClient (cc )
18511861 req := & healthpb.HealthCheckRequest {
18521862 Service : serviceName ,
@@ -2872,10 +2882,11 @@ func testRPCTimeout(t *testing.T, e env) {
28722882 Payload : payload ,
28732883 }
28742884 for i := - 1 ; i <= 10 ; i ++ {
2875- ctx , _ := context .WithTimeout (context .Background (), time .Duration (i )* time .Millisecond )
2885+ ctx , cancel := context .WithTimeout (context .Background (), time .Duration (i )* time .Millisecond )
28762886 if _ , err := tc .UnaryCall (ctx , req ); grpc .Code (err ) != codes .DeadlineExceeded {
28772887 t .Fatalf ("TestService/UnaryCallv(_, _) = _, %v; want <nil>, error code: %s" , err , codes .DeadlineExceeded )
28782888 }
2889+ cancel ()
28792890 }
28802891}
28812892
@@ -3355,7 +3366,8 @@ func testClientStreaming(t *testing.T, e env, sizes []int) {
33553366 defer te .tearDown ()
33563367 tc := testpb .NewTestServiceClient (te .clientConn ())
33573368
3358- ctx , _ := context .WithTimeout (te .ctx , time .Second * 30 )
3369+ ctx , cancel := context .WithTimeout (te .ctx , time .Second * 30 )
3370+ defer cancel ()
33593371 stream , err := tc .StreamingInputCall (ctx )
33603372 if err != nil {
33613373 t .Fatalf ("%v.StreamingInputCall(_) = _, %v, want <nil>" , tc , err )
@@ -3569,7 +3581,8 @@ func testStreamsQuotaRecovery(t *testing.T, e env) {
35693581 Payload : payload ,
35703582 }
35713583 // No rpc should go through due to the max streams limit.
3572- ctx , _ := context .WithTimeout (context .Background (), 10 * time .Millisecond )
3584+ ctx , cancel := context .WithTimeout (context .Background (), 10 * time .Millisecond )
3585+ defer cancel ()
35733586 if _ , err := tc .UnaryCall (ctx , req , grpc .FailFast (false )); grpc .Code (err ) != codes .DeadlineExceeded {
35743587 t .Errorf ("TestService/UnaryCall(_, _) = _, %v, want _, %s" , err , codes .DeadlineExceeded )
35753588 }
0 commit comments