From d8b7f3afb739477c064e5e5d85d9c37fa72bf0f6 Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Fri, 6 Oct 2023 19:32:50 +0200 Subject: [PATCH 1/4] Use Sync Send methods in Sync HttpClient tests --- .../tests/System/Net/Http/DribbleStream.cs | 4 +-- .../HttpClientHandlerTest.AutoRedirect.cs | 2 +- .../Net/Http/HttpClientHandlerTestBase.cs | 5 +++ .../System/Net/Http/HttpProtocolTests.cs | 34 ++++++++++--------- .../System/Net/Http/IdnaProtocolTests.cs | 2 +- .../tests/FunctionalTests/MetricsTest.cs | 15 +++++--- 6 files changed, 38 insertions(+), 24 deletions(-) diff --git a/src/libraries/Common/tests/System/Net/Http/DribbleStream.cs b/src/libraries/Common/tests/System/Net/Http/DribbleStream.cs index d2e12d8cb99f9..0261b982073cf 100644 --- a/src/libraries/Common/tests/System/Net/Http/DribbleStream.cs +++ b/src/libraries/Common/tests/System/Net/Http/DribbleStream.cs @@ -20,8 +20,8 @@ public override async Task WriteAsync(byte[] buffer, int offset, int count, Canc { for (int i = 0; i < count; i++) { - await _wrapped.WriteAsync(buffer, offset + i, 1); - await _wrapped.FlushAsync(); + await _wrapped.WriteAsync(buffer, offset + i, 1, cancellationToken); + await _wrapped.FlushAsync(cancellationToken); await Task.Yield(); // introduce short delays, enough to send packets individually but not so long as to extend test duration significantly } } diff --git a/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.AutoRedirect.cs b/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.AutoRedirect.cs index c9bbdbc0c13c6..1fa1642bd4e89 100644 --- a/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.AutoRedirect.cs +++ b/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTest.AutoRedirect.cs @@ -184,7 +184,7 @@ await LoopbackServer.CreateServerAsync(async (origServer, origUrl) => { await LoopbackServer.CreateServerAsync(async (redirectServer, redirectUrl) => { - Task getResponseTask = client.GetAsync(origUrl); + Task getResponseTask = client.GetAsync(TestAsync, origUrl); Task redirectTask = redirectServer.AcceptConnectionSendResponseAndCloseAsync(); diff --git a/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTestBase.cs b/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTestBase.cs index 798b23907cc9c..fdf301a809b86 100644 --- a/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTestBase.cs +++ b/src/libraries/Common/tests/System/Net/Http/HttpClientHandlerTestBase.cs @@ -238,5 +238,10 @@ public static Task GetByteArrayAsync(this HttpClient client, bool async, return client.GetByteArrayAsync(uri); #endif } + + public static Task GetAsync(this HttpClient client, bool async, Uri uri, HttpCompletionOption completionOption = default, CancellationToken cancellationToken = default) + { + return SendAsync(client, async, new HttpRequestMessage(HttpMethod.Get, uri), completionOption, cancellationToken); + } } } diff --git a/src/libraries/Common/tests/System/Net/Http/HttpProtocolTests.cs b/src/libraries/Common/tests/System/Net/Http/HttpProtocolTests.cs index b90cffafeab5d..6aecb3179a613 100644 --- a/src/libraries/Common/tests/System/Net/Http/HttpProtocolTests.cs +++ b/src/libraries/Common/tests/System/Net/Http/HttpProtocolTests.cs @@ -266,7 +266,8 @@ await LoopbackServer.CreateServerAsync(async (server, url) => { using (HttpClient client = CreateHttpClient()) { - Task getResponseTask = client.GetAsync(url); + Task getResponseTask = client.GetAsync(TestAsync, url); + await TestHelper.WhenAllCompletedOrAnyFailed( getResponseTask, server.AcceptConnectionSendCustomResponseAndCloseAsync( @@ -356,7 +357,7 @@ await LoopbackServer.CreateServerAsync(async (server, url) => Task ignoredServerTask = server.AcceptConnectionSendCustomResponseAndCloseAsync( responseString + "\r\nConnection: close\r\nContent-Length: 0\r\n\r\n"); - await Assert.ThrowsAsync(() => client.GetAsync(url)); + await Assert.ThrowsAsync(() => client.GetAsync(TestAsync, url)); } }, new LoopbackServer.Options { StreamWrapper = GetStream }); } @@ -366,23 +367,24 @@ await LoopbackServer.CreateServerAsync(async (server, url) => [InlineData("\n")] public async Task GetAsync_ResponseHasNormalLineEndings_Success(string lineEnding) { - await LoopbackServer.CreateServerAsync(async (server, url) => + // Using an unusually high timeout as this test can take a longer time to execute on busy CI machines. + TimeSpan timeout = TestHelper.PassingTestTimeout * 5; + + await LoopbackServer.CreateClientAndServerAsync(async url => { - using (HttpClient client = CreateHttpClient()) - { - Task getResponseTask = client.GetAsync(url); - Task> serverTask = server.AcceptConnectionSendCustomResponseAndCloseAsync( - $"HTTP/1.1 200 OK{lineEnding}Connection: close\r\nDate: {DateTimeOffset.UtcNow:R}{lineEnding}Server: TestServer{lineEnding}Content-Length: 0{lineEnding}{lineEnding}"); + using HttpClient client = CreateHttpClient(); - await TestHelper.WhenAllCompletedOrAnyFailed(getResponseTask, serverTask); + using HttpResponseMessage response = await client.GetAsync(TestAsync, url).WaitAsync(timeout); - using (HttpResponseMessage response = await getResponseTask) - { - Assert.Equal(200, (int)response.StatusCode); - Assert.Equal("OK", response.ReasonPhrase); - Assert.Equal("TestServer", response.Headers.Server.ToString()); - } - } + Assert.Equal(200, (int)response.StatusCode); + Assert.Equal("OK", response.ReasonPhrase); + Assert.Equal("TestServer", response.Headers.Server.ToString()); + }, + async server => + { + await server.AcceptConnectionSendCustomResponseAndCloseAsync( + "HTTP/1.1 200 OK\nConnection: close\nDate: {DateTimeOffset.UtcNow:R}\nServer: TestServer\nContent-Length: 0\n\n".ReplaceLineEndings(lineEnding)) + .WaitAsync(timeout); }, new LoopbackServer.Options { StreamWrapper = GetStream }); } diff --git a/src/libraries/Common/tests/System/Net/Http/IdnaProtocolTests.cs b/src/libraries/Common/tests/System/Net/Http/IdnaProtocolTests.cs index d3889a769de00..ea91330355004 100644 --- a/src/libraries/Common/tests/System/Net/Http/IdnaProtocolTests.cs +++ b/src/libraries/Common/tests/System/Net/Http/IdnaProtocolTests.cs @@ -40,7 +40,7 @@ await LoopbackServer.CreateServerAsync(async (server, serverUrl) => using (HttpClient client = CreateHttpClient(handler)) { - Task getResponseTask = client.GetAsync(uri); + Task getResponseTask = client.GetAsync(TestAsync, uri); Task> serverTask = server.AcceptConnectionSendResponseAndCloseAsync(); await TestHelper.WhenAllCompletedOrAnyFailed(getResponseTask, serverTask); diff --git a/src/libraries/System.Net.Http/tests/FunctionalTests/MetricsTest.cs b/src/libraries/System.Net.Http/tests/FunctionalTests/MetricsTest.cs index 15bb4bcbe2bb9..4365cf35faa28 100644 --- a/src/libraries/System.Net.Http/tests/FunctionalTests/MetricsTest.cs +++ b/src/libraries/System.Net.Http/tests/FunctionalTests/MetricsTest.cs @@ -717,10 +717,17 @@ protected override void Dispose(bool disposing) base.Dispose(disposing); } - protected Task SendAsync(HttpMessageInvoker invoker, HttpRequestMessage request, CancellationToken cancellationToken = default) => - TestHttpMessageInvoker ? - invoker.SendAsync(request, cancellationToken) : - ((HttpClient)invoker).SendAsync(TestAsync, request, cancellationToken); + protected Task SendAsync(HttpMessageInvoker invoker, HttpRequestMessage request, CancellationToken cancellationToken = default) + { + if (TestHttpMessageInvoker) + { + return TestAsync + ? invoker.SendAsync(request, cancellationToken) + : Task.Run(() => invoker.Send(request, cancellationToken)); + } + + return ((HttpClient)invoker).SendAsync(TestAsync, request, cancellationToken); + } protected HttpMessageInvoker CreateHttpMessageInvoker(HttpMessageHandler? handler = null) => TestHttpMessageInvoker ? From ebdc8f7a62e16e17525a0b5b3c8c60d05445da97 Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Fri, 6 Oct 2023 19:47:13 +0200 Subject: [PATCH 2/4] Set the timeout on the client --- .../Common/tests/System/Net/Http/HttpProtocolTests.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libraries/Common/tests/System/Net/Http/HttpProtocolTests.cs b/src/libraries/Common/tests/System/Net/Http/HttpProtocolTests.cs index 6aecb3179a613..44430a84f8a8e 100644 --- a/src/libraries/Common/tests/System/Net/Http/HttpProtocolTests.cs +++ b/src/libraries/Common/tests/System/Net/Http/HttpProtocolTests.cs @@ -373,8 +373,9 @@ public async Task GetAsync_ResponseHasNormalLineEndings_Success(string lineEndin await LoopbackServer.CreateClientAndServerAsync(async url => { using HttpClient client = CreateHttpClient(); + client.Timeout = timeout; - using HttpResponseMessage response = await client.GetAsync(TestAsync, url).WaitAsync(timeout); + using HttpResponseMessage response = await client.GetAsync(TestAsync, url); Assert.Equal(200, (int)response.StatusCode); Assert.Equal("OK", response.ReasonPhrase); From be3cba515f773aca2b713997ef419ce461facb03 Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Fri, 6 Oct 2023 20:37:55 +0200 Subject: [PATCH 3/4] Yay Framework --- src/libraries/Common/tests/System/Net/Http/HttpProtocolTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/Common/tests/System/Net/Http/HttpProtocolTests.cs b/src/libraries/Common/tests/System/Net/Http/HttpProtocolTests.cs index 44430a84f8a8e..2b833bc01c5f2 100644 --- a/src/libraries/Common/tests/System/Net/Http/HttpProtocolTests.cs +++ b/src/libraries/Common/tests/System/Net/Http/HttpProtocolTests.cs @@ -384,7 +384,7 @@ await LoopbackServer.CreateClientAndServerAsync(async url => async server => { await server.AcceptConnectionSendCustomResponseAndCloseAsync( - "HTTP/1.1 200 OK\nConnection: close\nDate: {DateTimeOffset.UtcNow:R}\nServer: TestServer\nContent-Length: 0\n\n".ReplaceLineEndings(lineEnding)) + "HTTP/1.1 200 OK\nConnection: close\nDate: {DateTimeOffset.UtcNow:R}\nServer: TestServer\nContent-Length: 0\n\n".Replace("\n", lineEnding)) .WaitAsync(timeout); }, new LoopbackServer.Options { StreamWrapper = GetStream }); } From 92952d1dfa2d34ccf090dc3543de6347642232dd Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Fri, 6 Oct 2023 22:54:25 +0200 Subject: [PATCH 4/4] Yay Framework --- src/libraries/Common/tests/System/Net/Http/HttpProtocolTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/Common/tests/System/Net/Http/HttpProtocolTests.cs b/src/libraries/Common/tests/System/Net/Http/HttpProtocolTests.cs index 2b833bc01c5f2..d5c61f82246de 100644 --- a/src/libraries/Common/tests/System/Net/Http/HttpProtocolTests.cs +++ b/src/libraries/Common/tests/System/Net/Http/HttpProtocolTests.cs @@ -368,7 +368,7 @@ await LoopbackServer.CreateServerAsync(async (server, url) => public async Task GetAsync_ResponseHasNormalLineEndings_Success(string lineEnding) { // Using an unusually high timeout as this test can take a longer time to execute on busy CI machines. - TimeSpan timeout = TestHelper.PassingTestTimeout * 5; + TimeSpan timeout = TimeSpan.FromMilliseconds(TestHelper.PassingTestTimeoutMilliseconds * 5); await LoopbackServer.CreateClientAndServerAsync(async url => {