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>
diff --git a/http2/clientconn_test.go b/http2/clientconn_test.go
index 60c7f4d..de2b935 100644
--- a/http2/clientconn_test.go
+++ b/http2/clientconn_test.go
@@ -194,6 +194,13 @@
 	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
diff --git a/http2/transport_test.go b/http2/transport_test.go
index a78c2b5..49aaf8c 100644
--- a/http2/transport_test.go
+++ b/http2/transport_test.go
@@ -4243,35 +4243,28 @@
 }
 
 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)
 	}
 }