From e79d2381fc26902df77103b6a154e8729774a86e Mon Sep 17 00:00:00 2001 From: Anis Elleuch Date: Thu, 1 Sep 2016 00:07:44 +0100 Subject: [PATCH] Fix rare 'go test -race' failure in ListenServe{Plain,TLS} (#2588) --- cmd/server-mux_test.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/cmd/server-mux_test.go b/cmd/server-mux_test.go index aaaaca93f..221b6052f 100644 --- a/cmd/server-mux_test.go +++ b/cmd/server-mux_test.go @@ -153,7 +153,7 @@ func TestServerCloseBlocking(t *testing.T) { m.mu.Unlock() } -func TestListenAndServe(t *testing.T) { +func TestListenAndServePlain(t *testing.T) { wait := make(chan struct{}) addr := "127.0.0.1:" + strconv.Itoa(getFreePort()) errc := make(chan error) @@ -171,6 +171,8 @@ func TestListenAndServe(t *testing.T) { // Make sure we don't block by closing wait after a timeout tf := time.AfterFunc(time.Millisecond*500, func() { errc <- errors.New("Unable to connect to server") }) + wg := &sync.WaitGroup{} + wg.Add(1) // Keep trying the server until it's accepting connections go func() { client := http.Client{Timeout: time.Millisecond * 10} @@ -182,9 +184,12 @@ func TestListenAndServe(t *testing.T) { } } + wg.Done() tf.Stop() // Cancel the timeout since we made a successful request }() + wg.Wait() + // Block until we get an error or wait closed select { case err := <-errc: @@ -230,6 +235,8 @@ func TestListenAndServeTLS(t *testing.T) { // Make sure we don't block by closing wait after a timeout tf := time.AfterFunc(time.Millisecond*500, func() { errc <- errors.New("Unable to connect to server") }) + wg := &sync.WaitGroup{} + wg.Add(1) // Keep trying the server until it's accepting connections go func() { @@ -248,9 +255,12 @@ func TestListenAndServeTLS(t *testing.T) { } } + wg.Done() tf.Stop() // Cancel the timeout since we made a successful request }() + wg.Wait() + // Block until we get an error or wait closed select { case err := <-errc: