http2: deflake TestTransportReuseAfterError

This test issues a request with a short timeout, and expects that
the request timing out will result in the connection it was sent
on being marked as unusable.

However, it is possible for the request to time out before it is
sent, with no effect on the connection. The test's next request
then uses the same connection and hangs.

Rather than a timeout, cancel the request after it is received
on the server.

Fixes golang/go#59934

Change-Id: I1144686377158d0654e0f91a1b0312021a02a01d
Reviewed-on: https://go-review.googlesource.com/c/net/+/496055
Reviewed-by: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Damien Neil <dneil@google.com>
diff --git a/http2/transport_test.go b/http2/transport_test.go
index 54d4551..68e17fd 100644
--- a/http2/transport_test.go
+++ b/http2/transport_test.go
@@ -6434,15 +6434,17 @@
 
 	// Request 2 is also made on conn 1.
 	// Reading the response will block.
-	// The request fails when the context deadline expires.
+	// The request is canceled once the server receives it.
 	// Conn 1 should now be flagged as unfit for reuse.
-	timeoutCtx, cancel := context.WithTimeout(context.Background(), 1*time.Millisecond)
-	defer cancel()
-	_, err := tr.RoundTrip(req.Clone(timeoutCtx))
+	req2Ctx, cancel := context.WithCancel(context.Background())
+	go func() {
+		<-serverReqc
+		cancel()
+	}()
+	_, err := tr.RoundTrip(req.Clone(req2Ctx))
 	if err == nil {
-		t.Errorf("request 2 unexpectedly succeeded (want timeout)")
+		t.Errorf("request 2 unexpectedly succeeded (want cancel)")
 	}
-	time.Sleep(1 * time.Millisecond)
 
 	// Request 3 is made on a new conn, and succeeds.
 	res3, err := tr.RoundTrip(req.Clone(context.Background()))