From 4f1104363ead1edecd0c5580533c487a450bf97c Mon Sep 17 00:00:00 2001 From: David Fowler Date: Thu, 4 Apr 2019 10:07:03 -0700 Subject: [PATCH] Make TestServer IAsyncDisposable (#9059) - This should make it a bit more maintainable than it is today (having to call server.StopAsync before Dispose calls it synchronously) --- .../BadHttpRequestTests.cs | 12 +- .../ChunkedRequestTests.cs | 46 +-- .../ChunkedResponseTests.cs | 100 ++---- .../ConnectionAdapterTests.cs | 25 +- .../ConnectionLimitTests.cs | 13 +- .../DefaultHeaderTests.cs | 3 +- .../EventSourceTests.cs | 4 +- .../HttpConnectionManagerTests.cs | 4 +- .../HttpProtocolSelectionTests.cs | 6 +- .../HttpsConnectionAdapterTests.cs | 51 +-- .../InMemory.FunctionalTests/HttpsTests.cs | 23 +- .../KeepAliveTimeoutTests.cs | 18 +- .../LoggingConnectionAdapterTests.cs | 5 +- .../MaxRequestBodySizeTests.cs | 33 +- .../MaxRequestLineSizeTests.cs | 6 +- .../RequestBodyTimeoutTests.cs | 9 +- .../RequestHeaderLimitsTests.cs | 12 +- .../RequestHeadersTimeoutTests.cs | 12 +- .../RequestTargetProcessingTests.cs | 9 +- .../InMemory.FunctionalTests/RequestTests.cs | 122 +++---- .../ResponseDrainingTests.cs | 3 +- .../InMemory.FunctionalTests/ResponseTests.cs | 324 ++++++------------ .../TestTransport/TestServer.cs | 10 +- .../InMemory.FunctionalTests/UpgradeTests.cs | 27 +- 24 files changed, 287 insertions(+), 590 deletions(-) diff --git a/src/Servers/Kestrel/test/InMemory.FunctionalTests/BadHttpRequestTests.cs b/src/Servers/Kestrel/test/InMemory.FunctionalTests/BadHttpRequestTests.cs index 4657c3e6aa..ae9cfe8927 100644 --- a/src/Servers/Kestrel/test/InMemory.FunctionalTests/BadHttpRequestTests.cs +++ b/src/Servers/Kestrel/test/InMemory.FunctionalTests/BadHttpRequestTests.cs @@ -153,7 +153,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests [Fact] public async Task BadRequestLogsAreNotHigherThanInformation() { - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { await context.Request.Body.ReadAsync(new byte[1], 0, 1); }, new TestServiceContext(LoggerFactory))) @@ -166,8 +166,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests ""); await ReceiveBadRequestResponse(connection, "400 Bad Request", server.Context.DateHeaderValue); } - - await server.StopAsync(); } Assert.All(TestSink.Writes, w => Assert.InRange(w.LogLevel, LogLevel.Trace, LogLevel.Information)); @@ -177,7 +175,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests [Fact] public async Task TestRequestSplitting() { - using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory))) + await using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory))) { using (var client = server.CreateConnection()) { @@ -187,8 +185,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests await client.Receive("HTTP/1.1 400"); } - - await server.StopAsync(); } } @@ -203,15 +199,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests .Setup(trace => trace.ConnectionBadRequest(It.IsAny(), It.IsAny())) .Callback((connectionId, exception) => loggedException = exception); - using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory, mockKestrelTrace.Object))) + await using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory, mockKestrelTrace.Object))) { using (var connection = server.CreateConnection()) { await connection.SendAll(request); await ReceiveBadRequestResponse(connection, expectedResponseStatusCode, server.Context.DateHeaderValue, expectedAllowHeader); } - - await server.StopAsync(); } mockKestrelTrace.Verify(trace => trace.ConnectionBadRequest(It.IsAny(), It.IsAny())); diff --git a/src/Servers/Kestrel/test/InMemory.FunctionalTests/ChunkedRequestTests.cs b/src/Servers/Kestrel/test/InMemory.FunctionalTests/ChunkedRequestTests.cs index 65e5a5e386..7247e11226 100644 --- a/src/Servers/Kestrel/test/InMemory.FunctionalTests/ChunkedRequestTests.cs +++ b/src/Servers/Kestrel/test/InMemory.FunctionalTests/ChunkedRequestTests.cs @@ -71,7 +71,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(App, testContext)) + await using (var server = new TestServer(App, testContext)) { using (var connection = server.CreateConnection()) { @@ -100,7 +100,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(PipeApp, testContext)) + await using (var server = new TestServer(PipeApp, testContext)) { using (var connection = server.CreateConnection()) { @@ -129,7 +129,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(); - using (var server = new TestServer(AppChunked, testContext)) + await using (var server = new TestServer(AppChunked, testContext)) { using (var connection = server.CreateConnection()) { @@ -162,8 +162,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", "Goodbye"); } - - await server.StopAsync(); } } @@ -172,7 +170,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = httpContext.Response; var request = httpContext.Request; @@ -218,7 +216,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", "Hello World"); } - await server.StopAsync(); } } @@ -228,7 +225,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var requestCount = 10; var requestsReceived = 0; - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = httpContext.Response; var request = httpContext.Request; @@ -304,8 +301,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests await connection.Send(fullRequest); await connection.Receive(expectedFullResponse); } - - await server.StopAsync(); } } @@ -315,7 +310,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var requestCount = 10; var requestsReceived = 0; - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = httpContext.Response; var request = httpContext.Request; @@ -394,8 +389,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests await connection.Send(fullRequest); await connection.Receive(expectedFullResponse); } - - await server.StopAsync(); } } [Fact] @@ -411,7 +404,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests headerLine.Length + 2 + trailingHeaderLine.Length + 1; - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { var buffer = new byte[128]; while (await context.Request.Body.ReadAsync(buffer, 0, buffer.Length) != 0) ; // read to end @@ -439,7 +432,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -453,7 +445,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var testContext = new TestServiceContext(LoggerFactory); testContext.ServerOptions.Limits.MaxRequestHeaderCount = 2; - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { var buffer = new byte[128]; while (await context.Request.Body.ReadAsync(buffer, 0, buffer.Length) != 0) ; // read to end @@ -481,7 +473,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -492,7 +483,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var requestCount = 10; var requestsReceived = 0; - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = httpContext.Response; var request = httpContext.Request; @@ -568,7 +559,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests await connection.Send(fullRequest); await connection.Receive(expectedFullResponse); } - await server.StopAsync(); } } @@ -576,7 +566,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests public async Task InvalidLengthResultsIn400() { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = httpContext.Response; var request = httpContext.Request; @@ -620,7 +610,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests public async Task InvalidSizedDataResultsIn400() { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = httpContext.Response; var request = httpContext.Request; @@ -657,7 +647,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -666,7 +655,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests public async Task ChunkedNotFinalTransferCodingResultsIn400() { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(httpContext => + await using (var server = new TestServer(httpContext => { return Task.CompletedTask; }, testContext)) @@ -762,7 +751,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -773,7 +761,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var readStartedTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var exTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var readTask = httpContext.Request.Body.CopyToAsync(Stream.Null); readStartedTcs.SetResult(null); @@ -810,8 +798,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var badReqEx = await exTcs.Task.TimeoutAfter(TestConstants.DefaultTimeout); Assert.Equal(RequestRejectionReason.UnexpectedEndOfRequestContent, badReqEx.Reason); } - - await server.StopAsync(); } } @@ -821,7 +807,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var tcs = new TaskCompletionSource(); var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = httpContext.Response; var request = httpContext.Request; @@ -869,7 +855,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", "Hello World"); } - await server.StopAsync(); } } @@ -878,7 +863,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = httpContext.Response; var request = httpContext.Request; @@ -968,7 +953,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } } diff --git a/src/Servers/Kestrel/test/InMemory.FunctionalTests/ChunkedResponseTests.cs b/src/Servers/Kestrel/test/InMemory.FunctionalTests/ChunkedResponseTests.cs index 9bdf8785e9..3d109476a5 100644 --- a/src/Servers/Kestrel/test/InMemory.FunctionalTests/ChunkedResponseTests.cs +++ b/src/Servers/Kestrel/test/InMemory.FunctionalTests/ChunkedResponseTests.cs @@ -20,7 +20,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = httpContext.Response; await response.BodyWriter.WriteAsync(new Memory(Encoding.ASCII.GetBytes("Hello "), 0, 6)); @@ -47,7 +47,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -56,7 +55,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { await httpContext.Response.WriteAsync("Hello "); await httpContext.Response.WriteAsync("World!"); @@ -76,7 +75,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", "Hello World!"); } - await server.StopAsync(); } } @@ -85,7 +83,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { await httpContext.Response.WriteAsync("Hello "); await httpContext.Response.WriteAsync("World!"); @@ -113,7 +111,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -122,7 +119,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); var expectedString = new string('a', 10000); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { await httpContext.Response.WriteAsync(expectedString); }, testContext)) @@ -151,7 +148,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -163,7 +159,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); var expectedString = new string('a', length); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { await httpContext.Response.StartAsync(); var memory = httpContext.Response.BodyWriter.GetMemory(length); @@ -193,7 +189,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -206,7 +201,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); var expectedString = new string('a', partialLength); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { await httpContext.Response.StartAsync(); var memory = httpContext.Response.BodyWriter.GetMemory(100000); @@ -235,7 +230,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -244,7 +238,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { httpContext.Response.Headers["Connection"] = "close"; await httpContext.Response.WriteAsync("Hello "); @@ -272,7 +266,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -281,7 +274,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = httpContext.Response; await response.BodyWriter.WriteAsync(new Memory(Encoding.ASCII.GetBytes("Hello "), 0, 6)); @@ -309,7 +302,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -320,7 +312,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var flushed = new SemaphoreSlim(0, 1); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = httpContext.Response; await response.WriteAsync(""); @@ -354,7 +346,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -363,7 +354,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = httpContext.Response; await response.BodyWriter.WriteAsync(new Memory(new byte[0], 0, 0)); @@ -385,7 +376,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -394,7 +384,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = httpContext.Response; await response.BodyWriter.WriteAsync(new Memory(Encoding.ASCII.GetBytes("Hello World!"), 0, 12)); @@ -419,7 +409,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "Hello World!", ""); } - await server.StopAsync(); } } @@ -428,7 +417,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = httpContext.Response; await response.BodyWriter.WriteAsync(new Memory(new byte[0], 0, 0)); @@ -452,7 +441,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -463,7 +451,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var flushWh = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = httpContext.Response; await response.BodyWriter.WriteAsync(new Memory(Encoding.ASCII.GetBytes("Hello "), 0, 6)); @@ -499,7 +487,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -508,7 +495,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = httpContext.Response; response.Headers["Transfer-Encoding"] = "chunked"; @@ -538,8 +525,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - - await server.StopAsync(); } } @@ -548,7 +533,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = httpContext.Response; @@ -584,8 +569,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - - await server.StopAsync(); } } @@ -594,7 +577,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = httpContext.Response; @@ -629,8 +612,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - - await server.StopAsync(); } } @@ -641,7 +622,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var semaphore = new SemaphoreSlim(initialCount: 0); var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = httpContext.Response; @@ -685,8 +666,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - - await server.StopAsync(); } } @@ -697,7 +676,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var semaphore = new SemaphoreSlim(initialCount: 0); var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = httpContext.Response; @@ -743,8 +722,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - - await server.StopAsync(); } } @@ -755,7 +732,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var semaphore = new SemaphoreSlim(initialCount: 0); var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = httpContext.Response; @@ -802,8 +779,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - - await server.StopAsync(); } } @@ -812,7 +787,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = httpContext.Response; @@ -846,8 +821,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - - await server.StopAsync(); } } @@ -856,7 +829,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = httpContext.Response; await response.StartAsync(); @@ -895,8 +868,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - - await server.StopAsync(); } } @@ -905,7 +876,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = httpContext.Response; @@ -938,8 +909,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - - await server.StopAsync(); } } @@ -948,7 +917,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = httpContext.Response; @@ -980,8 +949,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - - await server.StopAsync(); } } @@ -990,7 +957,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = httpContext.Response; @@ -1021,8 +988,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - - await server.StopAsync(); } } @@ -1031,7 +996,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = httpContext.Response; @@ -1058,8 +1023,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - - await server.StopAsync(); } } @@ -1070,11 +1033,9 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = httpContext.Response; - - var memory = response.BodyWriter.GetMemory(4096); var fisrtPartOfResponse = Encoding.ASCII.GetBytes(new string('a', writeSize)); fisrtPartOfResponse.CopyTo(memory); @@ -1100,8 +1061,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - - await server.StopAsync(); } } @@ -1112,7 +1071,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = httpContext.Response; @@ -1141,15 +1100,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - - await server.StopAsync(); } } [Fact] public async Task ChunkedWithBothPipeAndStreamWorks() { - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = httpContext.Response; @@ -1191,7 +1148,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } diff --git a/src/Servers/Kestrel/test/InMemory.FunctionalTests/ConnectionAdapterTests.cs b/src/Servers/Kestrel/test/InMemory.FunctionalTests/ConnectionAdapterTests.cs index 79df18c8d9..c54c2a031b 100644 --- a/src/Servers/Kestrel/test/InMemory.FunctionalTests/ConnectionAdapterTests.cs +++ b/src/Servers/Kestrel/test/InMemory.FunctionalTests/ConnectionAdapterTests.cs @@ -42,7 +42,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var sendString = "POST / HTTP/1.0\r\nContent-Length: 12\r\n\r\nHello World?"; - using (var server = new TestServer(requestDelegate, serviceContext, listenOptions)) + await using (var server = new TestServer(requestDelegate, serviceContext, listenOptions)) { using (var connection = server.CreateConnection()) { @@ -55,7 +55,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", "Hello World!"); } - await server.StopAsync(); } Assert.Equal(sendString.Length, adapter.BytesRead); @@ -72,7 +71,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var serviceContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(requestDelegate, serviceContext, listenOptions)) + await using (var server = new TestServer(requestDelegate, serviceContext, listenOptions)) { using (var connection = server.CreateConnection()) { @@ -88,7 +87,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", "Hello World!"); } - await server.StopAsync(); } } @@ -103,7 +101,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var serviceContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(requestDelegate, serviceContext, listenOptions)) + await using (var server = new TestServer(requestDelegate, serviceContext, listenOptions)) { using (var connection = server.CreateConnection()) { @@ -111,7 +109,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests connection.ShutdownSend(); await connection.WaitForConnectionClose(); } - await server.StopAsync(); } } @@ -126,7 +123,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var serviceContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(requestDelegate, serviceContext, listenOptions)) + await using (var server = new TestServer(requestDelegate, serviceContext, listenOptions)) { using (var connection = server.CreateConnection()) { @@ -134,7 +131,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests connection.ShutdownSend(); await connection.WaitForConnectionClose(); } - await server.StopAsync(); } } @@ -151,7 +147,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var serviceContext = new TestServiceContext(LoggerFactory); var stopTask = Task.CompletedTask; - using (var server = new TestServer(requestDelegate, serviceContext, listenOptions)) + await using (var server = new TestServer(requestDelegate, serviceContext, listenOptions)) using (var shutdownCts = new CancellationTokenSource(TestConstants.DefaultTimeout)) { using (var connection = server.CreateConnection()) @@ -177,7 +173,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var serviceContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(TestApp.EchoApp, serviceContext, listenOptions)) + await using (var server = new TestServer(TestApp.EchoApp, serviceContext, listenOptions)) { Task stopTask; @@ -207,7 +203,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var serviceContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(requestDelegate, serviceContext, listenOptions)) + await using (var server = new TestServer(requestDelegate, serviceContext, listenOptions)) { using (var connection = server.CreateConnection()) { @@ -218,7 +214,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests await connection.WaitForConnectionClose(); } - await server.StopAsync(); } Assert.Contains(TestApplicationErrorLogger.Messages, m => m.Message.Contains($"Uncaught exception from the {nameof(IConnectionAdapter.OnConnectionAsync)} method of an {nameof(IConnectionAdapter)}.")); @@ -234,7 +229,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var serviceContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { await context.Response.WriteAsync("Hello "); await context.Response.Body.FlushAsync(); @@ -254,7 +249,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", "Hello World!"); } - await server.StopAsync(); } } @@ -268,7 +262,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var serviceContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { await context.Response.BodyWriter.WriteAsync(Encoding.ASCII.GetBytes("Hello ")); await context.Response.BodyWriter.FlushAsync(); @@ -288,7 +282,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", "Hello World!"); } - await server.StopAsync(); } } diff --git a/src/Servers/Kestrel/test/InMemory.FunctionalTests/ConnectionLimitTests.cs b/src/Servers/Kestrel/test/InMemory.FunctionalTests/ConnectionLimitTests.cs index fc9c256112..6c1aa25d53 100644 --- a/src/Servers/Kestrel/test/InMemory.FunctionalTests/ConnectionLimitTests.cs +++ b/src/Servers/Kestrel/test/InMemory.FunctionalTests/ConnectionLimitTests.cs @@ -31,7 +31,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests counter.OnLock += (s, e) => lockedTcs.TrySetResult(e); counter.OnRelease += (s, e) => releasedTcs.TrySetResult(null); - using (var server = CreateServerWithMaxConnections(async context => + await using (var server = CreateServerWithMaxConnections(async context => { await context.Response.WriteAsync("Hello"); await requestTcs.Task; @@ -44,8 +44,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests Assert.True(await lockedTcs.Task.DefaultTimeout()); requestTcs.TrySetResult(null); } - - await server.StopAsync(); } await releasedTcs.Task.DefaultTimeout(); @@ -54,7 +52,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests [Fact] public async Task UpgradedConnectionsCountsAgainstDifferentLimit() { - using (var server = CreateServerWithMaxConnections(async context => + await using (var server = CreateServerWithMaxConnections(async context => { var feature = context.Features.Get(); if (feature.IsUpgradableRequest) @@ -96,7 +94,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests await rejected.WaitForConnectionClose(); } } - await server.StopAsync(); } } @@ -106,7 +103,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests const int max = 10; var requestTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - using (var server = CreateServerWithMaxConnections(async context => + await using (var server = CreateServerWithMaxConnections(async context => { await context.Response.WriteAsync("Hello"); await requestTcs.Task; @@ -142,7 +139,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests requestTcs.TrySetResult(null); } - await server.StopAsync(); } } @@ -174,7 +170,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests } }; - using (var server = CreateServerWithMaxConnections(_ => Task.CompletedTask, counter)) + await using (var server = CreateServerWithMaxConnections(_ => Task.CompletedTask, counter)) { // open a bunch of connections in parallel Parallel.For(0, count, async i => @@ -199,7 +195,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests await closedTcs.Task.TimeoutAfter(TimeSpan.FromSeconds(120)); Assert.Equal(count, opened); Assert.Equal(count, closed); - await server.StopAsync(); } } diff --git a/src/Servers/Kestrel/test/InMemory.FunctionalTests/DefaultHeaderTests.cs b/src/Servers/Kestrel/test/InMemory.FunctionalTests/DefaultHeaderTests.cs index fd6b06621f..58a5b99c7f 100644 --- a/src/Servers/Kestrel/test/InMemory.FunctionalTests/DefaultHeaderTests.cs +++ b/src/Servers/Kestrel/test/InMemory.FunctionalTests/DefaultHeaderTests.cs @@ -19,7 +19,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests ServerOptions = { AddServerHeader = true } }; - using (var server = new TestServer(ctx => Task.CompletedTask, testContext)) + await using (var server = new TestServer(ctx => Task.CompletedTask, testContext)) { using (var connection = server.CreateConnection()) { @@ -45,7 +45,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } } diff --git a/src/Servers/Kestrel/test/InMemory.FunctionalTests/EventSourceTests.cs b/src/Servers/Kestrel/test/InMemory.FunctionalTests/EventSourceTests.cs index 37ad630bc1..6138585122 100644 --- a/src/Servers/Kestrel/test/InMemory.FunctionalTests/EventSourceTests.cs +++ b/src/Servers/Kestrel/test/InMemory.FunctionalTests/EventSourceTests.cs @@ -30,7 +30,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests string connectionId = null; string requestId = null; int port; - using (var server = new TestServer(context => + await using (var server = new TestServer(context => { connectionId = context.Features.Get().ConnectionId; requestId = context.TraceIdentifier; @@ -47,8 +47,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests .DefaultTimeout(); await connection.Receive("HTTP/1.1 200"); } - - await server.StopAsync(); } // capture list here as other tests executing in parallel may log events diff --git a/src/Servers/Kestrel/test/InMemory.FunctionalTests/HttpConnectionManagerTests.cs b/src/Servers/Kestrel/test/InMemory.FunctionalTests/HttpConnectionManagerTests.cs index 2326d0cc76..2fc83d668e 100644 --- a/src/Servers/Kestrel/test/InMemory.FunctionalTests/HttpConnectionManagerTests.cs +++ b/src/Servers/Kestrel/test/InMemory.FunctionalTests/HttpConnectionManagerTests.cs @@ -43,7 +43,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var testContext = new TestServiceContext(new LoggerFactory(), mockTrace.Object); testContext.InitializeHeartbeat(); - using (var server = new TestServer(context => + await using (var server = new TestServer(context => { appStartedWh.Release(); var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); @@ -69,8 +69,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests } Assert.True(logWaitAttempts < 10); - - await server.StopAsync(); } } #endif diff --git a/src/Servers/Kestrel/test/InMemory.FunctionalTests/HttpProtocolSelectionTests.cs b/src/Servers/Kestrel/test/InMemory.FunctionalTests/HttpProtocolSelectionTests.cs index af1ae592e1..8b29ad1a13 100644 --- a/src/Servers/Kestrel/test/InMemory.FunctionalTests/HttpProtocolSelectionTests.cs +++ b/src/Servers/Kestrel/test/InMemory.FunctionalTests/HttpProtocolSelectionTests.cs @@ -63,14 +63,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests Protocols = serverProtocols }; - using (var server = new TestServer(context => Task.CompletedTask, testContext, listenOptions)) + await using (var server = new TestServer(context => Task.CompletedTask, testContext, listenOptions)) { using (var connection = server.CreateConnection()) { await connection.Send(request); await connection.Receive(expectedResponse); } - await server.StopAsync(); } } @@ -83,13 +82,12 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests Protocols = serverProtocols }; - using (var server = new TestServer(context => Task.CompletedTask, testContext, listenOptions)) + await using (var server = new TestServer(context => Task.CompletedTask, testContext, listenOptions)) { using (var connection = server.CreateConnection()) { await connection.WaitForConnectionClose(); } - await server.StopAsync(); } Assert.Single(TestApplicationErrorLogger.Messages, message => message.LogLevel == LogLevel.Error diff --git a/src/Servers/Kestrel/test/InMemory.FunctionalTests/HttpsConnectionAdapterTests.cs b/src/Servers/Kestrel/test/InMemory.FunctionalTests/HttpsConnectionAdapterTests.cs index 64391f6c12..fe46fba80f 100644 --- a/src/Servers/Kestrel/test/InMemory.FunctionalTests/HttpsConnectionAdapterTests.cs +++ b/src/Servers/Kestrel/test/InMemory.FunctionalTests/HttpsConnectionAdapterTests.cs @@ -42,7 +42,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests } }; - using (var server = new TestServer(App, new TestServiceContext(LoggerFactory), listenOptions)) + await using (var server = new TestServer(App, new TestServiceContext(LoggerFactory), listenOptions)) { var result = await server.HttpClientSlim.PostAsync($"https://localhost:{server.Port}/", new FormUrlEncodedContent(new[] { @@ -51,7 +51,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests validateCertificate: false); Assert.Equal("content=Hello+World%3F", result); - await server.StopAsync(); } } @@ -66,7 +65,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests } }; - using (var server = new TestServer(context => + await using (var server = new TestServer(context => { var tlsFeature = context.Features.Get(); Assert.NotNull(tlsFeature); @@ -83,7 +82,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var result = await server.HttpClientSlim.GetStringAsync($"https://localhost:{server.Port}/", validateCertificate: false); Assert.Equal("hello world", result); - await server.StopAsync(); } } @@ -103,11 +101,10 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests }; - using (var server = new TestServer(App, new TestServiceContext(LoggerFactory), listenOptions)) + await using (var server = new TestServer(App, new TestServiceContext(LoggerFactory), listenOptions)) { await Assert.ThrowsAnyAsync( () => server.HttpClientSlim.GetStringAsync($"https://localhost:{server.Port}/")); - await server.StopAsync(); } } @@ -126,7 +123,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests } }; - using (var server = new TestServer(context => + await using (var server = new TestServer(context => { var tlsFeature = context.Features.Get(); Assert.NotNull(tlsFeature); @@ -136,7 +133,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var result = await server.HttpClientSlim.GetStringAsync($"https://localhost:{server.Port}/", validateCertificate: false); Assert.Equal("hello world", result); - await server.StopAsync(); } } @@ -159,7 +155,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests } }; - using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory), listenOptions)) + await using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory), listenOptions)) { using (var connection = server.CreateConnection()) { @@ -170,7 +166,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests await stream.AuthenticateAsClientAsync("localhost", new X509CertificateCollection(), SslProtocols.Tls12 | SslProtocols.Tls11, false); Assert.True(stream.RemoteCertificate.Equals(_x509Certificate2)); } - await server.StopAsync(); } } @@ -195,7 +190,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests }) } }; - using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory), listenOptions)) + await using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory), listenOptions)) { using (var connection = server.CreateConnection()) { @@ -207,7 +202,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests Assert.True(stream.RemoteCertificate.Equals(_x509Certificate2)); Assert.Equal(1, selectorCalled); } - await server.StopAsync(); } } @@ -236,7 +230,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests }) } }; - using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory), listenOptions)) + await using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory), listenOptions)) { using (var connection = server.CreateConnection()) { @@ -258,7 +252,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests Assert.True(stream.RemoteCertificate.Equals(_x509Certificate2NoExt)); Assert.Equal(2, selectorCalled); } - await server.StopAsync(); } } @@ -280,7 +273,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests }) } }; - using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory), listenOptions)) + await using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory), listenOptions)) { using (var connection = server.CreateConnection()) { @@ -292,7 +285,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests stream.AuthenticateAsClientAsync("localhost", new X509CertificateCollection(), SslProtocols.Tls12 | SslProtocols.Tls11, false)); Assert.Equal(1, selectorCalled); } - await server.StopAsync(); } } @@ -318,7 +310,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests }) } }; - using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory), listenOptions)) + await using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory), listenOptions)) { using (var connection = server.CreateConnection()) { @@ -330,7 +322,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests Assert.True(stream.RemoteCertificate.Equals(_x509Certificate2)); Assert.Equal(1, selectorCalled); } - await server.StopAsync(); } } @@ -352,7 +343,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests }) } }; - using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory), listenOptions)) + await using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory), listenOptions)) { using (var connection = server.CreateConnection()) { @@ -364,7 +355,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests stream.AuthenticateAsClientAsync("localhost", new X509CertificateCollection(), SslProtocols.Tls12 | SslProtocols.Tls11, false)); Assert.Equal(1, selectorCalled); } - await server.StopAsync(); } } @@ -387,7 +377,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests } }; - using (var server = new TestServer(context => + await using (var server = new TestServer(context => { var tlsFeature = context.Features.Get(); Assert.NotNull(tlsFeature); @@ -405,7 +395,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests await stream.AuthenticateAsClientAsync("localhost", new X509CertificateCollection(), SslProtocols.Tls12 | SslProtocols.Tls11, false); await AssertConnectionResult(stream, true); } - await server.StopAsync(); } } @@ -420,11 +409,10 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests } }; - using (var server = new TestServer(context => context.Response.WriteAsync(context.Request.Scheme), new TestServiceContext(LoggerFactory), listenOptions)) + await using (var server = new TestServer(context => context.Response.WriteAsync(context.Request.Scheme), new TestServiceContext(LoggerFactory), listenOptions)) { var result = await server.HttpClientSlim.GetStringAsync($"https://localhost:{server.Port}/", validateCertificate: false); Assert.Equal("https", result); - await server.StopAsync(); } } @@ -444,7 +432,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests } }; - using (var server = new TestServer(context => context.Response.WriteAsync("hello world"), new TestServiceContext(LoggerFactory), listenOptions)) + await using (var server = new TestServer(context => context.Response.WriteAsync("hello world"), new TestServiceContext(LoggerFactory), listenOptions)) { // SslStream is used to ensure the certificate is actually passed to the server // HttpClient might not send the certificate because it is invalid or it doesn't match any @@ -455,7 +443,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var ex = await Assert.ThrowsAsync( async () => await stream.AuthenticateAsClientAsync("localhost", new X509CertificateCollection(), SslProtocols.Tls, false)); } - await server.StopAsync(); } } @@ -485,7 +472,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests } }; - using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory), listenOptions)) + await using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory), listenOptions)) { using (var connection = server.CreateConnection()) { @@ -494,7 +481,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests await AssertConnectionResult(stream, true); Assert.True(clientCertificateValidationCalled); } - await server.StopAsync(); } } @@ -517,7 +503,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests } }; - using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory), listenOptions)) + await using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory), listenOptions)) { using (var connection = server.CreateConnection()) { @@ -525,7 +511,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests await stream.AuthenticateAsClientAsync("localhost", new X509CertificateCollection(), SslProtocols.Tls12 | SslProtocols.Tls11, false); await AssertConnectionResult(stream, false); } - await server.StopAsync(); } } @@ -546,7 +531,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests } }; - using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory), listenOptions)) + await using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory), listenOptions)) { using (var connection = server.CreateConnection()) { @@ -554,7 +539,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests await stream.AuthenticateAsClientAsync("localhost", new X509CertificateCollection(), SslProtocols.Tls12 | SslProtocols.Tls11, false); await AssertConnectionResult(stream, false); } - await server.StopAsync(); } } @@ -584,7 +568,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests return context.Response.WriteAsync("hello world"); }; - using (var server = new TestServer(app, new TestServiceContext(LoggerFactory), listenOptions)) + await using (var server = new TestServer(app, new TestServiceContext(LoggerFactory), listenOptions)) { // SslStream is used to ensure the certificate is actually passed to the server // HttpClient might not send the certificate because it is invalid or it doesn't match any @@ -595,7 +579,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests await stream.AuthenticateAsClientAsync("localhost", new X509CertificateCollection(), SslProtocols.Tls12 | SslProtocols.Tls11, false); await AssertConnectionResult(stream, true); } - await server.StopAsync(); } } diff --git a/src/Servers/Kestrel/test/InMemory.FunctionalTests/HttpsTests.cs b/src/Servers/Kestrel/test/InMemory.FunctionalTests/HttpsTests.cs index a2848925b8..6f834ebc8c 100644 --- a/src/Servers/Kestrel/test/InMemory.FunctionalTests/HttpsTests.cs +++ b/src/Servers/Kestrel/test/InMemory.FunctionalTests/HttpsTests.cs @@ -121,7 +121,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var loggerProvider = new HandshakeErrorLoggerProvider(); LoggerFactory.AddProvider(loggerProvider); - using (var server = new TestServer(context => Task.CompletedTask, + await using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory), listenOptions => { @@ -134,7 +134,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests } await loggerProvider.FilterLogger.LogTcs.Task.DefaultTimeout(); - await server.StopAsync(); } Assert.Equal(1, loggerProvider.FilterLogger.LastEventId.Id); @@ -149,7 +148,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var loggerProvider = new HandshakeErrorLoggerProvider(); LoggerFactory.AddProvider(loggerProvider); - using (var server = new TestServer(context => Task.CompletedTask, + await using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory), listenOptions => { @@ -163,7 +162,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests } await loggerProvider.FilterLogger.LogTcs.Task.DefaultTimeout(); - await server.StopAsync(); } Assert.Equal(1, loggerProvider.FilterLogger.LastEventId.Id); @@ -179,7 +177,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var loggerProvider = new HandshakeErrorLoggerProvider(); LoggerFactory.AddProvider(loggerProvider); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var ct = httpContext.RequestAborted; while (!ct.IsCancellationRequested) @@ -213,7 +211,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests await sslStream.ReadAsync(new byte[32], 0, 32); } - await server.StopAsync(); } Assert.False(loggerProvider.ErrorLogger.ObjectDisposedExceptionLogged); @@ -226,7 +223,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var loggerProvider = new HandshakeErrorLoggerProvider(); LoggerFactory.AddProvider(loggerProvider); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { httpContext.Abort(); try @@ -269,7 +266,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var loggerProvider = new HandshakeErrorLoggerProvider(); LoggerFactory.AddProvider(loggerProvider); - using (var server = new TestServer(context => Task.CompletedTask, + await using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory), listenOptions => { @@ -283,7 +280,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests enabledSslProtocols: SslProtocols.Tls11 | SslProtocols.Tls12, checkCertificateRevocation: false); } - await server.StopAsync(); } Assert.False(loggerProvider.ErrorLogger.ObjectDisposedExceptionLogged); @@ -296,7 +292,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var loggerProvider = new HandshakeErrorLoggerProvider(); LoggerFactory.AddProvider(loggerProvider); - using (var server = new TestServer(context => Task.CompletedTask, + await using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory), listenOptions => { @@ -307,7 +303,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { connection.Reset(); } - await server.StopAsync(); } } @@ -323,7 +318,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var handshakeStartedTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); TimeSpan handshakeTimeout = default; - using (var server = new TestServer(context => Task.CompletedTask, + await using (var server = new TestServer(context => Task.CompletedTask, testContext, listenOptions => { @@ -348,7 +343,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests Assert.Equal(0, await connection.Stream.ReadAsync(new byte[1], 0, 1).DefaultTimeout()); } - await server.StopAsync(); } await loggerProvider.FilterLogger.LogTcs.Task.DefaultTimeout(); @@ -362,7 +356,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var loggerProvider = new HandshakeErrorLoggerProvider(); LoggerFactory.AddProvider(loggerProvider); - using (var server = new TestServer(context => Task.CompletedTask, + await using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory), listenOptions => { @@ -378,7 +372,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests enabledSslProtocols: SslProtocols.Tls, checkCertificateRevocation: false)); } - await server.StopAsync(); } await loggerProvider.FilterLogger.LogTcs.Task.DefaultTimeout(); diff --git a/src/Servers/Kestrel/test/InMemory.FunctionalTests/KeepAliveTimeoutTests.cs b/src/Servers/Kestrel/test/InMemory.FunctionalTests/KeepAliveTimeoutTests.cs index 80c5061719..422520f5b8 100644 --- a/src/Servers/Kestrel/test/InMemory.FunctionalTests/KeepAliveTimeoutTests.cs +++ b/src/Servers/Kestrel/test/InMemory.FunctionalTests/KeepAliveTimeoutTests.cs @@ -29,7 +29,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var testContext = new TestServiceContext(LoggerFactory); var heartbeatManager = new HeartbeatManager(testContext.ConnectionManager); - using (var server = CreateServer(testContext)) + await using (var server = CreateServer(testContext)) { using (var connection = server.CreateConnection()) { @@ -46,7 +46,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests await connection.WaitForConnectionClose(); } - await server.StopAsync(); } } @@ -56,7 +55,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var testContext = new TestServiceContext(LoggerFactory); var heartbeatManager = new HeartbeatManager(testContext.ConnectionManager); - using (var server = CreateServer(testContext)) + await using (var server = CreateServer(testContext)) { using (var connection = server.CreateConnection()) { @@ -74,7 +73,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests heartbeatManager.OnHeartbeat(testContext.SystemClock.UtcNow); } } - await server.StopAsync(); } } @@ -84,7 +82,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var testContext = new TestServiceContext(LoggerFactory); var heartbeatManager = new HeartbeatManager(testContext.ConnectionManager); - using (var server = CreateServer(testContext)) + await using (var server = CreateServer(testContext)) { using (var connection = server.CreateConnection()) { @@ -114,7 +112,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests ""); await ReceiveResponse(connection, testContext); } - await server.StopAsync(); } } @@ -125,7 +122,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var heartbeatManager = new HeartbeatManager(testContext.ConnectionManager); var cts = new CancellationTokenSource(); - using (var server = CreateServer(testContext, longRunningCt: cts.Token)) + await using (var server = CreateServer(testContext, longRunningCt: cts.Token)) { using (var connection = server.CreateConnection()) { @@ -154,7 +151,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests ""); await ReceiveResponse(connection, testContext); } - await server.StopAsync(); } } @@ -164,7 +160,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var testContext = new TestServiceContext(LoggerFactory); var heartbeatManager = new HeartbeatManager(testContext.ConnectionManager); - using (var server = CreateServer(testContext)) + await using (var server = CreateServer(testContext)) { using (var connection = server.CreateConnection()) { @@ -174,7 +170,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests await connection.WaitForConnectionClose(); } - await server.StopAsync(); } } @@ -185,7 +180,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var heartbeatManager = new HeartbeatManager(testContext.ConnectionManager); var cts = new CancellationTokenSource(); - using (var server = CreateServer(testContext, upgradeCt: cts.Token)) + await using (var server = CreateServer(testContext, upgradeCt: cts.Token)) { using (var connection = server.CreateConnection()) { @@ -212,7 +207,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests await connection.Receive("hello, world"); } - await server.StopAsync(); } } diff --git a/src/Servers/Kestrel/test/InMemory.FunctionalTests/LoggingConnectionAdapterTests.cs b/src/Servers/Kestrel/test/InMemory.FunctionalTests/LoggingConnectionAdapterTests.cs index ae8ad7768f..bb70e492bf 100644 --- a/src/Servers/Kestrel/test/InMemory.FunctionalTests/LoggingConnectionAdapterTests.cs +++ b/src/Servers/Kestrel/test/InMemory.FunctionalTests/LoggingConnectionAdapterTests.cs @@ -16,7 +16,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests [Fact] public async Task LoggingConnectionAdapterCanBeAddedBeforeAndAfterHttpsAdapter() { - using (var server = new TestServer(context => + await using (var server = new TestServer(context => { context.Response.ContentLength = 12; return context.Response.WriteAsync("Hello World!"); @@ -32,11 +32,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var response = await server.HttpClientSlim.GetStringAsync($"https://localhost:{server.Port}/", validateCertificate: false) .DefaultTimeout(); - - Assert.Equal("Hello World!", response); } - await server.StopAsync(); } } } diff --git a/src/Servers/Kestrel/test/InMemory.FunctionalTests/MaxRequestBodySizeTests.cs b/src/Servers/Kestrel/test/InMemory.FunctionalTests/MaxRequestBodySizeTests.cs index bc2fa11687..6feeeef83a 100644 --- a/src/Servers/Kestrel/test/InMemory.FunctionalTests/MaxRequestBodySizeTests.cs +++ b/src/Servers/Kestrel/test/InMemory.FunctionalTests/MaxRequestBodySizeTests.cs @@ -23,7 +23,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var globalMaxRequestBodySize = 0x100000000; BadHttpRequestException requestRejectedEx = null; - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { var buffer = new byte[1]; requestRejectedEx = await Assert.ThrowsAsync( @@ -48,7 +48,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } Assert.NotNull(requestRejectedEx); @@ -64,7 +63,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var perRequestMaxRequestBodySize = 0x100000000; BadHttpRequestException requestRejectedEx = null; - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { var feature = context.Features.Get(); Assert.Equal(globalMaxRequestBodySize, feature.MaxRequestBodySize); @@ -95,7 +94,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } Assert.NotNull(requestRejectedEx); @@ -105,7 +103,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests [Fact] public async Task DoesNotRejectRequestWithContentLengthHeaderExceedingGlobalLimitIfLimitDisabledPerRequest() { - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { var feature = context.Features.Get(); Assert.Equal(0, feature.MaxRequestBodySize); @@ -139,14 +137,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", "A"); } - await server.StopAsync(); } } [Fact] public async Task DoesNotRejectBodylessGetRequestWithZeroMaxRequestBodySize() { - using (var server = new TestServer(context => context.Request.Body.CopyToAsync(Stream.Null), + await using (var server = new TestServer(context => context.Request.Body.CopyToAsync(Stream.Null), new TestServiceContext { ServerOptions = { Limits = { MaxRequestBodySize = 0 } } })) { using (var connection = server.CreateConnection()) @@ -172,7 +169,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -184,7 +180,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var payload = new string('A', payloadSize); InvalidOperationException invalidOpEx = null; - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { var buffer = new byte[1]; Assert.Equal(1, await context.Request.Body.ReadAsync(buffer, 0, 1)); @@ -213,7 +209,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } Assert.NotNull(invalidOpEx); @@ -225,7 +220,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { InvalidOperationException invalidOpEx = null; - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { var upgradeFeature = context.Features.Get(); var stream = await upgradeFeature.UpgradeAsync(); @@ -253,7 +248,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests ""); await connection.ReceiveEnd(); } - await server.StopAsync(); } Assert.NotNull(invalidOpEx); @@ -266,7 +260,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests BadHttpRequestException requestRejectedEx1 = null; BadHttpRequestException requestRejectedEx2 = null; - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { var buffer = new byte[1]; requestRejectedEx1 = await Assert.ThrowsAsync( @@ -293,7 +287,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } Assert.NotNull(requestRejectedEx1); @@ -309,7 +302,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var globalMaxRequestBodySize = chunkedPayload.Length - 1; BadHttpRequestException requestRejectedEx = null; - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { var buffer = new byte[11]; requestRejectedEx = await Assert.ThrowsAsync(async () => @@ -341,7 +334,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } Assert.NotNull(requestRejectedEx); @@ -355,7 +347,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var trailingHeaders = "Trailing-Header: trailing-value\r\n\r\n"; var globalMaxRequestBodySize = chunkedPayload.Length; - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { var offset = 0; var count = 0; @@ -387,7 +379,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -399,7 +390,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var firstRequest = true; BadHttpRequestException requestRejectedEx = null; - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { var feature = context.Features.Get(); Assert.Equal(globalMaxRequestBodySize, feature.MaxRequestBodySize); @@ -456,7 +447,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } Assert.NotNull(requestRejectedEx); @@ -469,7 +459,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests BadHttpRequestException requestRejectedEx1 = null; BadHttpRequestException requestRejectedEx2 = null; - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { var buffer = new byte[1]; requestRejectedEx1 = await Assert.ThrowsAsync( @@ -496,7 +486,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } Assert.NotNull(requestRejectedEx1); diff --git a/src/Servers/Kestrel/test/InMemory.FunctionalTests/MaxRequestLineSizeTests.cs b/src/Servers/Kestrel/test/InMemory.FunctionalTests/MaxRequestLineSizeTests.cs index 80f1692ba2..70c83b3e83 100644 --- a/src/Servers/Kestrel/test/InMemory.FunctionalTests/MaxRequestLineSizeTests.cs +++ b/src/Servers/Kestrel/test/InMemory.FunctionalTests/MaxRequestLineSizeTests.cs @@ -28,7 +28,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests [InlineData("DELETE /a%20b%20c/d%20e?f=ghi HTTP/1.1\r\nHost:\r\n\r\n", 1027)] public async Task ServerAcceptsRequestLineWithinLimit(string request, int limit) { - using (var server = CreateServer(limit)) + await using (var server = CreateServer(limit)) { using (var connection = server.CreateConnection()) { @@ -44,7 +44,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -55,7 +54,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests [InlineData("DELETE /a%20b%20c/d%20e?f=ghi HTTP/1.1\r\n")] public async Task ServerRejectsRequestLineExceedingLimit(string requestLine) { - using (var server = CreateServer(requestLine.Length - 1)) + await using (var server = CreateServer(requestLine.Length - 1)) { using (var connection = server.CreateConnection()) { @@ -68,7 +67,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } diff --git a/src/Servers/Kestrel/test/InMemory.FunctionalTests/RequestBodyTimeoutTests.cs b/src/Servers/Kestrel/test/InMemory.FunctionalTests/RequestBodyTimeoutTests.cs index ae659dbb34..f96848bc47 100644 --- a/src/Servers/Kestrel/test/InMemory.FunctionalTests/RequestBodyTimeoutTests.cs +++ b/src/Servers/Kestrel/test/InMemory.FunctionalTests/RequestBodyTimeoutTests.cs @@ -27,7 +27,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var appRunningEvent = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - using (var server = new TestServer(context => + await using (var server = new TestServer(context => { context.Features.Get().MinDataRate = new MinDataRate(bytesPerSecond: 1, gracePeriod: gracePeriod); @@ -88,7 +88,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -107,7 +106,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var appRunningEvent = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - using (var server = new TestServer(context => + await using (var server = new TestServer(context => { context.Features.Get().MinDataRate = null; @@ -134,7 +133,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } Assert.Contains(TestSink.Writes, w => w.EventId.Id == 32 && w.LogLevel == LogLevel.Information); @@ -151,7 +149,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var appRunningTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var exceptionSwallowedTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { context.Features.Get().MinDataRate = new MinDataRate(bytesPerSecond: 1, gracePeriod: gracePeriod); @@ -208,7 +206,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", "hello, world"); } - await server.StopAsync(); } } } diff --git a/src/Servers/Kestrel/test/InMemory.FunctionalTests/RequestHeaderLimitsTests.cs b/src/Servers/Kestrel/test/InMemory.FunctionalTests/RequestHeaderLimitsTests.cs index bf26bf13ab..5ab5b467e8 100644 --- a/src/Servers/Kestrel/test/InMemory.FunctionalTests/RequestHeaderLimitsTests.cs +++ b/src/Servers/Kestrel/test/InMemory.FunctionalTests/RequestHeaderLimitsTests.cs @@ -27,7 +27,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var headers = MakeHeaders(headerCount); - using (var server = CreateServer(maxRequestHeadersTotalSize: headers.Length + extraLimit)) + await using (var server = CreateServer(maxRequestHeadersTotalSize: headers.Length + extraLimit)) { using (var connection = server.CreateConnection()) { @@ -43,7 +43,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -60,7 +59,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var headers = MakeHeaders(headerCount); - using (var server = CreateServer(maxRequestHeaderCount: maxHeaderCount)) + await using (var server = CreateServer(maxRequestHeaderCount: maxHeaderCount)) { using (var connection = server.CreateConnection()) { @@ -76,7 +75,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -87,7 +85,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var headers = MakeHeaders(headerCount); - using (var server = CreateServer(maxRequestHeadersTotalSize: headers.Length - 1)) + await using (var server = CreateServer(maxRequestHeadersTotalSize: headers.Length - 1)) { using (var connection = server.CreateConnection()) { @@ -100,7 +98,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -112,7 +109,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var headers = MakeHeaders(headerCount); - using (var server = CreateServer(maxRequestHeaderCount: maxHeaderCount)) + await using (var server = CreateServer(maxRequestHeaderCount: maxHeaderCount)) { using (var connection = server.CreateConnection()) { @@ -125,7 +122,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } diff --git a/src/Servers/Kestrel/test/InMemory.FunctionalTests/RequestHeadersTimeoutTests.cs b/src/Servers/Kestrel/test/InMemory.FunctionalTests/RequestHeadersTimeoutTests.cs index 0701536fe9..65448bd6d2 100644 --- a/src/Servers/Kestrel/test/InMemory.FunctionalTests/RequestHeadersTimeoutTests.cs +++ b/src/Servers/Kestrel/test/InMemory.FunctionalTests/RequestHeadersTimeoutTests.cs @@ -28,7 +28,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var testContext = new TestServiceContext(LoggerFactory); var heartbeatManager = new HeartbeatManager(testContext.ConnectionManager); - using (var server = CreateServer(testContext)) + await using (var server = CreateServer(testContext)) { using (var connection = server.CreateConnection()) { @@ -42,7 +42,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests await ReceiveTimeoutResponse(connection, testContext); } - await server.StopAsync(); } } @@ -52,7 +51,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var testContext = new TestServiceContext(LoggerFactory); var heartbeatManager = new HeartbeatManager(testContext.ConnectionManager); - using (var server = CreateServer(testContext)) + await using (var server = CreateServer(testContext)) { using (var connection = server.CreateConnection()) { @@ -72,7 +71,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests await ReceiveResponse(connection, testContext); } - await server.StopAsync(); } } @@ -84,7 +82,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var testContext = new TestServiceContext(LoggerFactory); var heartbeatManager = new HeartbeatManager(testContext.ConnectionManager); - using (var server = CreateServer(testContext)) + await using (var server = CreateServer(testContext)) { using (var connection = server.CreateConnection()) { @@ -96,7 +94,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests await ReceiveTimeoutResponse(connection, testContext); } - await server.StopAsync(); } } @@ -109,7 +106,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests // Disable response rate, so we can finish the send loop without timing out the response. testContext.ServerOptions.Limits.MinResponseDataRate = null; - using (var server = CreateServer(testContext)) + await using (var server = CreateServer(testContext)) { using (var connection = server.CreateConnection()) { @@ -125,7 +122,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests await connection.WaitForConnectionClose(); } - await server.StopAsync(); } } diff --git a/src/Servers/Kestrel/test/InMemory.FunctionalTests/RequestTargetProcessingTests.cs b/src/Servers/Kestrel/test/InMemory.FunctionalTests/RequestTargetProcessingTests.cs index e086a527e4..8f2ef644f1 100644 --- a/src/Servers/Kestrel/test/InMemory.FunctionalTests/RequestTargetProcessingTests.cs +++ b/src/Servers/Kestrel/test/InMemory.FunctionalTests/RequestTargetProcessingTests.cs @@ -20,7 +20,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { Assert.Equal("/\u0041\u030A/B/\u0041\u030A", context.Request.Path.Value); @@ -42,7 +42,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", "Hello World"); } - await server.StopAsync(); } } @@ -66,7 +65,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { Assert.Equal(requestTarget, context.Features.Get().RawTarget); @@ -88,7 +87,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", "Hello World"); } - await server.StopAsync(); } } @@ -100,7 +98,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var method = (HttpMethod)intMethod; var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { Assert.Equal(requestTarget, context.Features.Get().RawTarget); Assert.Empty(context.Request.Path.Value); @@ -129,7 +127,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", "Hello World"); } - await server.StopAsync(); } } } diff --git a/src/Servers/Kestrel/test/InMemory.FunctionalTests/RequestTests.cs b/src/Servers/Kestrel/test/InMemory.FunctionalTests/RequestTests.cs index bca9b9d1b1..8ed69dc763 100644 --- a/src/Servers/Kestrel/test/InMemory.FunctionalTests/RequestTests.cs +++ b/src/Servers/Kestrel/test/InMemory.FunctionalTests/RequestTests.cs @@ -30,7 +30,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var requestBodyPersisted = false; var responseBodyPersisted = false; - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { if (context.Request.Body is MemoryStream) { @@ -53,8 +53,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests Assert.False(requestBodyPersisted); Assert.False(responseBodyPersisted); - - await server.StopAsync(); } } @@ -63,7 +61,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var responseBodyPersisted = false; PipeWriter bodyPipe = null; - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { if (context.Response.BodyWriter == bodyPipe) { @@ -79,8 +77,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests Assert.Equal(string.Empty, await server.HttpClientSlim.GetStringAsync($"http://localhost:{server.Port}/")); Assert.False(responseBodyPersisted); - - await server.StopAsync(); } } @@ -89,7 +85,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var responseBodyPersisted = false; PipeWriter bodyPipe = null; - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { if (context.Response.BodyWriter == bodyPipe) { @@ -104,8 +100,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests Assert.Equal("hello, world", await server.HttpClientSlim.GetStringAsync($"http://localhost:{server.Port}/")); Assert.False(responseBodyPersisted); - - await server.StopAsync(); } } @@ -116,7 +110,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var readTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var cts = new CancellationTokenSource(); - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { var buffer = new byte[1024]; try @@ -174,7 +168,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", "Read cancelled"); } - await server.StopAsync(); } } @@ -183,7 +176,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var dataRead = false; - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { var stream = await context.Features.Get().UpgradeAsync(); var data = new byte[3]; @@ -208,7 +201,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } Assert.True(dataRead); @@ -237,7 +229,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var rawTargetTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var queryTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { pathTcs.TrySetResult(context.Request.Path); queryTcs.TrySetResult(context.Request.Query); @@ -280,7 +272,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests Assert.Equal(queryValue, queryTcs.Task.Result["q"]); } } - await server.StopAsync(); } } @@ -288,7 +279,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests public async Task AppCanSetTraceIdentifier() { const string knownId = "xyz123"; - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { context.TraceIdentifier = knownId; await context.Response.WriteAsync(context.TraceIdentifier); @@ -296,7 +287,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var requestId = await server.HttpClientSlim.GetStringAsync($"http://localhost:{server.Port}/"); Assert.Equal(knownId, requestId); - await server.StopAsync(); } } @@ -306,7 +296,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests const int identifierLength = 22; const int iterations = 10; - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { Assert.Equal(identifierLength, Encoding.ASCII.GetByteCount(context.TraceIdentifier)); context.Response.ContentLength = identifierLength; @@ -353,7 +343,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests usedIds.Add(id); } } - await server.StopAsync(); } } @@ -362,7 +351,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(TestApp.EchoAppChunked, testContext)) + await using (var server = new TestServer(TestApp.EchoAppChunked, testContext)) { using (var connection = server.CreateConnection()) { @@ -388,7 +377,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", "Goodbye"); } - await server.StopAsync(); } } @@ -398,7 +386,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(TestApp.EchoApp, testContext)) + await using (var server = new TestServer(TestApp.EchoApp, testContext)) { using (var connection = server.CreateConnection()) { @@ -429,7 +417,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", "Hello World"); } - await server.StopAsync(); } } @@ -438,7 +425,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(TestApp.EchoAppChunked, testContext)) + await using (var server = new TestServer(TestApp.EchoAppChunked, testContext)) { using (var connection = server.CreateConnection()) { @@ -464,7 +451,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", "Goodbye"); } - await server.StopAsync(); } } @@ -473,7 +459,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(TestApp.EchoApp, testContext)) + await using (var server = new TestServer(TestApp.EchoApp, testContext)) { using (var connection = server.CreateConnection()) { @@ -504,7 +490,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", "Goodbye"); } - await server.StopAsync(); } } @@ -513,7 +498,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(TestApp.EchoAppChunked, testContext)) + await using (var server = new TestServer(TestApp.EchoAppChunked, testContext)) { using (var connection = server.CreateConnection()) { @@ -561,7 +546,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", "Goodbye"); } - await server.StopAsync(); } } @@ -570,7 +554,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(TestApp.EchoAppChunked, testContext)) + await using (var server = new TestServer(TestApp.EchoAppChunked, testContext)) { using (var connection = server.CreateConnection()) { @@ -594,7 +578,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", "Hello World"); } - await server.StopAsync(); } } @@ -603,7 +586,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { // This will hang if 0 content length is not assumed by the server Assert.Equal(0, await httpContext.Request.Body.ReadAsync(new byte[1], 0, 1).DefaultTimeout()); @@ -641,7 +624,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -650,7 +632,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var readResult = await httpContext.Request.BodyReader.ReadAsync().AsTask().DefaultTimeout(); // This will hang if 0 content length is not assumed by the server @@ -689,7 +671,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -698,7 +679,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var readResult = await httpContext.Request.BodyReader.ReadAsync(); // This will hang if 0 content length is not assumed by the server @@ -722,8 +703,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - - await server.StopAsync(); } } @@ -732,7 +711,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var readResult = await httpContext.Request.BodyReader.ReadAsync(); // This will hang if 0 content length is not assumed by the server @@ -759,17 +738,15 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - - await server.StopAsync(); } } - [Fact] + [Fact] public async Task ContentLengthReadAsyncPipeReaderBufferRequestBodyMultipleTimes() { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var readResult = await httpContext.Request.BodyReader.ReadAsync(); // This will hang if 0 content length is not assumed by the server @@ -800,8 +777,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - - await server.StopAsync(); } } @@ -811,7 +786,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var testContext = new TestServiceContext(LoggerFactory); var tcs = new TaskCompletionSource(); var tcs2 = new TaskCompletionSource(); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var readResult = await httpContext.Request.BodyReader.ReadAsync(); Assert.Equal(3, readResult.Buffer.Length); @@ -850,8 +825,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - - await server.StopAsync(); } } @@ -859,7 +832,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests public async Task ContentLengthDoesNotConsumeEntireBufferDoesNotThrow() { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var readResult = await httpContext.Request.BodyReader.ReadAsync(); @@ -886,8 +859,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - - await server.StopAsync(); } } @@ -900,7 +871,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests Scheduler = PipeScheduler.Inline }; - using (var server = new TestServer(TestApp.EchoAppChunked, testContext)) + await using (var server = new TestServer(TestApp.EchoAppChunked, testContext)) { using (var connection = server.CreateConnection()) { @@ -919,7 +890,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests connection.ShutdownSend(); await connection.ReceiveEnd(); } - await server.StopAsync(); } } @@ -931,7 +901,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests IHeaderDictionary originalRequestHeaders = null; var firstRequest = true; - using (var server = new TestServer(httpContext => + await using (var server = new TestServer(httpContext => { var requestFeature = httpContext.Features.Get(); @@ -970,7 +940,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -981,7 +950,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { var upgradeFeature = context.Features.Get(); var duplexStream = await upgradeFeature.UpgradeAsync(); @@ -1008,7 +977,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", message); } - await server.StopAsync(); } } @@ -1024,7 +992,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests IHeaderDictionary lastRequestHeaders = null; IHeaderDictionary lastResponseHeaders = null; - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { if (context.Request.Body != lastStream) { @@ -1085,7 +1053,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests Assert.Equal(1, streamCount); Assert.Equal(1, requestHeadersCount); Assert.Equal(1, responseHeadersCount); - await server.StopAsync(); } } @@ -1093,7 +1060,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests [MemberData(nameof(HostHeaderData))] public async Task MatchesValidRequestTargetAndHostHeader(string request, string hostHeader) { - using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory))) + await using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory))) { using (var connection = server.CreateConnection()) { @@ -1104,7 +1071,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests await connection.Receive("HTTP/1.1 200 OK"); } - await server.StopAsync(); } } @@ -1112,7 +1078,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests public async Task ServerConsumesKeepAliveContentLengthRequest() { // The app doesn't read the request body, so it should be consumed by the server - using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory))) + await using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory))) { using (var connection = server.CreateConnection()) { @@ -1146,7 +1112,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -1154,7 +1119,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests public async Task ServerConsumesKeepAliveChunkedRequest() { // The app doesn't read the request body, so it should be consumed by the server - using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory))) + await using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory))) { using (var connection = server.CreateConnection()) { @@ -1195,7 +1160,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -1203,7 +1167,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests public async Task NonKeepAliveRequestNotConsumedByAppCompletes() { // The app doesn't read the request body, so it should be consumed by the server - using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory))) + await using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory))) { using (var connection = server.CreateConnection()) { @@ -1222,7 +1186,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -1230,7 +1193,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests public async Task UpgradedRequestNotConsumedByAppCompletes() { // The app doesn't read the request body, so it should be consumed by the server - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { var upgradeFeature = context.Features.Get(); var duplexStream = await upgradeFeature.UpgradeAsync(); @@ -1255,7 +1218,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", "goodbye"); } - await server.StopAsync(); } } @@ -1268,7 +1230,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var serviceContext = new TestServiceContext(LoggerFactory); var heartbeatManager = new HeartbeatManager(serviceContext.ConnectionManager); - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { context.Features.Get().MinDataRate = new MinDataRate(bytesPerSecond: double.MaxValue, gracePeriod: Heartbeat.Interval + TimeSpan.FromTicks(1)); @@ -1316,14 +1278,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", "hello"); } - await server.StopAsync(); } } [Fact] public async Task SynchronousReadsDisallowedByDefault() { - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { var bodyControlFeature = context.Features.Get(); Assert.False(bodyControlFeature.AllowSynchronousIO); @@ -1374,7 +1335,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests [Fact] public async Task SynchronousReadsAllowedByOptIn() { - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { var bodyControlFeature = context.Features.Get(); Assert.False(bodyControlFeature.AllowSynchronousIO); @@ -1411,7 +1372,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", "Hello"); } - await server.StopAsync(); } } @@ -1423,7 +1383,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests ServerOptions = { AllowSynchronousIO = false } }; - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { var bodyControlFeature = context.Features.Get(); Assert.False(bodyControlFeature.AllowSynchronousIO); @@ -1456,7 +1416,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -1468,7 +1427,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests ServerOptions = { AllowSynchronousIO = true } }; - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { var bodyControlFeature = context.Features.Get(); Assert.True(bodyControlFeature.AllowSynchronousIO); @@ -1508,7 +1467,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var tcs = new TaskCompletionSource(); var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = httpContext.Response; var request = httpContext.Request; @@ -1550,7 +1509,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", "Hello World"); } - await server.StopAsync(); } } @@ -1559,7 +1517,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = httpContext.Response; var request = httpContext.Request; @@ -1595,7 +1553,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", "Hello World"); } - await server.StopAsync(); } } @@ -1605,7 +1562,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var tcs = new TaskCompletionSource(); var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = httpContext.Response; var request = httpContext.Request; @@ -1639,7 +1596,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } diff --git a/src/Servers/Kestrel/test/InMemory.FunctionalTests/ResponseDrainingTests.cs b/src/Servers/Kestrel/test/InMemory.FunctionalTests/ResponseDrainingTests.cs index 2bf646f823..f0603fef3a 100644 --- a/src/Servers/Kestrel/test/InMemory.FunctionalTests/ResponseDrainingTests.cs +++ b/src/Servers/Kestrel/test/InMemory.FunctionalTests/ResponseDrainingTests.cs @@ -32,7 +32,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests var heartbeatManager = new HeartbeatManager(testContext.ConnectionManager); var minRate = new MinDataRate(16384, TimeSpan.FromSeconds(2)); - using (var server = new TestServer(context => + await using (var server = new TestServer(context => { context.Features.Get().MinDataRate = minRate; return Task.CompletedTask; @@ -79,7 +79,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Tests Assert.Single(TestApplicationErrorLogger.Messages, w => w.EventId.Id == 28 && w.LogLevel == LogLevel.Information); } - await server.StopAsync(); } } } diff --git a/src/Servers/Kestrel/test/InMemory.FunctionalTests/ResponseTests.cs b/src/Servers/Kestrel/test/InMemory.FunctionalTests/ResponseTests.cs index c9705fefb8..3980092bd1 100644 --- a/src/Servers/Kestrel/test/InMemory.FunctionalTests/ResponseTests.cs +++ b/src/Servers/Kestrel/test/InMemory.FunctionalTests/ResponseTests.cs @@ -35,7 +35,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var onStartingCalled = false; TaskCompletionSource onCompletedTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - using (var server = new TestServer(context => + await using (var server = new TestServer(context => { context.Response.OnStarting(() => Task.Run(() => onStartingCalled = true)); context.Response.OnCompleted(() => Task.Run(() => @@ -65,7 +65,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests await onCompletedTcs.Task.DefaultTimeout(); Assert.False(onStartingCalled); } - await server.StopAsync(); } } @@ -74,7 +73,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { InvalidOperationException ex = null; - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { await context.Response.WriteAsync("hello, world"); await context.Response.BodyWriter.FlushAsync(); @@ -101,7 +100,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests Assert.NotNull(ex); } - await server.StopAsync(); } } @@ -110,7 +108,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { InvalidOperationException ex = null; - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { await context.Response.StartAsync(); ex = Assert.Throws(() => context.Response.OnStarting(_ => Task.CompletedTask, null)); @@ -134,7 +132,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests Assert.NotNull(ex); } - await server.StopAsync(); } } @@ -146,7 +143,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var appTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var writeBlockedTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { try { @@ -202,7 +199,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests await Assert.ThrowsAsync(() => appTcs.Task).DefaultTimeout(); } - await server.StopAsync(); } } @@ -304,7 +300,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests [Fact] public async Task OnCompletedExceptionShouldNotPreventAResponse() { - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { context.Response.OnCompleted(_ => throw new Exception(), null); await context.Response.WriteAsync("hello, world"); @@ -328,7 +324,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -337,7 +332,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var delayTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { context.Response.OnCompleted(async () => { @@ -366,7 +361,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests } delayTcs.SetResult(null); - await server.StopAsync(); } } @@ -375,7 +369,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var onCompletedTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - using (var server = new TestServer(httpContext => + await using (var server = new TestServer(httpContext => { httpContext.Response.OnCompleted(() => Task.Run(() => { @@ -400,7 +394,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } await onCompletedTcs.Task.DefaultTimeout(); @@ -412,7 +405,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { BadHttpRequestException readException = null; - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { readException = await Assert.ThrowsAsync( async () => await httpContext.Request.Body.ReadAsync(new byte[1], 0, 1)); @@ -433,7 +426,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } Assert.NotNull(readException); @@ -445,7 +437,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests [Fact] public async Task TransferEncodingChunkedSetOnUnknownLengthHttp11Response() { - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { await httpContext.Response.WriteAsync("hello, "); await httpContext.Response.WriteAsync("world"); @@ -471,7 +463,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -480,7 +471,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests [InlineData(StatusCodes.Status304NotModified)] public async Task TransferEncodingChunkedNotSetOnNonBodyResponse(int statusCode) { - using (var server = new TestServer(httpContext => + await using (var server = new TestServer(httpContext => { httpContext.Response.StatusCode = statusCode; return Task.CompletedTask; @@ -499,14 +490,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } [Fact] public async Task ContentLengthZeroSetOn205Response() { - using (var server = new TestServer(httpContext => + await using (var server = new TestServer(httpContext => { httpContext.Response.StatusCode = 205; return Task.CompletedTask; @@ -526,7 +516,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -537,7 +526,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var responseWriteTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { httpContext.Response.StatusCode = statusCode; @@ -572,7 +561,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -581,7 +569,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var responseWriteTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { httpContext.Response.StatusCode = 205; @@ -617,14 +605,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } [Fact] public async Task TransferEncodingNotSetOnHeadResponse() { - using (var server = new TestServer(httpContext => + await using (var server = new TestServer(httpContext => { return Task.CompletedTask; }, new TestServiceContext(LoggerFactory))) @@ -642,7 +629,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -657,7 +643,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests .Setup(trace => trace.ConnectionHeadResponseBodyWrite(It.IsAny(), response.Length)) .Callback((connectionId, count) => logTcs.SetResult(null)); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { await httpContext.Response.WriteAsync(response); await httpContext.Response.BodyWriter.FlushAsync(); @@ -681,7 +667,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests // might be 1 by the time ProduceEnd() gets called and the message is logged. await logTcs.Task.DefaultTimeout(); } - await server.StopAsync(); } mockKestrelTrace.Verify(kestrelTrace => @@ -696,7 +681,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests ServerOptions = { AllowSynchronousIO = true } }; - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { httpContext.Response.ContentLength = 11; await httpContext.Response.BodyWriter.WriteAsync(new Memory(Encoding.ASCII.GetBytes("hello,"), 0, 6)); @@ -719,7 +704,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests await connection.WaitForConnectionClose(); } - await server.StopAsync(); } var logMessage = Assert.Single(TestApplicationErrorLogger.Messages, message => message.LogLevel == LogLevel.Error); @@ -735,7 +719,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var serviceContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { httpContext.Response.ContentLength = 11; await httpContext.Response.WriteAsync("hello,"); @@ -756,7 +740,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", "hello,"); } - await server.StopAsync(); } var logMessage = Assert.Single(TestApplicationErrorLogger.Messages, message => message.LogLevel == LogLevel.Error); @@ -773,7 +756,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests ServerOptions = { AllowSynchronousIO = true } }; - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = Encoding.ASCII.GetBytes("hello, world"); httpContext.Response.ContentLength = 5; @@ -795,7 +778,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } var logMessage = Assert.Single(TestApplicationErrorLogger.Messages, message => message.LogLevel == LogLevel.Error); @@ -809,7 +791,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var serviceContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = Encoding.ASCII.GetBytes("hello, world"); httpContext.Response.ContentLength = 5; @@ -831,7 +813,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } var logMessage = Assert.Single(TestApplicationErrorLogger.Messages, message => message.LogLevel == LogLevel.Error); @@ -852,7 +833,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests logTcs.SetResult(null); }); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { httpContext.Response.ContentLength = 13; await httpContext.Response.WriteAsync("hello, world"); @@ -883,7 +864,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests // The server should close the connection in this situation. await connection.WaitForConnectionClose(); } - await server.StopAsync(); } mockTrace.Verify(trace => @@ -900,7 +880,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var requestAborted = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var mockTrace = new Mock(); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { httpContext.RequestAborted.Register(() => { @@ -936,7 +916,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests // abort triggered by the connection RST and the abort called when // disposing the server. await requestAborted.Task.DefaultTimeout(); - await server.StopAsync(); } // With the server disposed we know all connections were drained and all messages were logged. @@ -948,7 +927,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var serviceContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(httpContext => + await using (var server = new TestServer(httpContext => { httpContext.Response.ContentLength = 5; return Task.CompletedTask; @@ -975,7 +954,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } var error = TestApplicationErrorLogger.Messages.Where(message => message.LogLevel == LogLevel.Error); @@ -990,7 +968,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var serviceContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { httpContext.Response.ContentLength = 0; @@ -1014,7 +992,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } Assert.Empty(TestApplicationErrorLogger.Messages.Where(message => message.LogLevel == LogLevel.Error)); @@ -1029,7 +1006,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var serviceContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { httpContext.Response.Headers["Transfer-Encoding"] = "chunked"; httpContext.Response.ContentLength = 13; @@ -1051,7 +1028,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", "hello, world"); } - await server.StopAsync(); } Assert.Empty(TestApplicationErrorLogger.Messages.Where(message => message.LogLevel == LogLevel.Error)); @@ -1066,7 +1042,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var serviceContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { httpContext.Response.Headers["Transfer-Encoding"] = "chunked"; httpContext.Response.ContentLength = 11; @@ -1088,7 +1064,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", "hello, world"); } - await server.StopAsync(); } Assert.Empty(TestApplicationErrorLogger.Messages.Where(message => message.LogLevel == LogLevel.Error)); @@ -1097,7 +1072,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests [Fact] public async Task HeadResponseCanContainContentLengthHeader() { - using (var server = new TestServer(httpContext => + await using (var server = new TestServer(httpContext => { httpContext.Response.ContentLength = 42; return Task.CompletedTask; @@ -1117,7 +1092,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -1126,7 +1100,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var flushed = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { httpContext.Response.ContentLength = 12; await httpContext.Response.WriteAsync("hello, world"); @@ -1149,7 +1123,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests flushed.SetResult(null); } - await server.StopAsync(); } } @@ -1160,7 +1133,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var serviceContext = new TestServiceContext(LoggerFactory) { ServerOptions = { AllowSynchronousIO = true } }; - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { httpContext.Response.ContentLength = 12; await httpContext.Response.BodyWriter.WriteAsync(new Memory(Encoding.ASCII.GetBytes("hello, world"), 0, 12)); @@ -1183,7 +1156,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests flushed.SetResult(null); } - await server.StopAsync(); } } @@ -1192,7 +1164,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var flushed = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { httpContext.Response.ContentLength = 12; await httpContext.Response.WriteAsync(""); @@ -1218,7 +1190,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests await connection.Receive("hello, world"); } - await server.StopAsync(); } } @@ -1228,7 +1199,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var expectedResponse = string.Empty; var responseWritten = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { try { @@ -1260,7 +1231,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", expectedResponse); } - await server.StopAsync(); } } @@ -1269,7 +1239,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests [InlineData("chunked, gzip")] public async Task ConnectionClosedWhenChunkedIsNotFinalTransferCoding(string responseTransferEncoding) { - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { httpContext.Response.Headers["Transfer-Encoding"] = responseTransferEncoding; await httpContext.Response.WriteAsync("hello, world"); @@ -1306,7 +1276,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", "hello, world"); } - await server.StopAsync(); } } @@ -1315,7 +1284,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests [InlineData("chunked, gzip")] public async Task ConnectionClosedWhenChunkedIsNotFinalTransferCodingEvenIfConnectionKeepAliveSetInResponse(string responseTransferEncoding) { - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { httpContext.Response.Headers["Connection"] = "keep-alive"; httpContext.Response.Headers["Transfer-Encoding"] = responseTransferEncoding; @@ -1353,7 +1322,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", "hello, world"); } - await server.StopAsync(); } } @@ -1362,7 +1330,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests [InlineData("gzip, chunked")] public async Task ConnectionKeptAliveWhenChunkedIsFinalTransferCoding(string responseTransferEncoding) { - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { httpContext.Response.Headers["Transfer-Encoding"] = responseTransferEncoding; @@ -1397,7 +1365,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", "hello, world"); } - await server.StopAsync(); } } @@ -1406,7 +1373,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var serviceContext = new TestServiceContext(LoggerFactory) { ServerOptions = { AllowSynchronousIO = true } }; - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { httpContext.Response.OnStarting(() => { @@ -1440,7 +1407,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -1449,7 +1415,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var serviceContext = new TestServiceContext(LoggerFactory) { ServerOptions = { AllowSynchronousIO = true } }; - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { httpContext.Response.OnStarting(() => { @@ -1483,7 +1449,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -1492,7 +1457,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var serviceContext = new TestServiceContext(LoggerFactory) { ServerOptions = { AllowSynchronousIO = true } }; - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { httpContext.Response.OnStarting(() => { @@ -1529,7 +1494,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -1538,7 +1502,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var serviceContext = new TestServiceContext(LoggerFactory) { ServerOptions = { AllowSynchronousIO = true } }; - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { httpContext.Response.OnStarting(() => { @@ -1575,14 +1539,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } [Fact] public async Task FirstWriteAsyncVerifiedAfterOnStarting() { - using (var server = new TestServer(httpContext => + await using (var server = new TestServer(httpContext => { httpContext.Response.OnStarting(() => { @@ -1616,14 +1579,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } [Fact] public async Task SubsequentWriteAsyncVerifiedAfterOnStarting() { - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { httpContext.Response.OnStarting(() => { @@ -1660,14 +1622,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } [Fact] public async Task WhenResponseAlreadyStartedResponseEndedBeforeConsumingRequestBody() { - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { await httpContext.Response.WriteAsync("hello, world"); }, new TestServiceContext(LoggerFactory))) @@ -1698,14 +1659,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } [Fact] public async Task WhenResponseNotStartedResponseEndedBeforeConsumingRequestBody() { - using (var server = new TestServer(httpContext => Task.CompletedTask, + await using (var server = new TestServer(httpContext => Task.CompletedTask, new TestServiceContext(LoggerFactory))) { using (var connection = server.CreateConnection()) @@ -1727,7 +1687,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } Assert.Contains(TestApplicationErrorLogger.Messages, w => w.EventId.Id == 17 && w.LogLevel == LogLevel.Information && w.Exception is BadHttpRequestException @@ -1738,7 +1697,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests public async Task RequestDrainingFor100ContinueDoesNotBlockResponse() { var foundMessage = false; - using (var server = new TestServer(httpContext => + await using (var server = new TestServer(httpContext => { return httpContext.Request.Body.ReadAsync(new byte[1], 0, 1); }, new TestServiceContext(LoggerFactory))) @@ -1798,7 +1757,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests await connection.ReceiveEnd(); } - await server.StopAsync(); } Assert.True(foundMessage, "Expected log not found"); @@ -1807,7 +1765,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests [Fact] public async Task Sending100ContinueDoesNotPreventAutomatic400Responses() { - using (var server = new TestServer(httpContext => + await using (var server = new TestServer(httpContext => { return httpContext.Request.Body.ReadAsync(new byte[1], 0, 1); }, new TestServiceContext(LoggerFactory))) @@ -1842,7 +1800,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } Assert.Contains(TestApplicationErrorLogger.Messages, w => w.EventId.Id == 17 && w.LogLevel == LogLevel.Information && w.Exception is BadHttpRequestException @@ -1852,7 +1809,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests [Fact] public async Task Sending100ContinueAndResponseSendsChunkTerminatorBeforeConsumingRequestBody() { - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { await httpContext.Request.Body.ReadAsync(new byte[1], 0, 1); await httpContext.Response.WriteAsync("hello, world"); @@ -1893,7 +1850,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -1902,7 +1858,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var serviceContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(TestApp.EchoApp, serviceContext)) + await using (var server = new TestServer(TestApp.EchoApp, serviceContext)) { using (var connection = server.CreateConnection()) { @@ -1918,7 +1874,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", "Hello World"); } - await server.StopAsync(); } } @@ -1927,7 +1882,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(TestApp.EmptyApp, testContext)) + await using (var server = new TestServer(TestApp.EmptyApp, testContext)) { using (var connection = server.CreateConnection()) { @@ -1951,7 +1906,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -1960,7 +1914,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { Assert.Equal(0, await httpContext.Request.Body.ReadAsync(new byte[1], 0, 1).DefaultTimeout()); }, testContext)) @@ -1996,7 +1950,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -2005,7 +1958,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(TestApp.EmptyApp, testContext)) + await using (var server = new TestServer(TestApp.EmptyApp, testContext)) { using (var connection = server.CreateConnection()) { @@ -2020,7 +1973,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -2029,7 +1981,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var request = httpContext.Request; var response = httpContext.Response; @@ -2070,7 +2022,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -2079,7 +2030,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var request = httpContext.Request; var stream = await httpContext.Features.Get().UpgradeAsync(); @@ -2117,7 +2068,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", "hello, world"); } - await server.StopAsync(); } } @@ -2128,7 +2078,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests bool onStartingCalled = false; - using (var server = new TestServer(httpContext => + await using (var server = new TestServer(httpContext => { var response = httpContext.Response; response.OnStarting(_ => @@ -2165,7 +2115,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } Assert.False(onStartingCalled); @@ -2181,7 +2130,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var onStartingException = new Exception(); @@ -2222,7 +2171,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } // The first registered OnStarting callback should have been called, @@ -2238,7 +2186,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var testContext = new TestServiceContext(LoggerFactory); var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = httpContext.Response; response.OnStarting(state1 => @@ -2273,8 +2221,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests await tcs.Task.DefaultTimeout(); } - - await server.StopAsync(); } } @@ -2284,7 +2230,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var testContext = new TestServiceContext(LoggerFactory); var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = httpContext.Response; response.OnCompleted(state1 => @@ -2320,8 +2266,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests await tcs.Task.DefaultTimeout(); } - - await server.StopAsync(); } } @@ -2333,7 +2277,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var onCompletedCalled1 = false; var onCompletedCalled2 = false; - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = httpContext.Response; response.OnCompleted(_ => @@ -2366,7 +2310,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", "Hello World"); } - await server.StopAsync(); } // All OnCompleted callbacks should be called even if they throw. @@ -2382,7 +2325,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests bool onStartingCalled = false; - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = httpContext.Response; response.OnStarting(_ => @@ -2410,7 +2353,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", "Hello World"); } - await server.StopAsync(); } Assert.True(onStartingCalled); @@ -2424,7 +2366,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests bool onStartingCalled = false; - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = httpContext.Response; response.OnStarting(_ => @@ -2452,7 +2394,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", "Hello"); } - await server.StopAsync(); } Assert.True(onStartingCalled); @@ -2465,7 +2406,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = httpContext.Response; response.Headers["Content-Length"] = new[] { "11" }; @@ -2486,7 +2427,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", "Hello World"); } - await server.StopAsync(); } Assert.Empty(TestApplicationErrorLogger.Messages.Where(message => message.LogLevel == LogLevel.Error)); @@ -2497,7 +2437,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(httpContext => + await using (var server = new TestServer(httpContext => { httpContext.Abort(); return Task.CompletedTask; @@ -2512,7 +2452,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests ""); await connection.ReceiveEnd(); } - await server.StopAsync(); } } @@ -2521,7 +2460,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(httpContext => + await using (var server = new TestServer(httpContext => { httpContext.Abort(); return Task.CompletedTask; @@ -2536,7 +2475,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests ""); await connection.ReceiveEnd(); } - await server.StopAsync(); } Assert.Single(TestApplicationErrorLogger.Messages.Where(m => m.Message.Contains(CoreStrings.ConnectionAbortedByApplication))); @@ -2551,7 +2489,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests Scheduler = PipeScheduler.Inline }; - using (var server = new TestServer(httpContext => + await using (var server = new TestServer(httpContext => { httpContext.Features.Get().Abort(); return Task.CompletedTask; @@ -2566,7 +2504,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests ""); await connection.ReceiveEnd(); } - await server.StopAsync(); } Assert.Single(TestApplicationErrorLogger.Messages.Where(m => m.Message.Contains("The connection was aborted by the application via IConnectionLifetimeFeature.Abort()."))); @@ -2580,7 +2517,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests IHeaderDictionary originalResponseHeaders = null; var firstRequest = true; - using (var server = new TestServer(httpContext => + await using (var server = new TestServer(httpContext => { var responseFeature = httpContext.Features.Get(); @@ -2619,7 +2556,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -2628,7 +2564,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { await httpContext.Response.StartAsync(); }, testContext)) @@ -2649,7 +2585,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -2658,7 +2593,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { httpContext.Response.ContentLength = 0; await httpContext.Response.StartAsync(); @@ -2678,7 +2613,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "Content-Length: 0", ""); } - await server.StopAsync(); } } @@ -2687,7 +2621,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { await httpContext.Response.StartAsync(); await httpContext.Response.WriteAsync(""); @@ -2709,7 +2643,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -2718,7 +2651,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { await httpContext.Response.StartAsync(); await httpContext.Response.WriteAsync("Hello World!"); @@ -2742,7 +2675,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -2751,7 +2683,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { await httpContext.Response.StartAsync(); Assert.True(httpContext.Response.HasStarted); @@ -2773,7 +2705,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -2782,7 +2713,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); var expectedException = new Exception(); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { await httpContext.Response.StartAsync(); throw expectedException; @@ -2802,7 +2733,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -2811,7 +2741,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); var expectedException = new Exception(); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { httpContext.Response.ContentLength = 11; await httpContext.Response.StartAsync(); @@ -2832,7 +2762,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -2843,7 +2772,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { await httpContext.Response.StartAsync(); Assert.True(httpContext.Response.HasStarted); @@ -2871,7 +2800,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests // If we reach this point before the app exits, this means the flush finished early. tcs.SetResult(null); } - await server.StopAsync(); } } @@ -2880,7 +2808,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { httpContext.Response.Headers["Content-Length"] = new[] { "11" }; await httpContext.Response.StartAsync(); @@ -2902,7 +2830,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", "Hello World"); } - await server.StopAsync(); } } @@ -2912,7 +2839,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var testContext = new TestServiceContext(LoggerFactory); var expectedLength = 100000; var expectedString = new string('a', expectedLength); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { httpContext.Response.Headers["Content-Length"] = new[] { expectedLength.ToString() }; await httpContext.Response.WriteAsync(expectedString); @@ -2933,7 +2860,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", expectedString); } - await server.StopAsync(); } } @@ -2942,7 +2868,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { await httpContext.Response.StartAsync(); await httpContext.Response.BodyWriter.FlushAsync(); @@ -2965,7 +2891,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -2979,7 +2904,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var callOrder = new Stack(); var onStartingTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { context.Response.OnStarting(_ => { @@ -3014,7 +2939,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests // Wait for all callbacks to be called. await onStartingTcs.Task.DefaultTimeout(); } - await server.StopAsync(); } Assert.Equal(1, callOrder.Pop()); @@ -3031,7 +2955,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var callOrder = new Stack(); var onCompletedTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { context.Response.OnCompleted(_ => { @@ -3066,7 +2990,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests // Wait for all callbacks to be called. await onCompletedTcs.Task.DefaultTimeout(); } - await server.StopAsync(); } Assert.Equal(1, callOrder.Pop()); @@ -3076,7 +2999,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests [Fact] public async Task SynchronousWritesDisallowedByDefault() { - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { var bodyControlFeature = context.Features.Get(); Assert.False(bodyControlFeature.AllowSynchronousIO); @@ -3106,7 +3029,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests [Fact] public async Task SynchronousWritesAllowedByOptIn() { - using (var server = new TestServer(context => + await using (var server = new TestServer(context => { var bodyControlFeature = context.Features.Get(); Assert.False(bodyControlFeature.AllowSynchronousIO); @@ -3137,7 +3060,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests ServerOptions = { AllowSynchronousIO = true } }; - using (var server = new TestServer(context => + await using (var server = new TestServer(context => { var bodyControlFeature = context.Features.Get(); Assert.True(bodyControlFeature.AllowSynchronousIO); @@ -3161,7 +3084,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", "Hello!"); } - await server.StopAsync(); } } @@ -3173,7 +3095,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests ServerOptions = { AllowSynchronousIO = false } }; - using (var server = new TestServer(context => + await using (var server = new TestServer(context => { var bodyControlFeature = context.Features.Get(); Assert.False(bodyControlFeature.AllowSynchronousIO); @@ -3201,14 +3123,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", "Hello!"); } - await server.StopAsync(); } } [Fact] public async Task NonZeroContentLengthFor304StatusCodeIsAllowed() { - using (var server = new TestServer(httpContext => + await using (var server = new TestServer(httpContext => { var response = httpContext.Response; response.StatusCode = StatusCodes.Status304NotModified; @@ -3231,7 +3152,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -3240,7 +3160,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = httpContext.Response; @@ -3265,8 +3185,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - - await server.StopAsync(); } } @@ -3275,7 +3193,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(httpContext => + await using (var server = new TestServer(httpContext => { var response = httpContext.Response; @@ -3297,8 +3215,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - - await server.StopAsync(); } } @@ -3307,7 +3223,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(httpContext => + await using (var server = new TestServer(httpContext => { var response = httpContext.Response; @@ -3331,8 +3247,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - - await server.StopAsync(); } } @@ -3341,7 +3255,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(httpContext => + await using (var server = new TestServer(httpContext => { var response = httpContext.Response; response.ContentLength = 12; @@ -3381,7 +3295,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var testContext = new TestServiceContext(LoggerFactory); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = httpContext.Response; response.ContentLength = 12; @@ -3412,15 +3326,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", "Hello World!"); } - - await server.StopAsync(); } } [Fact] public async Task ResponseBodyCanWrite() { - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { httpContext.Response.ContentLength = 12; await httpContext.Response.Body.WriteAsync(Encoding.ASCII.GetBytes("hello, world")); @@ -3440,14 +3352,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", "hello, world"); } - await server.StopAsync(); } } [Fact] public async Task ResponseBodyAndResponsePipeWorks() { - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var response = httpContext.Response; response.ContentLength = 54; @@ -3483,14 +3394,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "hello, world", "hello, world"); } - await server.StopAsync(); } } [Fact] public async Task ResponseBodyWriterCompleteWithoutExceptionDoesNotThrow() { - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { httpContext.Response.BodyWriter.Complete(); await Task.CompletedTask; @@ -3510,14 +3420,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } [Fact] public async Task ResponseBodyWriterCompleteWithoutExceptionWritesDoNotThrow() { - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { httpContext.Response.BodyWriter.Complete(); await httpContext.Response.WriteAsync("test"); @@ -3539,7 +3448,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -3547,7 +3455,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests public async Task ResponseAdvanceStateIsResetWithMultipleReqeusts() { var secondRequest = false; - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { if (secondRequest) { @@ -3592,14 +3500,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } [Fact] public async Task ResponseStartCalledAndAutoChunkStateIsResetWithMultipleReqeusts() { - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var memory = httpContext.Response.BodyWriter.GetMemory(); Encoding.ASCII.GetBytes("a").CopyTo(memory); @@ -3642,7 +3549,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -3650,7 +3556,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests public async Task ResponseStartCalledStateIsResetWithMultipleReqeusts() { var flip = false; - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { if (flip) { @@ -3704,7 +3610,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "a"); } } - await server.StopAsync(); } } @@ -3712,7 +3617,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests public async Task ResponseIsLeasedMemoryInvalidStateIsResetWithMultipleReqeusts() { var secondRequest = false; - using (var server = new TestServer(httpContext => + await using (var server = new TestServer(httpContext => { if (secondRequest) { @@ -3750,7 +3655,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -3758,7 +3662,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests public async Task ResponsePipeWriterCompleteWithException() { var expectedException = new Exception(); - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { httpContext.Response.BodyWriter.Complete(expectedException); await Task.CompletedTask; @@ -3780,14 +3684,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests Assert.Contains(TestSink.Writes, w => w.EventId.Id == 13 && w.LogLevel == LogLevel.Error && w.Exception is ConnectionAbortedException && w.Exception.InnerException == expectedException); } - await server.StopAsync(); } } [Fact] public async Task ResponseCompleteGetMemoryReturnsRentedMemory() { - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { await httpContext.Response.StartAsync(); httpContext.Response.BodyWriter.Complete(); @@ -3813,14 +3716,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } [Fact] public async Task ResponseCompleteGetMemoryReturnsRentedMemoryWithoutStartAsync() { - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { httpContext.Response.BodyWriter.Complete(); var memory = httpContext.Response.BodyWriter.GetMemory(); // Shouldn't throw @@ -3843,14 +3745,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } [Fact] public async Task ResponseGetMemoryAndStartAsyncMemoryReturnsNewMemory() { - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var memory = httpContext.Response.BodyWriter.GetMemory(); Assert.Equal(4096, memory.Length); @@ -3879,7 +3780,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -3887,7 +3787,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests [Fact] public async Task ResponseGetMemoryAndStartAsyncAdvanceThrows() { - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var memory = httpContext.Response.BodyWriter.GetMemory(); @@ -3913,14 +3813,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } [Fact] public async Task ResponseCompleteGetMemoryAdvanceInLoopDoesNotThrow() { - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { httpContext.Response.BodyWriter.Complete(); @@ -3949,14 +3848,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } [Fact] public async Task ResponseSetBodyAndPipeBodyIsWrapped() { - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { httpContext.Response.Body = new MemoryStream(); httpContext.Response.BodyWriter = new Pipe().Writer; @@ -3979,14 +3877,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } [Fact] public async Task ResponseSetBodyToSameValueTwiceGetPipeMultipleTimesDifferentObject() { - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var memoryStream = new MemoryStream(); httpContext.Response.Body = memoryStream; @@ -4013,14 +3910,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } [Fact] public async Task ResponseSetPipeToSameValueTwiceGetBodyMultipleTimesDifferent() { - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var pipeWriter = new Pipe().Writer; httpContext.Response.BodyWriter = pipeWriter; @@ -4047,14 +3943,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } [Fact] public async Task ResponseSetPipeAndBodyWriterIsWrapped() { - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { httpContext.Response.BodyWriter = new Pipe().Writer; httpContext.Response.Body = new MemoryStream(); @@ -4078,14 +3973,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } [Fact] public async Task ResponseWriteToBodyWriterAndStreamAllBlocksDisposed() { - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { for (var i = 0; i < 3; i++) { @@ -4114,14 +4008,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } [Fact] public async Task ResponseStreamWrappingWorks() { - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var oldBody = httpContext.Response.Body; httpContext.Response.Body = new MemoryStream(); @@ -4153,14 +4046,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } [Fact] public async Task ResponsePipeWrappingWorks() { - using (var server = new TestServer(async httpContext => + await using (var server = new TestServer(async httpContext => { var oldPipeWriter = httpContext.Response.BodyWriter; var pipe = new Pipe(); @@ -4193,7 +4085,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -4216,7 +4107,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests disposedTcs.TrySetResult(c.Response.StatusCode); }); - using (var server = new TestServer(handler, new TestServiceContext(loggerFactory), + await using (var server = new TestServer(handler, new TestServiceContext(loggerFactory), options => options.ListenOptions.Add(new ListenOptions(new IPEndPoint(IPAddress.Loopback, 0))), services => services.AddSingleton(mockHttpContextFactory.Object))) { @@ -4280,7 +4171,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var disposedStatusCode = await disposedTcs.Task.DefaultTimeout(); Assert.Equal(expectedServerStatusCode, (HttpStatusCode)disposedStatusCode); - await server.StopAsync(); } if (sendMalformedRequest) diff --git a/src/Servers/Kestrel/test/InMemory.FunctionalTests/TestTransport/TestServer.cs b/src/Servers/Kestrel/test/InMemory.FunctionalTests/TestTransport/TestServer.cs index b71f2ba490..9f3828f07b 100644 --- a/src/Servers/Kestrel/test/InMemory.FunctionalTests/TestTransport/TestServer.cs +++ b/src/Servers/Kestrel/test/InMemory.FunctionalTests/TestTransport/TestServer.cs @@ -13,7 +13,6 @@ using Microsoft.AspNetCore.Hosting.Server; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.AspNetCore.Testing; -using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Xunit; @@ -22,7 +21,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests.TestTrans /// /// In-memory TestServer /// _memoryPool; private readonly RequestDelegate _app; @@ -158,5 +157,12 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests.TestTrans token.Register(() => tcs.SetResult(null)); return tcs.Task; } + + public async ValueTask DisposeAsync() + { + // The concrete WebHost implements IAsyncDisposable + await ((IAsyncDisposable)_host).ConfigureAwait(false).DisposeAsync(); + _memoryPool.Dispose(); + } } } diff --git a/src/Servers/Kestrel/test/InMemory.FunctionalTests/UpgradeTests.cs b/src/Servers/Kestrel/test/InMemory.FunctionalTests/UpgradeTests.cs index f36d4e84a1..cf36bb8a15 100644 --- a/src/Servers/Kestrel/test/InMemory.FunctionalTests/UpgradeTests.cs +++ b/src/Servers/Kestrel/test/InMemory.FunctionalTests/UpgradeTests.cs @@ -21,7 +21,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests public async Task ResponseThrowsAfterUpgrade() { var upgrade = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { var feature = context.Features.Get(); var stream = await feature.UpgradeAsync(); @@ -51,7 +51,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests await connection.Receive("New protocol data"); await upgrade.Task.DefaultTimeout(); } - await server.StopAsync(); } } @@ -62,7 +61,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests const string recv = "Custom protocol recv"; var upgrade = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { try { @@ -107,7 +106,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests await upgrade.Task.DefaultTimeout(); } - await server.StopAsync(); } } @@ -115,7 +113,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests public async Task UpgradeCannotBeCalledMultipleTimes() { var upgradeTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { var feature = context.Features.Get(); await feature.UpgradeAsync(); @@ -146,7 +144,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests ""); await connection.WaitForConnectionClose(); } - await server.StopAsync(); } var ex = await Assert.ThrowsAsync(async () => await upgradeTcs.Task.DefaultTimeout()); @@ -156,7 +153,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests [Fact] public async Task RejectsRequestWithContentLengthAndUpgrade() { - using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory))) + await using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory))) { using (var connection = server.CreateConnection()) { @@ -175,14 +172,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } [Fact] public async Task AcceptsRequestWithNoContentLengthAndUpgrade() { - using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory))) + await using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory))) { using (var connection = server.CreateConnection()) { @@ -200,14 +196,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests await connection.SendEmptyGetWithUpgrade(); await connection.Receive("HTTP/1.1 200 OK"); } - await server.StopAsync(); } } [Fact] public async Task RejectsRequestWithChunkedEncodingAndUpgrade() { - using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory))) + await using (var server = new TestServer(context => Task.CompletedTask, new TestServiceContext(LoggerFactory))) { using (var connection = server.CreateConnection()) { @@ -225,7 +220,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "", ""); } - await server.StopAsync(); } } @@ -233,7 +227,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests public async Task ThrowsWhenUpgradingNonUpgradableRequest() { var upgradeTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { var feature = context.Features.Get(); Assert.False(feature.IsUpgradableRequest); @@ -256,7 +250,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests await connection.SendEmptyGet(); await connection.Receive("HTTP/1.1 200 OK"); } - await server.StopAsync(); } var ex = await Assert.ThrowsAsync(async () => await upgradeTcs.Task).DefaultTimeout(); @@ -271,7 +264,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests var serviceContext = new TestServiceContext(LoggerFactory); serviceContext.ConnectionManager = new ConnectionManager(serviceContext.Log, ResourceCounter.Quota(limit)); - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { var feature = context.Features.Get(); if (feature.IsUpgradableRequest) @@ -308,7 +301,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests await connection.Receive("HTTP/1.1 200"); } } - await server.StopAsync(); } var exception = await Assert.ThrowsAsync(async () => await upgradeTcs.Task.TimeoutAfter(TimeSpan.FromSeconds(60))); @@ -320,7 +312,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { var appCompletedTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - using (var server = new TestServer(async context => + await using (var server = new TestServer(async context => { var feature = context.Features.Get(); var duplexStream = await feature.UpgradeAsync(); @@ -349,7 +341,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests } await appCompletedTcs.Task.DefaultTimeout(); - await server.StopAsync(); } } }