Skip to content

Commit

Permalink
net/http: adaptive wait time in PersistConnLeak tests
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Richard Miller authored and bradfitz committed Mar 20, 2016
1 parent a7a1999 commit 34f0c0b
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/net/http/transport_test.go
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 34f0c0b

Please sign in to comment.