Skip to content

Commit 547eb8d

Browse files
Bryan C. Millsgopherbot
Bryan C. Mills
authored andcommitted
net/http: remove arbitrary timeouts in tests of Server.ErrorLog
This also allows us to remove the chanWriter helper from the test, using a simpler strings.Builder instead, relying on clientServerTest.close for synchronization. (I don't think this runs afoul of #38370, because the handler functions themselves in these tests should never be executed, let alone result in an asynchronous write to the error log.) Fixes #57599. Change-Id: I45c6cefca0bb218f6f9a9659de6bde454547f704 Reviewed-on: https://go-review.googlesource.com/c/go/+/539436 Run-TryBot: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Auto-Submit: Bryan Mills <[email protected]> Reviewed-by: Damien Neil <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 39d3c4a commit 547eb8d

File tree

2 files changed

+25
-38
lines changed

2 files changed

+25
-38
lines changed

src/net/http/client_test.go

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,6 @@ func pedanticReadAll(r io.Reader) (b []byte, err error) {
6060
}
6161
}
6262

63-
type chanWriter chan string
64-
65-
func (w chanWriter) Write(p []byte) (n int, err error) {
66-
w <- string(p)
67-
return len(p), nil
68-
}
69-
7063
func TestClient(t *testing.T) { run(t, testClient) }
7164
func testClient(t *testing.T, mode testMode) {
7265
ts := newClientServerTest(t, mode, robotsTxtHandler).ts
@@ -827,12 +820,12 @@ func TestClientInsecureTransport(t *testing.T) {
827820
run(t, testClientInsecureTransport, []testMode{https1Mode, http2Mode})
828821
}
829822
func testClientInsecureTransport(t *testing.T, mode testMode) {
830-
ts := newClientServerTest(t, mode, HandlerFunc(func(w ResponseWriter, r *Request) {
823+
cst := newClientServerTest(t, mode, HandlerFunc(func(w ResponseWriter, r *Request) {
831824
w.Write([]byte("Hello"))
832-
})).ts
833-
errc := make(chanWriter, 10) // but only expecting 1
834-
ts.Config.ErrorLog = log.New(errc, "", 0)
835-
defer ts.Close()
825+
}))
826+
ts := cst.ts
827+
errLog := new(strings.Builder)
828+
ts.Config.ErrorLog = log.New(errLog, "", 0)
836829

837830
// TODO(bradfitz): add tests for skipping hostname checks too?
838831
// would require a new cert for testing, and probably
@@ -851,15 +844,10 @@ func testClientInsecureTransport(t *testing.T, mode testMode) {
851844
}
852845
}
853846

854-
select {
855-
case v := <-errc:
856-
if !strings.Contains(v, "TLS handshake error") {
857-
t.Errorf("expected an error log message containing 'TLS handshake error'; got %q", v)
858-
}
859-
case <-time.After(5 * time.Second):
860-
t.Errorf("timeout waiting for logged error")
847+
cst.close()
848+
if !strings.Contains(errLog.String(), "TLS handshake error") {
849+
t.Errorf("expected an error log message containing 'TLS handshake error'; got %q", errLog)
861850
}
862-
863851
}
864852

865853
func TestClientErrorWithRequestURI(t *testing.T) {
@@ -897,9 +885,10 @@ func TestClientWithIncorrectTLSServerName(t *testing.T) {
897885
run(t, testClientWithIncorrectTLSServerName, []testMode{https1Mode, http2Mode})
898886
}
899887
func testClientWithIncorrectTLSServerName(t *testing.T, mode testMode) {
900-
ts := newClientServerTest(t, mode, HandlerFunc(func(w ResponseWriter, r *Request) {})).ts
901-
errc := make(chanWriter, 10) // but only expecting 1
902-
ts.Config.ErrorLog = log.New(errc, "", 0)
888+
cst := newClientServerTest(t, mode, HandlerFunc(func(w ResponseWriter, r *Request) {}))
889+
ts := cst.ts
890+
errLog := new(strings.Builder)
891+
ts.Config.ErrorLog = log.New(errLog, "", 0)
903892

904893
c := ts.Client()
905894
c.Transport.(*Transport).TLSClientConfig.ServerName = "badserver"
@@ -910,13 +899,10 @@ func testClientWithIncorrectTLSServerName(t *testing.T, mode testMode) {
910899
if !strings.Contains(err.Error(), "127.0.0.1") || !strings.Contains(err.Error(), "badserver") {
911900
t.Errorf("wanted error mentioning 127.0.0.1 and badserver; got error: %v", err)
912901
}
913-
select {
914-
case v := <-errc:
915-
if !strings.Contains(v, "TLS handshake error") {
916-
t.Errorf("expected an error log message containing 'TLS handshake error'; got %q", v)
917-
}
918-
case <-time.After(5 * time.Second):
919-
t.Errorf("timeout waiting for logged error")
902+
903+
cst.close()
904+
if !strings.Contains(errLog.String(), "TLS handshake error") {
905+
t.Errorf("expected an error log message containing 'TLS handshake error'; got %q", errLog)
920906
}
921907
}
922908

src/net/http/serve_test.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,27 +1400,28 @@ func TestTLSHandshakeTimeout(t *testing.T) {
14001400
run(t, testTLSHandshakeTimeout, []testMode{https1Mode, http2Mode})
14011401
}
14021402
func testTLSHandshakeTimeout(t *testing.T, mode testMode) {
1403-
errc := make(chanWriter, 10) // but only expecting 1
1404-
ts := newClientServerTest(t, mode, HandlerFunc(func(w ResponseWriter, r *Request) {}),
1403+
errLog := new(strings.Builder)
1404+
cst := newClientServerTest(t, mode, HandlerFunc(func(w ResponseWriter, r *Request) {}),
14051405
func(ts *httptest.Server) {
14061406
ts.Config.ReadTimeout = 250 * time.Millisecond
1407-
ts.Config.ErrorLog = log.New(errc, "", 0)
1407+
ts.Config.ErrorLog = log.New(errLog, "", 0)
14081408
},
1409-
).ts
1409+
)
1410+
ts := cst.ts
1411+
14101412
conn, err := net.Dial("tcp", ts.Listener.Addr().String())
14111413
if err != nil {
14121414
t.Fatalf("Dial: %v", err)
14131415
}
1414-
defer conn.Close()
1415-
14161416
var buf [1]byte
14171417
n, err := conn.Read(buf[:])
14181418
if err == nil || n != 0 {
14191419
t.Errorf("Read = %d, %v; want an error and no bytes", n, err)
14201420
}
1421+
conn.Close()
14211422

1422-
v := <-errc
1423-
if !strings.Contains(v, "timeout") && !strings.Contains(v, "TLS handshake") {
1423+
cst.close()
1424+
if v := errLog.String(); !strings.Contains(v, "timeout") && !strings.Contains(v, "TLS handshake") {
14241425
t.Errorf("expected a TLS handshake timeout error; got %q", v)
14251426
}
14261427
}

0 commit comments

Comments
 (0)