From 4e57b0e1f1e14a190d33427286231f9566aa3863 Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Wed, 19 Sep 2018 10:09:44 -0700 Subject: [PATCH] Add functional style request aborted test (#1406) --- .../Inprocess/ClientDisconnectTests.cs | 18 +++++++++++++++ .../shared/SharedStartup/Startup.shared.cs | 22 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/test/Common.FunctionalTests/Inprocess/ClientDisconnectTests.cs b/test/Common.FunctionalTests/Inprocess/ClientDisconnectTests.cs index fc5f60b26e..761c389d48 100644 --- a/test/Common.FunctionalTests/Inprocess/ClientDisconnectTests.cs +++ b/test/Common.FunctionalTests/Inprocess/ClientDisconnectTests.cs @@ -43,5 +43,23 @@ namespace Microsoft.AspNetCore.Server.IISIntegration.FunctionalTests var responseText = await response.Content.ReadAsStringAsync(); Assert.Equal("Hello World", responseText); } + + [ConditionalFact] + public async Task RequestAbortedTokenFires() + { + using (var connection = _fixture.CreateTestConnection()) + { + await connection.Send( + "GET /WaitForAbort HTTP/1.1", + "Host: localhost", + "Connection: close", + "", + ""); + + await _fixture.Client.RetryRequestAsync("/WaitingRequestCount", async message => await message.Content.ReadAsStringAsync() == "1"); + } + + await _fixture.Client.RetryRequestAsync("/WaitingRequestCount", async message => await message.Content.ReadAsStringAsync() == "0"); + } } } diff --git a/test/WebSites/shared/SharedStartup/Startup.shared.cs b/test/WebSites/shared/SharedStartup/Startup.shared.cs index 4c6d2eb4cf..1db25d5905 100644 --- a/test/WebSites/shared/SharedStartup/Startup.shared.cs +++ b/test/WebSites/shared/SharedStartup/Startup.shared.cs @@ -3,6 +3,7 @@ using System; using System.Linq; +using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Hosting; @@ -49,5 +50,26 @@ namespace TestSite var clientCert = context.Connection.ClientCertificate; await context.Response.WriteAsync(clientCert != null ? $"Enabled;{clientCert.GetCertHashString()}" : "Disabled"); } + + private static int _waitingRequestCount; + + public Task WaitForAbort(HttpContext context) + { + Interlocked.Increment(ref _waitingRequestCount); + try + { + context.RequestAborted.WaitHandle.WaitOne(); + return Task.CompletedTask; + } + finally + { + Interlocked.Decrement(ref _waitingRequestCount); + } + } + + public async Task WaitingRequestCount(HttpContext context) + { + await context.Response.WriteAsync(_waitingRequestCount.ToString()); + } } }