Fix flakiness in WhenAppWritesLessThanContentLengthButRequestIsAbortedErrorNotLogged.

This commit is contained in:
Cesar Blum Silveira 2017-03-27 10:10:28 -07:00 committed by GitHub
parent 47f1db20e0
commit db7348e776
1 changed files with 12 additions and 8 deletions

View File

@ -684,20 +684,21 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
[Fact] [Fact]
public async Task WhenAppWritesLessThanContentLengthButRequestIsAbortedErrorNotLogged() public async Task WhenAppWritesLessThanContentLengthButRequestIsAbortedErrorNotLogged()
{ {
var abortTcs = new TaskCompletionSource<object>(); var requestAborted = new SemaphoreSlim(0);
var mockTrace = new Mock<IKestrelTrace>(); var mockTrace = new Mock<IKestrelTrace>();
using (var server = new TestServer(async httpContext => using (var server = new TestServer(async httpContext =>
{ {
httpContext.RequestAborted.Register(() => httpContext.RequestAborted.Register(() =>
{ {
abortTcs.SetResult(null); requestAborted.Release(2);
}); });
httpContext.Response.ContentLength = 12; httpContext.Response.ContentLength = 12;
await httpContext.Response.WriteAsync("hello,"); await httpContext.Response.WriteAsync("hello,");
// Wait until the request is aborted so we know Frame will skip the response content length check. // Wait until the request is aborted so we know Frame will skip the response content length check.
await abortTcs.Task.TimeoutAfter(TimeSpan.FromSeconds(10)); await requestAborted.WaitAsync(TimeSpan.FromSeconds(10));
}, new TestServiceContext { Log = mockTrace.Object })) }, new TestServiceContext { Log = mockTrace.Object }))
{ {
using (var connection = server.CreateConnection()) using (var connection = server.CreateConnection())
@ -713,12 +714,15 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
"", "",
"hello,"); "hello,");
} }
}
// Verify the request was really aborted. A timeout in the await inside // Verify the request was really aborted. A timeout in
// the app func would cause a server error and skip the content length // the app would cause a server error and skip the content length
// check altogether, making the test pass for the wrong reason. // check altogether, making the test pass for the wrong reason.
Assert.True(abortTcs.Task.IsCompleted); // Await before disposing the server to prevent races between the
// abort triggered by the connection RST and the abort called when
// disposing the server.
await requestAborted.WaitAsync(TimeSpan.FromSeconds(10));
}
// With the server disposed we know all connections were drained and all messages were logged. // With the server disposed we know all connections were drained and all messages were logged.
mockTrace.Verify(trace => trace.ApplicationError(It.IsAny<string>(), It.IsAny<InvalidOperationException>()), Times.Never); mockTrace.Verify(trace => trace.ApplicationError(It.IsAny<string>(), It.IsAny<InvalidOperationException>()), Times.Never);