aboutsummaryrefslogtreecommitdiff
path: root/src/net/http/clientserver_test.go
diff options
context:
space:
mode:
authorDamien Neil <dneil@google.com>2024-11-25 11:27:33 -0800
committerGopher Robot <gobot@golang.org>2024-11-26 18:05:09 +0000
commit04879acdebbb08bdca00356f043d769c4b4375ce (patch)
treed30972224b07961b1946c198df6d17b792f264e3 /src/net/http/clientserver_test.go
parent592da0ba474b94b6eceee62b5613f1c9c1ed9c89 (diff)
downloadgo-04879acdebbb08bdca00356f043d769c4b4375ce.tar.xz
net/http: test for racing idle conn closure and new requests
TestTransportRemovesH2ConnsAfterIdle is experiencing flaky failures due to a bug in idle connection handling. Upon inspection, TestTransportRemovesH2ConnsAfterIdle is slow and (I think) not currently testing the condition that it was added to test. Using the new synctest package, this CL: - Adds a test for the failure causing flakes in this test. - Rewrites the existing test to use synctest to avoid sleeps. - Adds a new test that covers the condition the test was intended to examine. The new TestTransportIdleConnRacesRequest exercises the scenario where a never-used connection is closed by the idle-conn timer at the same time as a new request attempts to use it. In this race, the new request should either successfully use the old connection (superseding the idle timer) or should use a new connection; it should not use the closing connection and fail. TestTransportRemovesConnsAfterIdle verifies that a connection is reused before the idle timer expires, and not reused after. TestTransportRemovesConnsAfterBroken verifies that a connection is not reused after it encounters an error. This exercises the bug fixed in CL 196665, which introduced TestTransportRemovesH2ConnsAfterIdle. For #70515 Change-Id: Id23026d2903fb15ef9a831b2df71177ea177b096 Reviewed-on: https://go-review.googlesource.com/c/go/+/631795 Reviewed-by: Jonathan Amsterdam <jba@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Damien Neil <dneil@google.com>
Diffstat (limited to 'src/net/http/clientserver_test.go')
-rw-r--r--src/net/http/clientserver_test.go65
1 files changed, 57 insertions, 8 deletions
diff --git a/src/net/http/clientserver_test.go b/src/net/http/clientserver_test.go
index 0873038757..32d97ea9f0 100644
--- a/src/net/http/clientserver_test.go
+++ b/src/net/http/clientserver_test.go
@@ -40,9 +40,10 @@ import (
type testMode string
const (
- http1Mode = testMode("h1") // HTTP/1.1
- https1Mode = testMode("https1") // HTTPS/1.1
- http2Mode = testMode("h2") // HTTP/2
+ http1Mode = testMode("h1") // HTTP/1.1
+ https1Mode = testMode("https1") // HTTPS/1.1
+ http2Mode = testMode("h2") // HTTP/2
+ http2UnencryptedMode = testMode("h2unencrypted") // HTTP/2
)
type testNotParallelOpt struct{}
@@ -132,6 +133,7 @@ type clientServerTest struct {
ts *httptest.Server
tr *Transport
c *Client
+ li *fakeNetListener
}
func (t *clientServerTest) close() {
@@ -169,6 +171,8 @@ func optWithServerLog(lg *log.Logger) func(*httptest.Server) {
}
}
+var optFakeNet = new(struct{})
+
// newClientServerTest creates and starts an httptest.Server.
//
// The mode parameter selects the implementation to test:
@@ -180,6 +184,9 @@ func optWithServerLog(lg *log.Logger) func(*httptest.Server) {
//
// func(*httptest.Server) // run before starting the server
// func(*http.Transport)
+//
+// The optFakeNet option configures the server and client to use a fake network implementation,
+// suitable for use in testing/synctest tests.
func newClientServerTest(t testing.TB, mode testMode, h Handler, opts ...any) *clientServerTest {
if mode == http2Mode {
CondSkipHTTP2(t)
@@ -189,9 +196,31 @@ func newClientServerTest(t testing.TB, mode testMode, h Handler, opts ...any) *c
h2: mode == http2Mode,
h: h,
}
- cst.ts = httptest.NewUnstartedServer(h)
var transportFuncs []func(*Transport)
+
+ if idx := slices.Index(opts, any(optFakeNet)); idx >= 0 {
+ opts = slices.Delete(opts, idx, idx+1)
+ cst.li = fakeNetListen()
+ cst.ts = &httptest.Server{
+ Config: &Server{Handler: h},
+ Listener: cst.li,
+ }
+ transportFuncs = append(transportFuncs, func(tr *Transport) {
+ tr.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
+ return cst.li.connect(), nil
+ }
+ })
+ } else {
+ cst.ts = httptest.NewUnstartedServer(h)
+ }
+
+ if mode == http2UnencryptedMode {
+ p := &Protocols{}
+ p.SetUnencryptedHTTP2(true)
+ cst.ts.Config.Protocols = p
+ }
+
for _, opt := range opts {
switch opt := opt.(type) {
case func(*Transport):
@@ -212,6 +241,9 @@ func newClientServerTest(t testing.TB, mode testMode, h Handler, opts ...any) *c
cst.ts.Start()
case https1Mode:
cst.ts.StartTLS()
+ case http2UnencryptedMode:
+ ExportHttp2ConfigureServer(cst.ts.Config, nil)
+ cst.ts.Start()
case http2Mode:
ExportHttp2ConfigureServer(cst.ts.Config, nil)
cst.ts.TLS = cst.ts.Config.TLSConfig
@@ -221,7 +253,7 @@ func newClientServerTest(t testing.TB, mode testMode, h Handler, opts ...any) *c
}
cst.c = cst.ts.Client()
cst.tr = cst.c.Transport.(*Transport)
- if mode == http2Mode {
+ if mode == http2Mode || mode == http2UnencryptedMode {
if err := ExportHttp2ConfigureTransport(cst.tr); err != nil {
t.Fatal(err)
}
@@ -229,6 +261,13 @@ func newClientServerTest(t testing.TB, mode testMode, h Handler, opts ...any) *c
for _, f := range transportFuncs {
f(cst.tr)
}
+
+ if mode == http2UnencryptedMode {
+ p := &Protocols{}
+ p.SetUnencryptedHTTP2(true)
+ cst.tr.Protocols = p
+ }
+
t.Cleanup(func() {
cst.close()
})
@@ -246,9 +285,19 @@ func (w testLogWriter) Write(b []byte) (int, error) {
// Testing the newClientServerTest helper itself.
func TestNewClientServerTest(t *testing.T) {
- run(t, testNewClientServerTest, []testMode{http1Mode, https1Mode, http2Mode})
+ modes := []testMode{http1Mode, https1Mode, http2Mode}
+ t.Run("realnet", func(t *testing.T) {
+ run(t, func(t *testing.T, mode testMode) {
+ testNewClientServerTest(t, mode)
+ }, modes)
+ })
+ t.Run("synctest", func(t *testing.T) {
+ runSynctest(t, func(t testing.TB, mode testMode) {
+ testNewClientServerTest(t, mode, optFakeNet)
+ }, modes)
+ })
}
-func testNewClientServerTest(t *testing.T, mode testMode) {
+func testNewClientServerTest(t testing.TB, mode testMode, opts ...any) {
var got struct {
sync.Mutex
proto string
@@ -260,7 +309,7 @@ func testNewClientServerTest(t *testing.T, mode testMode) {
got.proto = r.Proto
got.hasTLS = r.TLS != nil
})
- cst := newClientServerTest(t, mode, h)
+ cst := newClientServerTest(t, mode, h, opts...)
if _, err := cst.c.Head(cst.ts.URL); err != nil {
t.Fatal(err)
}