diff --git a/src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.Http2.cs b/src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.Http2.cs index 89bb4a9ce9d5b..6f513bd363c35 100644 --- a/src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.Http2.cs +++ b/src/libraries/System.Net.Http/tests/FunctionalTests/HttpClientHandlerTest.Http2.cs @@ -1636,6 +1636,7 @@ public async Task Http2_PendingSend_Cancellation() public async Task Http2_PendingSend_SendsReset(bool waitForData) { var cts = new CancellationTokenSource(); + var rstReceived = new TaskCompletionSource(); string content = new string('*', 300); var stream = new CustomContent.SlowTestStream(Encoding.UTF8.GetBytes(content), null, count: 60); @@ -1649,8 +1650,8 @@ await Http2LoopbackServer.CreateClientAndServerAsync(async url => await Assert.ThrowsAnyAsync(async () => await client.SendAsync(request, cts.Token)); - // Delay for a bit to ensure that the RST_STREAM for the previous request is sent before the next request starts. - await Task.Delay(2000); + // Wait until the RST_STREAM for the previous request is received before the next request starts. + await rstReceived.Task.TimeoutAfter(TimeSpan.FromSeconds(60)); // Send another request to verify that connection is still functional. request = new HttpRequestMessage(HttpMethod.Get, url); @@ -1678,7 +1679,9 @@ await Http2LoopbackServer.CreateClientAndServerAsync(async url => frameCount++; } while (frame.Type != FrameType.RstStream); - Assert.Equal(1, frame.StreamId); + Assert.Equal(1, frame.StreamId); + + rstReceived.SetResult(true); frame = null; (streamId, requestData) = await connection.ReadAndParseRequestHeaderAsync();