net/http: deflake TestCancelRequestWhenSharingConnection

The test sleeps for 1 millisecond to give the cancellation a moment
to take effect. This is flaky because the request can finish before
the cancellation of the context is seen. It's easy to verify by adding

    time.Sleep(2*time.Millisecond)

after 0a6c4c8740/src/net/http/transport.go (L2619).
With this modification, the test fails about 5 times out of 10 runs.

The fix is easy. We just need to block the handler of the second
request until this request is cancelled. I have verify that the
updated test can uncover the issue fixed by CL 257818.
This commit is contained in:
Zeke Lu 2022-11-01 00:01:32 +08:00
parent 6774ddfec7
commit 99cb1c2eae
1 changed files with 4 additions and 3 deletions

View File

@ -6530,6 +6530,9 @@ func testCancelRequestWhenSharingConnection(t *testing.T, mode testMode) {
if !errors.Is(err, context.Canceled) {
t.Errorf("request 2: got err %v, want Canceled", err)
}
// Unblock the first request.
close(idlec)
}()
// Wait for the second request to arrive at the server, and then cancel
@ -6537,9 +6540,7 @@ func testCancelRequestWhenSharingConnection(t *testing.T, mode testMode) {
r2c := <-reqc
cancel()
// Give the cancellation a moment to take effect, and then unblock the first request.
time.Sleep(1 * time.Millisecond)
close(idlec)
<-idlec
close(r2c)
wg.Wait()