Allow the server to forcefully close socket it tests with connection failures

- This is a delayed reaction to 54caf30 which causes connections closed from
  the server to be closed less gracefully due to no longer waiting for a FIN
  from the client
This commit is contained in:
Stephen Halter 2016-03-04 14:47:35 -08:00
parent 0cffed76df
commit b55bef20aa
4 changed files with 28 additions and 9 deletions

View File

@ -373,7 +373,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
"HTTP/1.1 400 Bad Request",
"");
await connection.ReceiveStartsWith("Date:");
await connection.ReceiveEnd(
await connection.ReceiveForcedEnd(
"Content-Length: 0",
"Server: Kestrel",
"",
@ -417,7 +417,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
"HTTP/1.1 400 Bad Request",
"");
await connection.ReceiveStartsWith("Date:");
await connection.ReceiveEnd(
await connection.ReceiveForcedEnd(
"Content-Length: 0",
"Server: Kestrel",
"",

View File

@ -122,7 +122,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
"GET / HTTP/1.1",
"",
"");
await connection.ReceiveEnd(
await connection.ReceiveForcedEnd(
"HTTP/1.1 200 OK",
"Transfer-Encoding: chunked",
"",
@ -153,7 +153,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
"");
// Headers are sent before connection is closed, but chunked body terminator isn't sent
await connection.ReceiveEnd(
await connection.ReceiveForcedEnd(
"HTTP/1.1 200 OK",
"Transfer-Encoding: chunked",
"",

View File

@ -263,7 +263,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
{
using (var connection = new TestConnection(server.Port))
{
await connection.Send(
await connection.SendEnd(
"POST / HTTP/1.0",
"Content-Length: 11",
"",
@ -638,7 +638,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
"GET / HTTP/1.1",
"",
"");
await connection.ReceiveEnd(
await connection.ReceiveForcedEnd(
"HTTP/1.1 200 OK",
"Content-Length: 11",
"",
@ -680,7 +680,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
"GET / HTTP/1.1",
"",
"");
await connection.ReceiveEnd(
await connection.ReceiveForcedEnd(
"HTTP/1.1 200 OK",
"Content-Length: 11",
"",
@ -870,7 +870,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
"GET / HTTP/1.1",
"",
"");
await connection.ReceiveEnd(
await connection.ReceiveForcedEnd(
"HTTP/1.1 200 OK",
"Content-Length: 11",
"",
@ -941,7 +941,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
"",
"");
await connection.ReceiveEnd(
await connection.ReceiveForcedEnd(
"HTTP/1.1 200 OK",
"Content-Length: 5",
"",

View File

@ -126,6 +126,25 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
Assert.Equal("", text);
}
public async Task ReceiveForcedEnd(params string[] lines)
{
await Receive(lines);
try
{
var ch = new char[128];
var count = await _reader.ReadAsync(ch, 0, 128);
var text = new string(ch, 0, count);
Assert.Equal("", text);
}
catch (IOException)
{
// The server is forcefully closing the connection so an IOException:
// "Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host."
// isn't guaranteed but not unexpected.
}
}
public static Socket CreateConnectedLoopbackSocket(int port)
{
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);