Skip to content

Commit 2ed6ce1

Browse files
author
Bryan C. Mills
committed
http2: in TestContentEncodingNoSniffing, do not allow the Transport to outlive the serverTester
This test constructed a new serverTester for each subtest, but reused a Transport across all of the tests. I don't fully understand why, but for some reason that occasionally led to "connection reset by peer" failures when the port was reused. Scoping the Transport to the lifetime of the serverTester seems to resolve the errors: I tested with go test ./http2 -run=TestContentEncodingNoSniffing -httptest.serve=127.0.0.1:8080 -count=10000 and got lots of failures prior to this change and none after. Fixes golang/go#46762 (I hope.) Change-Id: I385c077c1d8627e42ce4d2db041878aacb5452fb Reviewed-on: https://go-review.googlesource.com/c/net/+/380154 Trust: Bryan Mills <[email protected]> Run-TryBot: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Damien Neil <[email protected]>
1 parent 4395403 commit 2ed6ce1

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

http2/server_test.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4212,9 +4212,6 @@ func TestContentEncodingNoSniffing(t *testing.T) {
42124212
},
42134213
}
42144214

4215-
tr := &Transport{TLSClientConfig: tlsConfigInsecure}
4216-
defer tr.CloseIdleConnections()
4217-
42184215
for _, tt := range resps {
42194216
t.Run(tt.name, func(t *testing.T) {
42204217
st := newServerTester(t, func(w http.ResponseWriter, r *http.Request) {
@@ -4225,22 +4222,32 @@ func TestContentEncodingNoSniffing(t *testing.T) {
42254222
}, optOnlyServer)
42264223
defer st.Close()
42274224

4225+
tr := &Transport{TLSClientConfig: tlsConfigInsecure}
4226+
defer tr.CloseIdleConnections()
4227+
42284228
req, _ := http.NewRequest("GET", st.ts.URL, nil)
42294229
res, err := tr.RoundTrip(req)
42304230
if err != nil {
4231-
t.Fatalf("Failed to fetch URL: %v", err)
4231+
t.Fatalf("GET %s: %v", st.ts.URL, err)
42324232
}
42334233
defer res.Body.Close()
4234-
if g, w := res.Header.Get("Content-Encoding"), tt.contentEncoding; g != w {
4234+
4235+
g := res.Header.Get("Content-Encoding")
4236+
t.Logf("%s: Content-Encoding: %s", st.ts.URL, g)
4237+
4238+
if w := tt.contentEncoding; g != w {
42354239
if w != nil { // The case where contentEncoding was set explicitly.
42364240
t.Errorf("Content-Encoding mismatch\n\tgot: %q\n\twant: %q", g, w)
42374241
} else if g != "" { // "" should be the equivalent when the contentEncoding is unset.
42384242
t.Errorf("Unexpected Content-Encoding %q", g)
42394243
}
42404244
}
4241-
if g, w := res.Header.Get("Content-Type"), tt.wantContentType; g != w {
4245+
4246+
g = res.Header.Get("Content-Type")
4247+
if w := tt.wantContentType; g != w {
42424248
t.Errorf("Content-Type mismatch\n\tgot: %q\n\twant: %q", g, w)
42434249
}
4250+
t.Logf("%s: Content-Type: %s", st.ts.URL, g)
42444251
})
42454252
}
42464253
}

0 commit comments

Comments
 (0)