net/http: eliminate more arbitrary timeouts in tests

Change-Id: I5b3158ecd0eb20dc433a53a2b03eb4551cbb3f7d
Reviewed-on: https://go-review.googlesource.com/c/go/+/477196
Auto-Submit: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Bryan Mills <bcmills@google.com>
Reviewed-by: Damien Neil <dneil@google.com>
This commit is contained in:
Bryan C. Mills 2023-03-17 12:42:02 -04:00 committed by Gopher Robot
parent 2449bbb5e6
commit 729c05d065
4 changed files with 453 additions and 457 deletions

View File

@ -1283,24 +1283,28 @@ func testInterruptWithPanic(t *testing.T, mode testMode, panicValue any) {
}
wantStackLogged := panicValue != nil && panicValue != ErrAbortHandler
if err := waitErrCondition(5*time.Second, 10*time.Millisecond, func() error {
waitCondition(t, 10*time.Millisecond, func(d time.Duration) bool {
gotLog := logOutput()
if !wantStackLogged {
if gotLog == "" {
return nil
return true
}
return fmt.Errorf("want no log output; got: %s", gotLog)
t.Fatalf("want no log output; got: %s", gotLog)
}
if gotLog == "" {
return fmt.Errorf("wanted a stack trace logged; got nothing")
if d > 0 {
t.Logf("wanted a stack trace logged; got nothing after %v", d)
}
return false
}
if !strings.Contains(gotLog, "created by ") && strings.Count(gotLog, "\n") < 6 {
return fmt.Errorf("output doesn't look like a panic stack trace. Got: %s", gotLog)
if d > 0 {
t.Logf("output doesn't look like a panic stack trace after %v. Got: %s", d, gotLog)
}
return false
}
return nil
}); err != nil {
t.Fatal(err)
}
return true
})
}
type lockedBytesBuffer struct {

View File

@ -140,29 +140,15 @@ func afterTest(t testing.TB) {
t.Errorf("Test appears to have leaked %s:\n%s", bad, stacks)
}
// waitCondition reports whether fn eventually returned true,
// checking immediately and then every checkEvery amount,
// until waitFor has elapsed, at which point it returns false.
func waitCondition(waitFor, checkEvery time.Duration, fn func() bool) bool {
deadline := time.Now().Add(waitFor)
for time.Now().Before(deadline) {
if fn() {
return true
}
time.Sleep(checkEvery)
// waitCondition waits for fn to return true,
// checking immediately and then at exponentially increasing intervals.
func waitCondition(t testing.TB, delay time.Duration, fn func(time.Duration) bool) {
t.Helper()
start := time.Now()
var since time.Duration
for !fn(since) {
time.Sleep(delay)
delay = 2*delay - (delay / 2) // 1.5x, rounded up
since = time.Since(start)
}
return false
}
// waitErrCondition is like waitCondition but with errors instead of bools.
func waitErrCondition(waitFor, checkEvery time.Duration, fn func() error) error {
deadline := time.Now().Add(waitFor)
var err error
for time.Now().Before(deadline) {
if err = fn(); err == nil {
return nil
}
time.Sleep(checkEvery)
}
return err
}

View File

@ -5439,12 +5439,16 @@ func testServerSetKeepAlivesEnabledClosesConns(t *testing.T, mode testMode) {
ts.Config.SetKeepAlivesEnabled(false)
var idle1 int
if !waitCondition(2*time.Second, 10*time.Millisecond, func() bool {
waitCondition(t, 10*time.Millisecond, func(d time.Duration) bool {
idle1 = tr.IdleConnCountForTesting("http", addr)
return idle1 == 0
}) {
t.Fatalf("idle count after SetKeepAlivesEnabled called = %v; want 0", idle1)
}
if idle1 != 0 {
if d > 0 {
t.Logf("idle count %v after SetKeepAlivesEnabled called = %v; waiting for 0", d, idle1)
}
return false
}
return true
})
a3 := get()
if a3 == a2 {
@ -5604,9 +5608,15 @@ func testServerKeepAlivesEnabled(t *testing.T, mode testMode) {
srv := cst.ts.Config
srv.SetKeepAlivesEnabled(false)
for try := 0; try < 2; try++ {
if !waitCondition(2*time.Second, 10*time.Millisecond, srv.ExportAllConnsIdle) {
t.Fatalf("request %v: test server has active conns", try)
}
waitCondition(t, 10*time.Millisecond, func(d time.Duration) bool {
if !srv.ExportAllConnsIdle() {
if d > 0 {
t.Logf("test server still has active conns after %v", d)
}
return false
}
return true
})
conns := 0
var info httptrace.GotConnInfo
ctx := httptrace.WithClientTrace(context.Background(), &httptrace.ClientTrace{

File diff suppressed because it is too large Load Diff