Fix test races caused by cedbe76.

This commit is contained in:
Cesar Blum Silveira 2016-12-13 12:55:15 -08:00
parent cedbe76f52
commit 4485bfec39
2 changed files with 25 additions and 7 deletions

View File

@ -590,14 +590,22 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
[Fact] [Fact]
public async Task WhenAppWritesLessThanContentLengthErrorLogged() public async Task WhenAppWritesLessThanContentLengthErrorLogged()
{ {
var testLogger = new TestApplicationErrorLogger(); string errorMessage = null;
var serviceContext = new TestServiceContext { Log = new TestKestrelTrace(testLogger) }; var logTcs = new TaskCompletionSource<object>();
var mockTrace = new Mock<IKestrelTrace>();
mockTrace
.Setup(trace => trace.ApplicationError(It.IsAny<string>(), It.IsAny<InvalidOperationException>()))
.Callback<string, Exception>((connectionId, ex) =>
{
errorMessage = ex.Message;
logTcs.SetResult(null);
});
using (var server = new TestServer(async httpContext => using (var server = new TestServer(async httpContext =>
{ {
httpContext.Response.ContentLength = 13; httpContext.Response.ContentLength = 13;
await httpContext.Response.WriteAsync("hello, world"); await httpContext.Response.WriteAsync("hello, world");
}, serviceContext)) }, new TestServiceContext { Log = mockTrace.Object }))
{ {
using (var connection = server.CreateConnection()) using (var connection = server.CreateConnection())
{ {
@ -611,13 +619,15 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
"Content-Length: 13", "Content-Length: 13",
"", "",
"hello, world"); "hello, world");
// Wait for error message to be logged.
await logTcs.Task.TimeoutAfter(TimeSpan.FromSeconds(10));
} }
} }
var errorMessage = Assert.Single(testLogger.Messages, message => message.LogLevel == LogLevel.Error);
Assert.Equal( Assert.Equal(
$"Response Content-Length mismatch: too few bytes written (12 of 13).", $"Response Content-Length mismatch: too few bytes written (12 of 13).",
errorMessage.Exception.Message); errorMessage);
} }
[Fact] [Fact]
@ -909,7 +919,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
"POST / HTTP/1.1", "POST / HTTP/1.1",
"Transfer-Encoding: chunked", "Transfer-Encoding: chunked",
"", "",
"wrong"); "gg");
await responseWritten.WaitAsync(); await responseWritten.WaitAsync();
await connection.ReceiveEnd( await connection.ReceiveEnd(
"HTTP/1.1 400 Bad Request", "HTTP/1.1 400 Bad Request",

View File

@ -1249,12 +1249,14 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
const string response = "hello, world"; const string response = "hello, world";
var callOrder = new Stack<int>(); var callOrder = new Stack<int>();
var onStartingTcs = new TaskCompletionSource<object>();
using (var server = new TestServer(async context => using (var server = new TestServer(async context =>
{ {
context.Response.OnStarting(_ => context.Response.OnStarting(_ =>
{ {
callOrder.Push(1); callOrder.Push(1);
onStartingTcs.SetResult(null);
return TaskCache.CompletedTask; return TaskCache.CompletedTask;
}, null); }, null);
context.Response.OnStarting(_ => context.Response.OnStarting(_ =>
@ -1280,7 +1282,8 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
"", "",
"hello, world"); "hello, world");
// Wait for all callbacks to be called.
await onStartingTcs.Task.TimeoutAfter(TimeSpan.FromSeconds(10));
} }
} }
@ -1295,12 +1298,14 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
const string response = "hello, world"; const string response = "hello, world";
var callOrder = new Stack<int>(); var callOrder = new Stack<int>();
var onCompletedTcs = new TaskCompletionSource<object>();
using (var server = new TestServer(async context => using (var server = new TestServer(async context =>
{ {
context.Response.OnCompleted(_ => context.Response.OnCompleted(_ =>
{ {
callOrder.Push(1); callOrder.Push(1);
onCompletedTcs.SetResult(null);
return TaskCache.CompletedTask; return TaskCache.CompletedTask;
}, null); }, null);
context.Response.OnCompleted(_ => context.Response.OnCompleted(_ =>
@ -1325,6 +1330,9 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
$"Content-Length: {response.Length}", $"Content-Length: {response.Length}",
"", "",
"hello, world"); "hello, world");
// Wait for all callbacks to be called.
await onCompletedTcs.Task.TimeoutAfter(TimeSpan.FromSeconds(10));
} }
} }