From 833680729394fcca4904ec569758c78b78411ee8 Mon Sep 17 00:00:00 2001 From: ZhouyihaiDing Date: Fri, 25 Aug 2017 13:10:07 -0700 Subject: [PATCH] the cancel function should be called to avoid ctx leak (#1465) --- balancer_test.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/balancer_test.go b/balancer_test.go index c4f4a144..8dd5c7c8 100644 --- a/balancer_test.go +++ b/balancer_test.go @@ -161,10 +161,11 @@ func TestEmptyAddrs(t *testing.T) { // Loop until the above updates apply. for { time.Sleep(10 * time.Millisecond) - ctx, _ := context.WithTimeout(context.Background(), 10*time.Millisecond) + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond) if err := Invoke(ctx, "/foo/bar", &expectedRequest, &reply, cc); err != nil { break } + cancel() } cc.Close() servers[0].stop() @@ -236,11 +237,12 @@ func TestCloseWithPendingRPC(t *testing.T) { r.w.inject(updates) // Loop until the above update applies. for { - ctx, _ := context.WithTimeout(context.Background(), 10*time.Millisecond) + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond) if err := Invoke(ctx, "/foo/bar", &expectedRequest, &reply, cc, FailFast(false)); Code(err) == codes.DeadlineExceeded { break } time.Sleep(10 * time.Millisecond) + cancel() } // Issue 2 RPCs which should be completed with error status once cc is closed. var wg sync.WaitGroup @@ -462,10 +464,11 @@ func TestPickFirstEmptyAddrs(t *testing.T) { // Loop until the above updates apply. for { time.Sleep(10 * time.Millisecond) - ctx, _ := context.WithTimeout(context.Background(), 10*time.Millisecond) + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond) if err := Invoke(ctx, "/foo/bar", &expectedRequest, &reply, cc); err != nil { break } + cancel() } } @@ -488,11 +491,12 @@ func TestPickFirstCloseWithPendingRPC(t *testing.T) { r.w.inject(updates) // Loop until the above update applies. for { - ctx, _ := context.WithTimeout(context.Background(), 10*time.Millisecond) + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond) if err := Invoke(ctx, "/foo/bar", &expectedRequest, &reply, cc, FailFast(false)); Code(err) == codes.DeadlineExceeded { break } time.Sleep(10 * time.Millisecond) + cancel() } // Issue 2 RPCs which should be completed with error status once cc is closed. var wg sync.WaitGroup