Skip to content

Commit e606983

Browse files
authored
Fix context warnings from govet. (#1486)
Pre-req work for #1484
1 parent 7fd9c2c commit e606983

File tree

5 files changed

+58
-32
lines changed

5 files changed

+58
-32
lines changed

balancer_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,12 @@ func TestGetOnWaitChannel(t *testing.T) {
284284
r.w.inject(updates)
285285
for {
286286
var reply string
287-
ctx, _ := context.WithTimeout(context.Background(), 10*time.Millisecond)
287+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
288288
if err := Invoke(ctx, "/foo/bar", &expectedRequest, &reply, cc, FailFast(false)); Code(err) == codes.DeadlineExceeded {
289+
cancel()
289290
break
290291
}
292+
cancel()
291293
time.Sleep(10 * time.Millisecond)
292294
}
293295
var wg sync.WaitGroup

clientconn_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,8 @@ func nonTemporaryErrorDialer(addr string, timeout time.Duration) (net.Conn, erro
293293
}
294294

295295
func TestDialWithBlockErrorOnNonTemporaryErrorDialer(t *testing.T) {
296-
ctx, _ := context.WithTimeout(context.Background(), 100*time.Millisecond)
296+
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
297+
defer cancel()
297298
if _, err := DialContext(ctx, "", WithInsecure(), WithDialer(nonTemporaryErrorDialer), WithBlock(), FailOnNonTempDialError(true)); err != nonTemporaryError {
298299
t.Fatalf("Dial(%q) = %v, want %v", "", err, nonTemporaryError)
299300
}

stream.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ func newClientStream(ctx context.Context, desc *StreamDesc, cc *ClientConn, meth
117117

118118
if mc.Timeout != nil {
119119
ctx, cancel = context.WithTimeout(ctx, *mc.Timeout)
120+
defer func() {
121+
if err != nil {
122+
cancel()
123+
}
124+
}()
120125
}
121126

122127
opts = append(cc.dopts.callOptions, opts...)

test/end2end_test.go

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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

13301339
func TestServiceConfigMaxMsgSize(t *testing.T) {
@@ -1846,7 +1855,8 @@ func testTap(t *testing.T, e env) {
18461855
}
18471856

18481857
func 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
}

transport/transport_test.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,7 +1020,8 @@ func TestLargeMessageSuspension(t *testing.T) {
10201020
Method: "foo.Large",
10211021
}
10221022
// Set a long enough timeout for writing a large message out.
1023-
ctx, _ := context.WithTimeout(context.Background(), time.Second)
1023+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
1024+
defer cancel()
10241025
s, err := ct.NewStream(ctx, callHdr)
10251026
if err != nil {
10261027
t.Fatalf("failed to open stream: %v", err)
@@ -1846,8 +1847,9 @@ func TestAccountCheckExpandingWindow(t *testing.T) {
18461847
st.fc.mu.Unlock()
18471848

18481849
// Check flow conrtrol window on client stream is equal to out flow on server stream.
1849-
ctx, _ := context.WithTimeout(context.Background(), time.Second)
1850+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
18501851
serverStreamSendQuota, err := wait(ctx, nil, nil, nil, sstream.sendQuotaPool.acquire())
1852+
cancel()
18511853
if err != nil {
18521854
return true, fmt.Errorf("error while acquiring server stream send quota. Err: %v", err)
18531855
}
@@ -1860,8 +1862,9 @@ func TestAccountCheckExpandingWindow(t *testing.T) {
18601862
}
18611863

18621864
// Check flow control window on server stream is equal to out flow on client stream.
1863-
ctx, _ = context.WithTimeout(context.Background(), time.Second)
1865+
ctx, cancel = context.WithTimeout(context.Background(), time.Second)
18641866
clientStreamSendQuota, err := wait(ctx, nil, nil, nil, cstream.sendQuotaPool.acquire())
1867+
cancel()
18651868
if err != nil {
18661869
return true, fmt.Errorf("error while acquiring client stream send quota. Err: %v", err)
18671870
}
@@ -1874,8 +1877,9 @@ func TestAccountCheckExpandingWindow(t *testing.T) {
18741877
}
18751878

18761879
// Check flow control window on client transport is equal to out flow of server transport.
1877-
ctx, _ = context.WithTimeout(context.Background(), time.Second)
1880+
ctx, cancel = context.WithTimeout(context.Background(), time.Second)
18781881
serverTrSendQuota, err := wait(ctx, nil, nil, nil, st.sendQuotaPool.acquire())
1882+
cancel()
18791883
if err != nil {
18801884
return true, fmt.Errorf("error while acquring server transport send quota. Err: %v", err)
18811885
}
@@ -1888,8 +1892,9 @@ func TestAccountCheckExpandingWindow(t *testing.T) {
18881892
}
18891893

18901894
// Check flow control window on server transport is equal to out flow of client transport.
1891-
ctx, _ = context.WithTimeout(context.Background(), time.Second)
1895+
ctx, cancel = context.WithTimeout(context.Background(), time.Second)
18921896
clientTrSendQuota, err := wait(ctx, nil, nil, nil, ct.sendQuotaPool.acquire())
1897+
cancel()
18931898
if err != nil {
18941899
return true, fmt.Errorf("error while acquiring client transport send quota. Err: %v", err)
18951900
}

0 commit comments

Comments
 (0)