From 2fb1ac854b2037b408121870f0dcc81474ca483b Mon Sep 17 00:00:00 2001 From: Shihao Xia Date: Wed, 5 Jan 2022 13:38:18 -0500 Subject: [PATCH] test: fix potential goroutine leak in TestUpdateAddresses_RetryFromFirstAddr (#5023) --- clientconn_test.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/clientconn_test.go b/clientconn_test.go index 7d6a40adb831..ee39370a87fb 100644 --- a/clientconn_test.go +++ b/clientconn_test.go @@ -859,12 +859,15 @@ func (s) TestUpdateAddresses_RetryFromFirstAddr(t *testing.T) { defer lis3.Close() closeServer2 := make(chan struct{}) + exitCh := make(chan struct{}) server1ContactedFirstTime := make(chan struct{}) server1ContactedSecondTime := make(chan struct{}) server2ContactedFirstTime := make(chan struct{}) server2ContactedSecondTime := make(chan struct{}) server3Contacted := make(chan struct{}) + defer close(exitCh) + // Launch server 1. go func() { // First, let's allow the initial connection to go READY. We need to do @@ -888,12 +891,18 @@ func (s) TestUpdateAddresses_RetryFromFirstAddr(t *testing.T) { // until balancer is built to process the addresses. stateNotifications := testBalancerBuilder.nextStateNotifier() // Wait for the transport to become ready. - for s := range stateNotifications { - if s == connectivity.Ready { - break + for { + select { + case st := <-stateNotifications: + if st == connectivity.Ready { + goto ready + } + case <-exitCh: + return } } + ready: // Once it's ready, curAddress has been set. So let's close this // connection prompting the first reconnect cycle. conn1.Close()