Call cancel on contexts in tests (#1412)

This commit is contained in:
Menghan Li
2017-08-02 10:43:35 -07:00
committed by GitHub
parent fa1cb32dc4
commit b463cc3276
2 changed files with 6 additions and 3 deletions

View File

@ -33,7 +33,8 @@ import (
) )
func assertState(wantState ConnectivityState, cc *ClientConn) (ConnectivityState, bool) { func assertState(wantState ConnectivityState, cc *ClientConn) (ConnectivityState, bool) {
ctx, _ := context.WithTimeout(context.Background(), time.Second) ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
var state ConnectivityState var state ConnectivityState
for state = cc.GetState(); state != wantState && cc.WaitForStateChange(ctx, state); state = cc.GetState() { for state = cc.GetState(); state != wantState && cc.WaitForStateChange(ctx, state); state = cc.GetState() {
} }

View File

@ -4793,7 +4793,8 @@ func testWaitForReadyConnection(t *testing.T, e env) {
cc := te.clientConn() // Non-blocking dial. cc := te.clientConn() // Non-blocking dial.
tc := testpb.NewTestServiceClient(cc) tc := testpb.NewTestServiceClient(cc)
ctx, _ := context.WithTimeout(context.Background(), time.Second) ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
state := cc.GetState() state := cc.GetState()
// Wait for connection to be Ready. // Wait for connection to be Ready.
for ; state != grpc.Ready && cc.WaitForStateChange(ctx, state); state = cc.GetState() { for ; state != grpc.Ready && cc.WaitForStateChange(ctx, state); state = cc.GetState() {
@ -4801,7 +4802,8 @@ func testWaitForReadyConnection(t *testing.T, e env) {
if state != grpc.Ready { if state != grpc.Ready {
t.Fatalf("Want connection state to be Ready, got %v", state) t.Fatalf("Want connection state to be Ready, got %v", state)
} }
ctx, _ = context.WithTimeout(context.Background(), time.Second) ctx, cancel = context.WithTimeout(context.Background(), time.Second)
defer cancel()
// Make a fail-fast RPC. // Make a fail-fast RPC.
if _, err := tc.EmptyCall(ctx, &testpb.Empty{}); err != nil { if _, err := tc.EmptyCall(ctx, &testpb.Empty{}); err != nil {
t.Fatalf("TestService/EmptyCall(_,_) = _, %v, want _, nil", err) t.Fatalf("TestService/EmptyCall(_,_) = _, %v, want _, nil", err)