Skip to content

Commit 1011e26

Browse files
committed
net/http: deflake TestServerKeepAlivesEnabled_h{1,2}
This test assumes that two successive TCP connections will use different source ports. This does not appear to be a universally safe assumption. Rewrite the test to use httptrace to detect connection reuse instead. Fixes #46707 Change-Id: Iebfbdfdeb77a1e6663a0c654dc847cc270c5d54d Reviewed-on: https://go-review.googlesource.com/c/go/+/360854 Trust: Damien Neil <[email protected]> Run-TryBot: Damien Neil <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent 80a7968 commit 1011e26

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

src/net/http/serve_test.go

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"net"
2424
. "net/http"
2525
"net/http/httptest"
26+
"net/http/httptrace"
2627
"net/http/httputil"
2728
"net/http/internal"
2829
"net/http/internal/testcert"
@@ -5689,22 +5690,37 @@ func testServerKeepAlivesEnabled(t *testing.T, h2 bool) {
56895690
}
56905691
// Not parallel: messes with global variable. (http2goAwayTimeout)
56915692
defer afterTest(t)
5692-
cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
5693-
fmt.Fprintf(w, "%v", r.RemoteAddr)
5694-
}))
5693+
cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {}))
56955694
defer cst.close()
56965695
srv := cst.ts.Config
56975696
srv.SetKeepAlivesEnabled(false)
5698-
a := cst.getURL(cst.ts.URL)
5699-
if !waitCondition(2*time.Second, 10*time.Millisecond, srv.ExportAllConnsIdle) {
5700-
t.Fatalf("test server has active conns")
5701-
}
5702-
b := cst.getURL(cst.ts.URL)
5703-
if a == b {
5704-
t.Errorf("got same connection between first and second requests")
5705-
}
5706-
if !waitCondition(2*time.Second, 10*time.Millisecond, srv.ExportAllConnsIdle) {
5707-
t.Fatalf("test server has active conns")
5697+
for try := 0; try < 2; try++ {
5698+
if !waitCondition(2*time.Second, 10*time.Millisecond, srv.ExportAllConnsIdle) {
5699+
t.Fatalf("request %v: test server has active conns", try)
5700+
}
5701+
conns := 0
5702+
var info httptrace.GotConnInfo
5703+
ctx := httptrace.WithClientTrace(context.Background(), &httptrace.ClientTrace{
5704+
GotConn: func(v httptrace.GotConnInfo) {
5705+
conns++
5706+
info = v
5707+
},
5708+
})
5709+
req, err := NewRequestWithContext(ctx, "GET", cst.ts.URL, nil)
5710+
if err != nil {
5711+
t.Fatal(err)
5712+
}
5713+
res, err := cst.c.Do(req)
5714+
if err != nil {
5715+
t.Fatal(err)
5716+
}
5717+
res.Body.Close()
5718+
if conns != 1 {
5719+
t.Fatalf("request %v: got %v conns, want 1", try, conns)
5720+
}
5721+
if info.Reused || info.WasIdle {
5722+
t.Fatalf("request %v: Reused=%v (want false), WasIdle=%v (want false)", try, info.Reused, info.WasIdle)
5723+
}
57085724
}
57095725
}
57105726

0 commit comments

Comments
 (0)