|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.IO; |
| 5 | +using System.Net.Sockets; |
| 6 | +using System.Net.Test.Common; |
| 7 | +using System.Threading; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Microsoft.DotNet.RemoteExecutor; |
| 10 | +using Xunit; |
| 11 | +using Xunit.Abstractions; |
| 12 | + |
| 13 | +namespace System.Net.Http.Functional.Tests |
| 14 | +{ |
| 15 | + [Collection(nameof(DisableParallelization))] // Reduces chance of timing-related issues |
| 16 | + [ConditionalClass(typeof(SocketsHttpHandler), nameof(SocketsHttpHandler.IsSupported))] |
| 17 | + public class SocketsHttpHandler_Cancellation_Test_NonParallel : HttpClientHandlerTestBase |
| 18 | + { |
| 19 | + public SocketsHttpHandler_Cancellation_Test_NonParallel(ITestOutputHelper output) : base(output) |
| 20 | + { |
| 21 | + } |
| 22 | + |
| 23 | + [OuterLoop("Incurs significant delay.")] |
| 24 | + [ConditionalTheory(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))] |
| 25 | + [InlineData("1.1", 10_000, 1_000, 100)] |
| 26 | + [InlineData("2.0", 10_000, 1_000, 100)] |
| 27 | + [InlineData("1.1", 20_000, 10_000, null)] |
| 28 | + [InlineData("2.0", 20_000, 10_000, null)] |
| 29 | + public static void CancelPendingRequest_DropsStalledConnectionAttempt(string versionString, int firstConnectionDelayMs, int requestTimeoutMs, int? pendingConnectionTimeoutOnRequestCompletion) |
| 30 | + { |
| 31 | + RemoteInvokeOptions options = new RemoteInvokeOptions(); |
| 32 | + if (pendingConnectionTimeoutOnRequestCompletion is not null) |
| 33 | + { |
| 34 | + options.StartInfo.EnvironmentVariables["DOTNET_SYSTEM_NET_HTTP_SOCKETSHTTPHANDLER_PENDINGCONNECTIONTIMEOUTONREQUESTCOMPLETION"] = pendingConnectionTimeoutOnRequestCompletion.ToString(); |
| 35 | + } |
| 36 | + |
| 37 | + RemoteExecutor.Invoke(CancelPendingRequest_DropsStalledConnectionAttempt_Impl, versionString, firstConnectionDelayMs.ToString(), requestTimeoutMs.ToString(), options).Dispose(); |
| 38 | + } |
| 39 | + |
| 40 | + private static async Task CancelPendingRequest_DropsStalledConnectionAttempt_Impl(string versionString, string firstConnectionDelayMsString, string requestTimeoutMsString) |
| 41 | + { |
| 42 | + var version = Version.Parse(versionString); |
| 43 | + LoopbackServerFactory factory = GetFactoryForVersion(version); |
| 44 | + |
| 45 | + const int AttemptCount = 3; |
| 46 | + int firstConnectionDelayMs = int.Parse(firstConnectionDelayMsString); |
| 47 | + int requestTimeoutMs = int.Parse(requestTimeoutMsString); |
| 48 | + bool firstConnection = true; |
| 49 | + |
| 50 | + using CancellationTokenSource cts0 = new CancellationTokenSource(requestTimeoutMs); |
| 51 | + |
| 52 | + await factory.CreateClientAndServerAsync(async uri => |
| 53 | + { |
| 54 | + using var handler = CreateHttpClientHandler(version); |
| 55 | + GetUnderlyingSocketsHttpHandler(handler).ConnectCallback = DoConnect; |
| 56 | + using var client = new HttpClient(handler) { DefaultRequestVersion = version }; |
| 57 | + |
| 58 | + await Assert.ThrowsAnyAsync<TaskCanceledException>(async () => |
| 59 | + { |
| 60 | + await client.GetAsync(uri, cts0.Token); |
| 61 | + }); |
| 62 | + |
| 63 | + for (int i = 0; i < AttemptCount; i++) |
| 64 | + { |
| 65 | + using var cts1 = new CancellationTokenSource(requestTimeoutMs); |
| 66 | + using var response = await client.GetAsync(uri, cts1.Token); |
| 67 | + Assert.Equal(HttpStatusCode.OK, response.StatusCode); |
| 68 | + } |
| 69 | + }, async server => |
| 70 | + { |
| 71 | + await server.AcceptConnectionAsync(async connection => |
| 72 | + { |
| 73 | + for (int i = 0; i < AttemptCount; i++) |
| 74 | + { |
| 75 | + await connection.ReadRequestDataAsync(); |
| 76 | + await connection.SendResponseAsync(); |
| 77 | + connection.CompleteRequestProcessing(); |
| 78 | + } |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + async ValueTask<Stream> DoConnect(SocketsHttpConnectionContext ctx, CancellationToken cancellationToken) |
| 83 | + { |
| 84 | + if (firstConnection) |
| 85 | + { |
| 86 | + firstConnection = false; |
| 87 | + await Task.Delay(100, cancellationToken); // Wait for the request to be pushed to the queue |
| 88 | + cts0.Cancel(); // cancel the first request faster than RequestTimeoutMs |
| 89 | + await Task.Delay(firstConnectionDelayMs, cancellationToken); // Simulate stalled connection |
| 90 | + } |
| 91 | + var s = new Socket(SocketType.Stream, ProtocolType.Tcp) { NoDelay = true }; |
| 92 | + await s.ConnectAsync(ctx.DnsEndPoint, cancellationToken); |
| 93 | + |
| 94 | + return new NetworkStream(s, ownsSocket: true); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + [OuterLoop("Incurs significant delay.")] |
| 99 | + [ConditionalTheory(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))] |
| 100 | + [InlineData(20_000)] |
| 101 | + [InlineData(Timeout.Infinite)] |
| 102 | + public void PendingConnectionTimeout_HighValue_PendingConnectionIsNotCancelled(int timeout) |
| 103 | + { |
| 104 | + RemoteExecutor.Invoke(async timoutStr => |
| 105 | + { |
| 106 | + // Setup "infinite" timeout of int.MaxValue milliseconds |
| 107 | + AppContext.SetData("System.Net.SocketsHttpHandler.PendingConnectionTimeoutOnRequestCompletion", int.Parse(timoutStr)); |
| 108 | + |
| 109 | + bool connected = false; |
| 110 | + CancellationTokenSource cts = new CancellationTokenSource(); |
| 111 | + |
| 112 | + await new Http11LoopbackServerFactory().CreateClientAndServerAsync(async uri => |
| 113 | + { |
| 114 | + using var handler = CreateHttpClientHandler(HttpVersion.Version11); |
| 115 | + GetUnderlyingSocketsHttpHandler(handler).ConnectCallback = DoConnect; |
| 116 | + using var client = new HttpClient(handler) { DefaultRequestVersion = HttpVersion.Version11 }; |
| 117 | + |
| 118 | + await Assert.ThrowsAnyAsync<TaskCanceledException>(() => client.GetAsync(uri, cts.Token)); |
| 119 | + }, |
| 120 | + async server => { |
| 121 | + await server.AcceptConnectionAsync(_ => Task.CompletedTask).WaitAsync(30_000); |
| 122 | + }); |
| 123 | + |
| 124 | + async ValueTask<Stream> DoConnect(SocketsHttpConnectionContext ctx, CancellationToken cancellationToken) |
| 125 | + { |
| 126 | + var s = new Socket(SocketType.Stream, ProtocolType.Tcp) { NoDelay = true }; |
| 127 | + await Task.Delay(100, cancellationToken); // Wait for the request to be pushed to the queue |
| 128 | + cts.Cancel(); |
| 129 | + |
| 130 | + await Task.Delay(10_000, cancellationToken); |
| 131 | + await s.ConnectAsync(ctx.DnsEndPoint, cancellationToken); |
| 132 | + connected = true; |
| 133 | + return new NetworkStream(s, ownsSocket: true); |
| 134 | + } |
| 135 | + |
| 136 | + Assert.True(connected); |
| 137 | + }, timeout.ToString()).Dispose(); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments