diff --git a/src/Servers/Kestrel/test/InMemory.FunctionalTests/RequestTests.cs b/src/Servers/Kestrel/test/InMemory.FunctionalTests/RequestTests.cs index c00978bc3b..10ad43f710 100644 --- a/src/Servers/Kestrel/test/InMemory.FunctionalTests/RequestTests.cs +++ b/src/Servers/Kestrel/test/InMemory.FunctionalTests/RequestTests.cs @@ -60,6 +60,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests public async Task RequestBodyReadAsyncCanBeCancelled() { var helloTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var readTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var cts = new CancellationTokenSource(); using (var server = new TestServer(async context => @@ -81,7 +82,9 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests try { - await context.Request.Body.ReadAsync(buffer, 0, 1, cts.Token).DefaultTimeout(); + var task = context.Request.Body.ReadAsync(buffer, 0, buffer.Length, cts.Token); + readTcs.TrySetResult(null); + await task; context.Response.ContentLength = 12; await context.Response.WriteAsync("Read success"); @@ -107,12 +110,11 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests await connection.Send("Hello "); await helloTcs.Task; + await readTcs.Task; // Cancel the body after hello is read cts.Cancel(); - await connection.Send("World"); - await connection.Receive($"HTTP/1.1 200 OK", $"Date: {server.Context.DateHeaderValue}", "Content-Length: 14", diff --git a/src/Servers/Kestrel/test/InMemory.FunctionalTests/ResponseTests.cs b/src/Servers/Kestrel/test/InMemory.FunctionalTests/ResponseTests.cs index 4453d46a5e..779fc1ac40 100644 --- a/src/Servers/Kestrel/test/InMemory.FunctionalTests/ResponseTests.cs +++ b/src/Servers/Kestrel/test/InMemory.FunctionalTests/ResponseTests.cs @@ -104,10 +104,9 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests public async Task ResponseBodyWriteAsyncCanBeCancelled() { var serviceContext = new TestServiceContext(LoggerFactory); - serviceContext.ServerOptions.Limits.MaxResponseBufferSize = 5; var cts = new CancellationTokenSource(); var appTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); - var writeStartedTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var writeBlockedTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); using (var server = new TestServer(async context => { @@ -115,21 +114,32 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests { await context.Response.WriteAsync("hello", cts.Token).DefaultTimeout(); - var task = context.Response.WriteAsync("world", cts.Token); - Assert.False(task.IsCompleted); + var data = new byte[1024 * 1024 * 10]; - writeStartedTcs.TrySetResult(null); + var timerTask = Task.Delay(TimeSpan.FromSeconds(1)); + var writeTask = context.Response.Body.WriteAsync(data, 0, data.Length, cts.Token).DefaultTimeout(); + var completedTask = await Task.WhenAny(writeTask, timerTask); - await task.DefaultTimeout(); + while (completedTask == writeTask) + { + await writeTask; + timerTask = Task.Delay(TimeSpan.FromSeconds(1)); + writeTask = context.Response.Body.WriteAsync(data, 0, data.Length, cts.Token).DefaultTimeout(); + completedTask = await Task.WhenAny(writeTask, timerTask); + } + + writeBlockedTcs.TrySetResult(null); + + await writeTask; } catch (Exception ex) { appTcs.TrySetException(ex); + writeBlockedTcs.TrySetException(ex); } finally { appTcs.TrySetResult(null); - writeStartedTcs.TrySetCanceled(); } }, serviceContext)) { @@ -148,7 +158,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.InMemory.FunctionalTests "5", "hello"); - await writeStartedTcs.Task.DefaultTimeout(); + await writeBlockedTcs.Task.DefaultTimeout(); cts.Cancel(); diff --git a/src/Servers/Kestrel/test/Libuv.FunctionalTests/Libuv.FunctionalTests.csproj b/src/Servers/Kestrel/test/Libuv.FunctionalTests/Libuv.FunctionalTests.csproj index 0e86764f26..7d2b01c3c8 100644 --- a/src/Servers/Kestrel/test/Libuv.FunctionalTests/Libuv.FunctionalTests.csproj +++ b/src/Servers/Kestrel/test/Libuv.FunctionalTests/Libuv.FunctionalTests.csproj @@ -21,6 +21,7 @@ + diff --git a/src/Servers/Kestrel/test/Sockets.FunctionalTests/Sockets.FunctionalTests.csproj b/src/Servers/Kestrel/test/Sockets.FunctionalTests/Sockets.FunctionalTests.csproj index be7f4001ee..a4b16ed9fb 100644 --- a/src/Servers/Kestrel/test/Sockets.FunctionalTests/Sockets.FunctionalTests.csproj +++ b/src/Servers/Kestrel/test/Sockets.FunctionalTests/Sockets.FunctionalTests.csproj @@ -19,6 +19,7 @@ +