Skip to content

Commit 4e2915b

Browse files
neildgopherbot
authored andcommitted
http2: modernize TestTransportAllocationsAfterResponseBodyClose
This is a transport test. Use a testClientConn rather than a Transport attached to a test server, for better control over the frames sent to the test transport. Drop one section of the test which pokes into the response body's transportResponseBody type, as being too coupled to the implementation internals. Change-Id: I7bc7c7c756fd0c596424fab9a892dda8d9e89d1c Reviewed-on: https://go-review.googlesource.com/c/net/+/701004 Reviewed-by: Nicholas Husin <[email protected]> Reviewed-by: Nicholas Husin <[email protected]> Auto-Submit: Damien Neil <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 30b0e78 commit 4e2915b

File tree

1 file changed

+28
-36
lines changed

1 file changed

+28
-36
lines changed

http2/transport_test.go

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3624,53 +3624,45 @@ func TestAuthorityAddr(t *testing.T) {
36243624
// Issue 20448: stop allocating for DATA frames' payload after
36253625
// Response.Body.Close is called.
36263626
func TestTransportAllocationsAfterResponseBodyClose(t *testing.T) {
3627-
megabyteZero := make([]byte, 1<<20)
3627+
synctestTest(t, testTransportAllocationsAfterResponseBodyClose)
3628+
}
3629+
func testTransportAllocationsAfterResponseBodyClose(t testing.TB) {
3630+
tc := newTestClientConn(t)
3631+
tc.greet()
36283632

3629-
writeErr := make(chan error, 1)
3633+
// Send request.
3634+
req, _ := http.NewRequest("PUT", "https://dummy.tld/", nil)
3635+
rt := tc.roundTrip(req)
3636+
tc.wantFrameType(FrameHeaders)
36303637

3631-
ts := newTestServer(t, func(w http.ResponseWriter, r *http.Request) {
3632-
w.(http.Flusher).Flush()
3633-
var sum int64
3634-
for i := 0; i < 100; i++ {
3635-
n, err := w.Write(megabyteZero)
3636-
sum += int64(n)
3637-
if err != nil {
3638-
writeErr <- err
3639-
return
3640-
}
3641-
}
3642-
t.Logf("wrote all %d bytes", sum)
3643-
writeErr <- nil
3638+
// Receive response with some body.
3639+
tc.writeHeaders(HeadersFrameParam{
3640+
StreamID: rt.streamID(),
3641+
EndHeaders: true,
3642+
EndStream: false,
3643+
BlockFragment: tc.makeHeaderBlockFragment(
3644+
":status", "200",
3645+
),
36443646
})
3647+
tc.writeData(rt.streamID(), false, make([]byte, 64))
3648+
tc.wantIdle()
36453649

3646-
tr := &Transport{TLSClientConfig: tlsConfigInsecure}
3647-
defer tr.CloseIdleConnections()
3648-
c := &http.Client{Transport: tr}
3649-
res, err := c.Get(ts.URL)
3650-
if err != nil {
3651-
t.Fatal(err)
3652-
}
3650+
// Client reads a byte of the body, and then closes it.
3651+
respBody := rt.response().Body
36533652
var buf [1]byte
3654-
if _, err := res.Body.Read(buf[:]); err != nil {
3653+
if _, err := respBody.Read(buf[:]); err != nil {
36553654
t.Error(err)
36563655
}
3657-
if err := res.Body.Close(); err != nil {
3656+
if err := respBody.Close(); err != nil {
36583657
t.Error(err)
36593658
}
3659+
tc.wantFrameType(FrameRSTStream)
36603660

3661-
trb, ok := res.Body.(transportResponseBody)
3662-
if !ok {
3663-
t.Fatalf("res.Body = %T; want transportResponseBody", res.Body)
3664-
}
3665-
if trb.cs.bufPipe.b != nil {
3666-
t.Errorf("response body pipe is still open")
3667-
}
3661+
// Server sends more of the body, which is ignored.
3662+
tc.writeData(rt.streamID(), false, make([]byte, 64))
36683663

3669-
gotErr := <-writeErr
3670-
if gotErr == nil {
3671-
t.Errorf("Handler unexpectedly managed to write its entire response without getting an error")
3672-
} else if gotErr != errStreamClosed {
3673-
t.Errorf("Handler Write err = %v; want errStreamClosed", gotErr)
3664+
if _, err := respBody.Read(buf[:]); err == nil {
3665+
t.Error("read from closed body unexpectedly succeeded")
36743666
}
36753667
}
36763668

0 commit comments

Comments
 (0)