]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: better failure in TestTransportPersistConnLeak
authorRuss Cox <rsc@golang.org>
Fri, 6 Jan 2017 06:17:24 +0000 (01:17 -0500)
committerRuss Cox <rsc@golang.org>
Fri, 6 Jan 2017 16:28:57 +0000 (16:28 +0000)
If one of the c.Get(ts.URL) results in an error, the child goroutine
calls t.Errorf, but the test goroutine gets stuck waiting for <-gotReqCh,
so the test hangs and the program is eventually killed (after 10 minutes!).
Whatever might have been printed to t.Errorf is never seen.
Adjust test so that the test fails cleanly in this case.

Still trying to debug why c.Get might fail.
It seems to have something to do with occasional connection
failures on macOS Sierra.

Change-Id: Ia797787bd51ea7cd6deb1192aec89c331c4f2c48
Reviewed-on: https://go-review.googlesource.com/34836
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/net/http/transport_test.go

index 5a402657cc3f5357626ea7e8ebf1c0bdd1517b6d..d5ddf6a1232a019e97d1d48f9f423d58d8af7dc0 100644 (file)
@@ -1083,8 +1083,10 @@ func waitNumGoroutine(nmax int) int {
 func TestTransportPersistConnLeak(t *testing.T) {
        // Not parallel: counts goroutines
        defer afterTest(t)
-       gotReqCh := make(chan bool)
-       unblockCh := make(chan bool)
+
+       const numReq = 25
+       gotReqCh := make(chan bool, numReq)
+       unblockCh := make(chan bool, numReq)
        ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
                gotReqCh <- true
                <-unblockCh
@@ -1098,14 +1100,15 @@ func TestTransportPersistConnLeak(t *testing.T) {
 
        n0 := runtime.NumGoroutine()
 
-       const numReq = 25
-       didReqCh := make(chan bool)
+       didReqCh := make(chan bool, numReq)
+       failed := make(chan bool, numReq)
        for i := 0; i < numReq; i++ {
                go func() {
                        res, err := c.Get(ts.URL)
                        didReqCh <- true
                        if err != nil {
                                t.Errorf("client fetch error: %v", err)
+                               failed <- true
                                return
                        }
                        res.Body.Close()
@@ -1114,7 +1117,13 @@ func TestTransportPersistConnLeak(t *testing.T) {
 
        // Wait for all goroutines to be stuck in the Handler.
        for i := 0; i < numReq; i++ {
-               <-gotReqCh
+               select {
+               case <-gotReqCh:
+                       // ok
+               case <-failed:
+                       close(unblockCh)
+                       return
+               }
        }
 
        nhigh := runtime.NumGoroutine()