mirror of
https://github.com/golang/net.git
synced 2026-03-31 10:27:08 +09:00
http2: modernize TestTransportRoundtripCloseOnWriteError
Rewrite this test to use a testClientConn and fake network, allowing us to inject its network error into the fake net rather than by twiddling the client connection internals. Change-Id: Idcd96498ceaee701ad0c053dc0c6ce74701cc182 Reviewed-on: https://go-review.googlesource.com/c/net/+/701006 Auto-Submit: Damien Neil <dneil@google.com> Reviewed-by: Nicholas Husin <husin@google.com> Reviewed-by: Nicholas Husin <nsh@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
committed by
Gopher Robot
parent
6dc6880bcd
commit
96e405cac1
@@ -194,6 +194,13 @@ func (tc *testClientConn) closeWrite() {
|
||||
tc.netconn.Close()
|
||||
}
|
||||
|
||||
// closeWrite causes the net.Conn used by the ClientConn to return a error
|
||||
// from Write calls.
|
||||
func (tc *testClientConn) closeWriteWithError(err error) {
|
||||
tc.netconn.loc.setReadError(io.EOF)
|
||||
tc.netconn.loc.setWriteError(err)
|
||||
}
|
||||
|
||||
// testRequestBody is a Request.Body for use in tests.
|
||||
type testRequestBody struct {
|
||||
tc *testClientConn
|
||||
|
||||
@@ -4243,35 +4243,28 @@ func TestTransportNewClientConnCloseOnWriteError(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestTransportRoundtripCloseOnWriteError(t *testing.T) {
|
||||
req, err := http.NewRequest("GET", "https://dummy.tld/", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ts := newTestServer(t, func(w http.ResponseWriter, r *http.Request) {})
|
||||
synctestTest(t, testTransportRoundtripCloseOnWriteError)
|
||||
}
|
||||
func testTransportRoundtripCloseOnWriteError(t testing.TB) {
|
||||
tc := newTestClientConn(t)
|
||||
tc.greet()
|
||||
|
||||
tr := &Transport{TLSClientConfig: tlsConfigInsecure}
|
||||
defer tr.CloseIdleConnections()
|
||||
ctx := context.Background()
|
||||
cc, err := tr.dialClientConn(ctx, ts.Listener.Addr().String(), false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
body := tc.newRequestBody()
|
||||
body.writeBytes(1)
|
||||
req, _ := http.NewRequest("GET", "https://dummy.tld/", body)
|
||||
rt := tc.roundTrip(req)
|
||||
|
||||
writeErr := errors.New("write error")
|
||||
cc.wmu.Lock()
|
||||
cc.werr = writeErr
|
||||
cc.wmu.Unlock()
|
||||
tc.closeWriteWithError(writeErr)
|
||||
|
||||
_, err = cc.RoundTrip(req)
|
||||
if err != writeErr {
|
||||
t.Fatalf("expected %v, got %v", writeErr, err)
|
||||
body.writeBytes(1)
|
||||
if err := rt.err(); err != writeErr {
|
||||
t.Fatalf("RoundTrip error %v, want %v", err, writeErr)
|
||||
}
|
||||
|
||||
cc.mu.Lock()
|
||||
closed := cc.closed
|
||||
cc.mu.Unlock()
|
||||
if !closed {
|
||||
t.Fatal("expected closed")
|
||||
rt2 := tc.roundTrip(req)
|
||||
if err := rt2.err(); err != errClientConnUnusable {
|
||||
t.Fatalf("RoundTrip error %v, want errClientConnUnusable", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user