Wait for connection in RequestTests.ConnectionResetAbortsRequest (#978).

This commit is contained in:
Cesar Blum Silveira 2016-07-17 14:51:04 -07:00
parent 41e50ba688
commit bdf9f8dd4e
1 changed files with 21 additions and 9 deletions

View File

@ -5,7 +5,6 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using System.Linq;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Net.Sockets; using System.Net.Sockets;
@ -16,7 +15,6 @@ using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Server.Kestrel.Internal.Networking; using Microsoft.AspNetCore.Server.Kestrel.Internal.Networking;
using Microsoft.AspNetCore.Testing;
using Microsoft.AspNetCore.Testing.xunit; using Microsoft.AspNetCore.Testing.xunit;
using Microsoft.Extensions.Logging.Testing; using Microsoft.Extensions.Logging.Testing;
using Newtonsoft.Json; using Newtonsoft.Json;
@ -27,6 +25,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
{ {
public class RequestTests public class RequestTests
{ {
private const int _semaphoreWaitTimeout = 2500;
[Theory] [Theory]
[InlineData(10 * 1024 * 1024, true)] [InlineData(10 * 1024 * 1024, true)]
// In the following dataset, send at least 2GB. // In the following dataset, send at least 2GB.
@ -193,8 +193,10 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
[Fact] [Fact]
public async Task ConnectionResetAbortsRequest() public async Task ConnectionResetAbortsRequest()
{ {
var connectionStarted = new SemaphoreSlim(0);
var connectionErrorLogged = new SemaphoreSlim(0); var connectionErrorLogged = new SemaphoreSlim(0);
var testSink = new ConnectionErrorTestSink(() => connectionErrorLogged.Release()); var testSink = new ConnectionErrorTestSink(
() => connectionStarted.Release(), () => connectionErrorLogged.Release());
var builder = new WebHostBuilder() var builder = new WebHostBuilder()
.UseLoggerFactory(new TestLoggerFactory(testSink, true)) .UseLoggerFactory(new TestLoggerFactory(testSink, true))
.UseKestrel() .UseKestrel()
@ -211,11 +213,14 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{ {
socket.Connect(new IPEndPoint(IPAddress.Loopback, host.GetPort())); socket.Connect(new IPEndPoint(IPAddress.Loopback, host.GetPort()));
// Wait until connection is established
Assert.True(await connectionStarted.WaitAsync(_semaphoreWaitTimeout));
// Force a reset
socket.LingerState = new LingerOption(true, 0); socket.LingerState = new LingerOption(true, 0);
} }
// Wait until connection error is logged // Wait until connection error is logged
Assert.True(await connectionErrorLogged.WaitAsync(2500)); Assert.True(await connectionErrorLogged.WaitAsync(_semaphoreWaitTimeout));
// Check for expected message // Check for expected message
Assert.NotNull(testSink.ConnectionErrorMessage); Assert.NotNull(testSink.ConnectionErrorMessage);
@ -237,7 +242,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
.Configure(app => app.Run(async context => .Configure(app => app.Run(async context =>
{ {
requestStarted.Release(); requestStarted.Release();
Assert.True(await connectionReset.WaitAsync(2500)); Assert.True(await connectionReset.WaitAsync(_semaphoreWaitTimeout));
try try
{ {
@ -276,12 +281,12 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
socket.Connect(new IPEndPoint(IPAddress.Loopback, host.GetPort())); socket.Connect(new IPEndPoint(IPAddress.Loopback, host.GetPort()));
socket.LingerState = new LingerOption(true, 0); socket.LingerState = new LingerOption(true, 0);
socket.Send(Encoding.ASCII.GetBytes("GET / HTTP/1.1\r\nContent-Length: 1\r\n\r\n")); socket.Send(Encoding.ASCII.GetBytes("GET / HTTP/1.1\r\nContent-Length: 1\r\n\r\n"));
Assert.True(await requestStarted.WaitAsync(2500)); Assert.True(await requestStarted.WaitAsync(_semaphoreWaitTimeout));
} }
connectionReset.Release(); connectionReset.Release();
Assert.True(await appDone.WaitAsync(2500)); Assert.True(await appDone.WaitAsync(_semaphoreWaitTimeout));
Assert.True(expectedExceptionThrown); Assert.True(expectedExceptionThrown);
} }
} }
@ -325,10 +330,12 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
private class ConnectionErrorTestSink : ITestSink private class ConnectionErrorTestSink : ITestSink
{ {
private readonly Action _connectionStarted;
private readonly Action _connectionErrorLogged; private readonly Action _connectionErrorLogged;
public ConnectionErrorTestSink(Action connectionErrorLogged) public ConnectionErrorTestSink(Action connectionStarted, Action connectionErrorLogged)
{ {
_connectionStarted = connectionStarted;
_connectionErrorLogged = connectionErrorLogged; _connectionErrorLogged = connectionErrorLogged;
} }
@ -348,9 +355,14 @@ namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
public void Write(WriteContext context) public void Write(WriteContext context)
{ {
const int connectionStartEventId = 1;
const int connectionErrorEventId = 14; const int connectionErrorEventId = 14;
if (context.EventId.Id == connectionErrorEventId) if (context.EventId.Id == connectionStartEventId)
{
_connectionStarted();
}
else if (context.EventId.Id == connectionErrorEventId)
{ {
ConnectionErrorMessage = context.Exception?.Message; ConnectionErrorMessage = context.Exception?.Message;
_connectionErrorLogged(); _connectionErrorLogged();