Fix flaky test: ConnectionResetMidRequestIsLoggedAsDebug (#1933)

This commit is contained in:
Nate McMaster 2017-07-03 11:37:23 -07:00 committed by GitHub
parent 00d17dea79
commit eea0c6490e
4 changed files with 17 additions and 12 deletions

View File

@ -12,7 +12,6 @@
Remove when fixed. Remove when fixed.
--> -->
<HasRuntimeOutput>true</HasRuntimeOutput> <HasRuntimeOutput>true</HasRuntimeOutput>
<RuntimeIdentifier Condition="'$(TargetFramework)' != 'netcoreapp2.0'">win7-x64</RuntimeIdentifier>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -12,7 +12,6 @@
Remove when fixed. Remove when fixed.
--> -->
<HasRuntimeOutput>true</HasRuntimeOutput> <HasRuntimeOutput>true</HasRuntimeOutput>
<RuntimeIdentifier Condition="'$(TargetFramework)' != 'netcoreapp2.0'">win7-x64</RuntimeIdentifier>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -28,6 +28,7 @@ using Moq;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using Xunit; using Xunit;
using Xunit.Abstractions;
namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
{ {
@ -37,6 +38,13 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
private const int _connectionResetEventId = 19; private const int _connectionResetEventId = 19;
private const int _semaphoreWaitTimeout = 2500; private const int _semaphoreWaitTimeout = 2500;
private readonly ITestOutputHelper _output;
public RequestTests(ITestOutputHelper output)
{
_output = output;
}
public static TheoryData<ListenOptions> ConnectionAdapterData => new TheoryData<ListenOptions> public static TheoryData<ListenOptions> ConnectionAdapterData => new TheoryData<ListenOptions>
{ {
new ListenOptions(new IPEndPoint(IPAddress.Loopback, 0)), new ListenOptions(new IPEndPoint(IPAddress.Loopback, 0)),
@ -385,6 +393,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
{ {
var requestStarted = new SemaphoreSlim(0); var requestStarted = new SemaphoreSlim(0);
var connectionReset = new SemaphoreSlim(0); var connectionReset = new SemaphoreSlim(0);
var connectionClosing = new SemaphoreSlim(0);
var loggedHigherThanDebug = false; var loggedHigherThanDebug = false;
var mockLogger = new Mock<ILogger>(); var mockLogger = new Mock<ILogger>();
@ -395,6 +404,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
.Setup(logger => logger.Log(It.IsAny<LogLevel>(), It.IsAny<EventId>(), It.IsAny<object>(), It.IsAny<Exception>(), It.IsAny<Func<object, Exception, string>>())) .Setup(logger => logger.Log(It.IsAny<LogLevel>(), It.IsAny<EventId>(), It.IsAny<object>(), It.IsAny<Exception>(), It.IsAny<Func<object, Exception, string>>()))
.Callback<LogLevel, EventId, object, Exception, Func<object, Exception, string>>((logLevel, eventId, state, exception, formatter) => .Callback<LogLevel, EventId, object, Exception, Func<object, Exception, string>>((logLevel, eventId, state, exception, formatter) =>
{ {
_output.WriteLine(logLevel + ": " + formatter(state, exception));
if (eventId.Id == _connectionResetEventId) if (eventId.Id == _connectionResetEventId)
{ {
connectionReset.Release(); connectionReset.Release();
@ -417,20 +428,16 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
using (var server = new TestServer(async context => using (var server = new TestServer(async context =>
{ {
requestStarted.Release(); requestStarted.Release();
await context.Request.Body.ReadAsync(new byte[1], 0, 1); await connectionClosing.WaitAsync();
}, },
new TestServiceContext(mockLoggerFactory.Object))) new TestServiceContext(mockLoggerFactory.Object)))
{ {
using (var connection = server.CreateConnection()) using (var connection = server.CreateConnection())
{ {
await connection.Send( await connection.SendEmptyGet();
"GET / HTTP/1.1",
"Host:",
"",
"");
// Wait until connection is established // Wait until connection is established
Assert.True(await requestStarted.WaitAsync(TimeSpan.FromSeconds(10))); Assert.True(await requestStarted.WaitAsync(TimeSpan.FromSeconds(30)), "request should have started");
// Force a reset // Force a reset
connection.Socket.LingerState = new LingerOption(true, 0); connection.Socket.LingerState = new LingerOption(true, 0);
@ -440,10 +447,11 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
// This check MUST come before disposing the server, otherwise there's a race where the RST // This check MUST come before disposing the server, otherwise there's a race where the RST
// is still in flight when the connection is aborted, leading to the reset never being received // is still in flight when the connection is aborted, leading to the reset never being received
// and therefore not logged. // and therefore not logged.
Assert.True(await connectionReset.WaitAsync(TimeSpan.FromSeconds(10))); Assert.True(await connectionReset.WaitAsync(TimeSpan.FromSeconds(30)), "Connection reset event should have been logged");
connectionClosing.Release();
} }
Assert.False(loggedHigherThanDebug); Assert.False(loggedHigherThanDebug, "Logged event should not have been higher than debug.");
} }
[Fact] [Fact]

View File

@ -13,7 +13,6 @@
Remove when fixed. Remove when fixed.
--> -->
<HasRuntimeOutput>true</HasRuntimeOutput> <HasRuntimeOutput>true</HasRuntimeOutput>
<RuntimeIdentifier Condition="'$(TargetFramework)' != 'netcoreapp2.0'">win7-x64</RuntimeIdentifier>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>