]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: adaptive wait time in PersistConnLeak tests
authorRichard Miller <miller.research@gmail.com>
Sun, 20 Mar 2016 19:17:36 +0000 (19:17 +0000)
committerBrad Fitzpatrick <bradfitz@golang.org>
Sun, 20 Mar 2016 22:53:45 +0000 (22:53 +0000)
In tests TransportPersistConnLeak and TransportPersistConnLeakShortBody,
there's a fixed wait time (100ms and 400ms respectively) to allow
goroutines to exit after CloseIdleConnections is called. This
is sometimes too short on a slow host running many simultaneous
tests.

This CL replaces the fixed sleep in each test with a sequence of
shorter sleeps, testing the number of remaining goroutines until
it reaches the threshold or an overall time limit of 500ms expires.
This prevents some failures in the plan9_arm builder, while reducing
the test time on faster machines.

Fixes #14887

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

index 9f17017651a55dac5421c818000dd87b88ad1c30..63fa7ce6b173ece21652b1664d0ee48186436321 100644 (file)
@@ -968,6 +968,17 @@ func TestTransportGzipShort(t *testing.T) {
        }
 }
 
+// Wait until number of goroutines is no greater than nmax, or time out.
+func waitNumGoroutine(nmax int) int {
+       nfinal := runtime.NumGoroutine()
+       for ntries := 10; ntries > 0 && nfinal > nmax; ntries-- {
+               time.Sleep(50 * time.Millisecond)
+               runtime.GC()
+               nfinal = runtime.NumGoroutine()
+       }
+       return nfinal
+}
+
 // tests that persistent goroutine connections shut down when no longer desired.
 func TestTransportPersistConnLeak(t *testing.T) {
        setParallel(t)
@@ -1019,10 +1030,7 @@ func TestTransportPersistConnLeak(t *testing.T) {
        }
 
        tr.CloseIdleConnections()
-       time.Sleep(100 * time.Millisecond)
-       runtime.GC()
-       runtime.GC() // even more.
-       nfinal := runtime.NumGoroutine()
+       nfinal := waitNumGoroutine(n0 + 5)
 
        growth := nfinal - n0
 
@@ -1061,9 +1069,7 @@ func TestTransportPersistConnLeakShortBody(t *testing.T) {
        }
        nhigh := runtime.NumGoroutine()
        tr.CloseIdleConnections()
-       time.Sleep(400 * time.Millisecond)
-       runtime.GC()
-       nfinal := runtime.NumGoroutine()
+       nfinal := waitNumGoroutine(n0 + 5)
 
        growth := nfinal - n0