From 34f0c0b3de8957f247c0bc99a682f622793fd88b Mon Sep 17 00:00:00 2001 From: Richard Miller Date: Sun, 20 Mar 2016 19:17:36 +0000 Subject: [PATCH] net/http: adaptive wait time in PersistConnLeak tests 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 TryBot-Result: Gobot Gobot Reviewed-by: Brad Fitzpatrick --- src/net/http/transport_test.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/net/http/transport_test.go b/src/net/http/transport_test.go index 9f17017651..63fa7ce6b1 100644 --- a/src/net/http/transport_test.go +++ b/src/net/http/transport_test.go @@ -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 -- 2.48.1