From 4e9f8366cfbc5f8154164dcba51b8f2dd3fe57b7 Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Thu, 17 Dec 2015 10:36:59 +0000 Subject: [PATCH] Move stream reuse test to EngineTests --- .../ReuseStreamsTests.cs | 129 ------------------ .../EngineTests.cs | 87 ++++++++++++ 2 files changed, 87 insertions(+), 129 deletions(-) delete mode 100644 test/Microsoft.AspNet.Server.Kestrel.FunctionalTests/ReuseStreamsTests.cs diff --git a/test/Microsoft.AspNet.Server.Kestrel.FunctionalTests/ReuseStreamsTests.cs b/test/Microsoft.AspNet.Server.Kestrel.FunctionalTests/ReuseStreamsTests.cs deleted file mode 100644 index b15c45d67b..0000000000 --- a/test/Microsoft.AspNet.Server.Kestrel.FunctionalTests/ReuseStreamsTests.cs +++ /dev/null @@ -1,129 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Collections.Generic; -using System.IO; -using System.Net.Http; -using System.Threading.Tasks; -using Microsoft.AspNet.Builder; -using Microsoft.AspNet.Hosting; -using Microsoft.AspNet.Http.Features; -using Microsoft.Extensions.Configuration; -using Xunit; - -namespace Microsoft.AspNet.Server.Kestrel.FunctionalTests -{ - public class ReuseStreamsTests - { - [Fact] - public async Task ReuseStreamsOn() - { - var streamCount = 0; - var loopCount = 20; - Stream lastStream = null; - var config = new ConfigurationBuilder() - .AddInMemoryCollection(new Dictionary - { - { "server.urls", "http://localhost:8801/" }, - { "kestrel.reuseStreams", "true" } - }) - .Build(); - - var builder = new WebApplicationBuilder() - .UseConfiguration(config) - .UseServerFactory("Microsoft.AspNet.Server.Kestrel") - .Configure(app => - { - var serverInfo = app.ServerFeatures.Get(); - app.Run(context => - { - if (context.Request.Body != lastStream) - { - lastStream = context.Request.Body; - streamCount++; - } - return context.Request.Body.CopyToAsync(context.Response.Body); - }); - }); - - using (var app = builder.Build().Start()) - { - using (var client = new HttpClient()) - { - for (int i = 0; i < loopCount; i++) - { - var content = $"{i} Hello World {i}"; - var request = new HttpRequestMessage() - { - RequestUri = new Uri("http://localhost:8801/"), - Method = HttpMethod.Post, - Content = new StringContent(content) - }; - request.Headers.Add("Connection", new string[] { "Keep-Alive" }); - var responseMessage = await client.SendAsync(request); - var result = await responseMessage.Content.ReadAsStringAsync(); - Assert.Equal(content, result); - } - } - } - - Assert.True(streamCount < loopCount); - } - - [Fact] - public async Task ReuseStreamsOff() - { - var streamCount = 0; - var loopCount = 20; - Stream lastStream = null; - var config = new ConfigurationBuilder() - .AddInMemoryCollection(new Dictionary - { - { "server.urls", "http://localhost:8802/" }, - { "kestrel.reuseStreams", "false" } - }) - .Build(); - - var hostBuilder = new WebApplicationBuilder() - .UseConfiguration(config) - .UseServerFactory("Microsoft.AspNet.Server.Kestrel") - .Configure(app => - { - var serverInfo = app.ServerFeatures.Get(); - app.Run(context => - { - if (context.Request.Body != lastStream) - { - lastStream = context.Request.Body; - streamCount++; - } - return context.Request.Body.CopyToAsync(context.Response.Body); - }); - }); - - using (var app = hostBuilder.Build().Start()) - { - using (var client = new HttpClient()) - { - for (int i = 0; i < loopCount; i++) - { - var content = $"{i} Hello World {i}"; - var request = new HttpRequestMessage() - { - RequestUri = new Uri("http://localhost:8802/"), - Method = HttpMethod.Post, - Content = new StringContent(content) - }; - request.Headers.Add("Connection", new string[] { "Keep-Alive" }); - var responseMessage = await client.SendAsync(request); - var result = await responseMessage.Content.ReadAsStringAsync(); - Assert.Equal(content, result); - } - } - } - - Assert.Equal(loopCount, streamCount); - } - } -} diff --git a/test/Microsoft.AspNet.Server.KestrelTests/EngineTests.cs b/test/Microsoft.AspNet.Server.KestrelTests/EngineTests.cs index 0bfa31b438..76b65c58f1 100644 --- a/test/Microsoft.AspNet.Server.KestrelTests/EngineTests.cs +++ b/test/Microsoft.AspNet.Server.KestrelTests/EngineTests.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Linq; using System.IO; using System.Net; using System.Net.Sockets; @@ -182,6 +183,92 @@ namespace Microsoft.AspNet.Server.KestrelTests } } + [ConditionalTheory] + [MemberData(nameof(ConnectionFilterData))] + public async Task ReuseStreamsOn(ServiceContext testContext) + { + testContext.ReuseStreams = true; + + var streamCount = 0; + var loopCount = 20; + Stream lastStream = null; + + using (var server = new TestServer( + context => + { + if (context.Request.Body != lastStream) + { + lastStream = context.Request.Body; + streamCount++; + } + context.Response.Headers.Clear(); + return context.Request.Body.CopyToAsync(context.Response.Body); + }, + testContext)) + { + + using (var connection = new TestConnection()) + { + var requestData = + Enumerable.Repeat("GET / HTTP/1.1\r\n", loopCount) + .Concat(new[] { "GET / HTTP/1.1\r\nConnection: close\r\n\r\nGoodbye" }); + + var responseData = + Enumerable.Repeat("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n", loopCount) + .Concat(new[] { "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\nGoodbye" }); + + await connection.SendEnd(requestData.ToArray()); + + await connection.ReceiveEnd(responseData.ToArray()); + } + + Assert.Equal(1, streamCount); + } + } + + [ConditionalTheory] + [MemberData(nameof(ConnectionFilterData))] + public async Task ReuseStreamsOff(ServiceContext testContext) + { + testContext.ReuseStreams = false; + + var streamCount = 0; + var loopCount = 20; + Stream lastStream = null; + + using (var server = new TestServer( + context => + { + if (context.Request.Body != lastStream) + { + lastStream = context.Request.Body; + streamCount++; + } + context.Response.Headers.Clear(); + return context.Request.Body.CopyToAsync(context.Response.Body); + }, + testContext)) + { + + using (var connection = new TestConnection()) + { + var requestData = + Enumerable.Repeat("GET / HTTP/1.1\r\n", loopCount) + .Concat(new[] { "GET / HTTP/1.1\r\nConnection: close\r\n\r\nGoodbye" }); + + var responseData = + Enumerable.Repeat("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n", loopCount) + .Concat(new[] { "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\nGoodbye" }); + + await connection.SendEnd(requestData.ToArray()); + + await connection.ReceiveEnd(responseData.ToArray()); + } + + Assert.Equal(loopCount + 1, streamCount); + } + } + [ConditionalTheory] [MemberData(nameof(ConnectionFilterData))] [FrameworkSkipCondition(RuntimeFrameworks.Mono, SkipReason = "Test hangs after execution on Mono.")]