From 923ccac940985c6c0b0c365e5ce8dd7f368df510 Mon Sep 17 00:00:00 2001 From: ManickaP Date: Wed, 9 Jul 2025 14:19:26 +0200 Subject: [PATCH 1/2] Add synchronization into the test --- .../tests/System/Net/Http/HttpClientHandlerTest.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.cs b/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.cs index 8d950791269880..6ea54e0315e4d3 100644 --- a/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.cs +++ b/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.cs @@ -2173,6 +2173,7 @@ public async Task SendAsync_InvalidRequestUri_Throws() public async Task SendAsync_RequestWithDangerousControlHeaderValue_ThrowsHttpRequestException(char dangerousChar, HeaderType headerType) { TaskCompletionSource acceptConnection = new TaskCompletionSource(); + SemaphoreSlim clientFinished = new SemaphoreSlim(0); await LoopbackServerFactory.CreateClientAndServerAsync(async uri => { @@ -2216,6 +2217,7 @@ await LoopbackServerFactory.CreateClientAndServerAsync(async uri => // WinHTTP validates the input before opening connection whereas SocketsHttpHandler opens connection first and validates only when writing to the wire. acceptConnection.SetResult(!IsWinHttpHandler); var ex = await Assert.ThrowsAnyAsync(() => client.SendAsync(request)); + clientFinished.Release(); var hrex = Assert.IsType(ex); if (IsWinHttpHandler) { @@ -2231,7 +2233,13 @@ await LoopbackServerFactory.CreateClientAndServerAsync(async uri => { if (await acceptConnection.Task) { - await IgnoreExceptions(server.AcceptConnectionAsync(c => Task.CompletedTask)); + await IgnoreExceptions(async () => + { + await server.AcceptConnectionAsync(async _ => + { + await clientFinished.WaitAsync(TestHelper.PassingTestTimeout); + }); + }); } }); } From 6a8f21612e10b1e6d61a350af5993dd9086b1801 Mon Sep 17 00:00:00 2001 From: ManickaP Date: Thu, 17 Jul 2025 18:26:06 +0200 Subject: [PATCH 2/2] Feedback --- .../Common/tests/System/Net/Http/HttpClientHandlerTest.cs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.cs b/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.cs index 6ea54e0315e4d3..1d2830ef845dfb 100644 --- a/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.cs +++ b/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.cs @@ -2233,13 +2233,7 @@ await LoopbackServerFactory.CreateClientAndServerAsync(async uri => { if (await acceptConnection.Task) { - await IgnoreExceptions(async () => - { - await server.AcceptConnectionAsync(async _ => - { - await clientFinished.WaitAsync(TestHelper.PassingTestTimeout); - }); - }); + await IgnoreExceptions(() => server.AcceptConnectionAsync(_ => clientFinished.WaitAsync(TestHelper.PassingTestTimeout))); } }); }