react to aspnet/Hosting#1005 (#68)

This commit is contained in:
Andrew Stanton-Nurse 2017-04-06 15:19:34 -07:00 committed by GitHub
parent c950e713c2
commit 50299cbac6
7 changed files with 33 additions and 140 deletions

View File

@ -1,6 +1,6 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26228.10
VisualStudioVersion = 15.0.26403.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{49AB8AAA-8160-48DF-A18B-78F51E54E02A}"
ProjectSection(SolutionItems) = preProject

View File

@ -6,6 +6,7 @@ using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Server.IntegrationTesting;
using Microsoft.AspNetCore.Server.IntegrationTesting.xunit;
using Microsoft.AspNetCore.Testing.xunit;
using Microsoft.DotNet.PlatformAbstractions;
using Microsoft.Extensions.Logging;
@ -15,13 +16,10 @@ using Xunit.Sdk;
namespace ServerComparison.FunctionalTests
{
public class HelloWorldTests
public class HelloWorldTests : LoggedTest
{
private readonly ITestOutputHelper _output;
public HelloWorldTests(ITestOutputHelper output)
public HelloWorldTests(ITestOutputHelper output) : base(output)
{
_output = output;
}
// Tests disabled on x86 because of https://github.com/aspnet/Hosting/issues/601
@ -60,13 +58,10 @@ namespace ServerComparison.FunctionalTests
public async Task HelloWorld(ServerType serverType, RuntimeFlavor runtimeFlavor, RuntimeArchitecture architecture, ApplicationType applicationType, [CallerMemberName] string testName = null)
{
testName = $"{testName}_{serverType}_{runtimeFlavor}_{architecture}_{applicationType}";
var loggerFactory = TestLoggingUtilities.SetUpLogging<HelloWorldTests>(_output, testName);
var logger = loggerFactory.CreateLogger("TestHarness");
Console.WriteLine($"Starting test: {testName}");
logger.LogInformation("Starting test: {testName}", testName);
using (logger.BeginScope("HelloWorld"))
using (StartLog(out var loggerFactory, testName))
{
var logger = loggerFactory.CreateLogger("HelloWorld");
var deploymentParameters = new DeploymentParameters(Helpers.GetApplicationPath(applicationType), serverType, runtimeFlavor, architecture)
{
EnvironmentName = "HelloWorld", // Will pick the Start class named 'StartupHelloWorld',
@ -84,13 +79,11 @@ namespace ServerComparison.FunctionalTests
using (var deployer = ApplicationDeployerFactory.Create(deploymentParameters, loggerFactory))
{
var deploymentResult = await deployer.DeployAsync();
var httpClientHandler = new HttpClientHandler();
var httpClient = new HttpClient(new LoggingHandler(loggerFactory, httpClientHandler)) { BaseAddress = new Uri(deploymentResult.ApplicationBaseUri) };
// Request to base address and check if various parts of the body are rendered & measure the cold startup time.
var response = await RetryHelper.RetryRequest(() =>
{
return httpClient.GetAsync(string.Empty);
return deploymentResult.HttpClient.GetAsync(string.Empty);
}, logger, deploymentResult.HostShutdownToken);
var responseText = await response.Content.ReadAsStringAsync();
@ -106,9 +99,6 @@ namespace ServerComparison.FunctionalTests
}
}
}
Console.WriteLine($"Finished test: {testName}");
logger.LogInformation("Finished test: {testName}", testName);
}
}
}

View File

@ -1,25 +0,0 @@
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
namespace ServerComparison.FunctionalTests
{
internal class LoggingHandler : DelegatingHandler
{
private ILogger _logger;
public LoggingHandler(ILoggerFactory loggerFactory, HttpMessageHandler innerHandler) : base(innerHandler)
{
_logger = loggerFactory.CreateLogger<HttpClient>();
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
_logger.LogDebug("Sending {method} {url}", request.Method, request.RequestUri);
var response = await base.SendAsync(request, cancellationToken);
_logger.LogDebug("Received {statusCode} {reasonPhrase} {url}", response.StatusCode, response.ReasonPhrase, request.RequestUri);
return response;
}
}
}

View File

@ -8,6 +8,7 @@ using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Server.IntegrationTesting;
using Microsoft.AspNetCore.Server.IntegrationTesting.xunit;
using Microsoft.AspNetCore.Testing.xunit;
using Microsoft.DotNet.PlatformAbstractions;
using Microsoft.Extensions.Logging;
@ -17,13 +18,10 @@ using Xunit.Sdk;
namespace ServerComparison.FunctionalTests
{
public class NtlmAuthenticationTests
public class NtlmAuthenticationTests : LoggedTest
{
private readonly ITestOutputHelper _output;
public NtlmAuthenticationTests(ITestOutputHelper output)
public NtlmAuthenticationTests(ITestOutputHelper output) : base(output)
{
_output = output;
}
[ConditionalTheory, Trait("ServerComparison.FunctionalTests", "ServerComparison.FunctionalTests")]
@ -38,13 +36,10 @@ namespace ServerComparison.FunctionalTests
public async Task NtlmAuthentication(ServerType serverType, RuntimeFlavor runtimeFlavor, RuntimeArchitecture architecture, ApplicationType applicationType)
{
var testName = $"NtlmAuthentication_{serverType}_{runtimeFlavor}_{architecture}_{applicationType}";
var loggerFactory = TestLoggingUtilities.SetUpLogging<HelloWorldTests>(_output, testName);
var logger = loggerFactory.CreateLogger("TestHarness");
Console.WriteLine($"Starting test: {testName}");
logger.LogInformation("Starting test: {testName}", testName);
using (logger.BeginScope("NtlmAuthenticationTest"))
using (StartLog(out var loggerFactory, testName))
{
var logger = loggerFactory.CreateLogger("NtlmAuthenticationTest");
var deploymentParameters = new DeploymentParameters(Helpers.GetApplicationPath(applicationType), serverType, runtimeFlavor, architecture)
{
EnvironmentName = "NtlmAuthentication", // Will pick the Start class named 'StartupNtlmAuthentication'
@ -62,8 +57,7 @@ namespace ServerComparison.FunctionalTests
using (var deployer = ApplicationDeployerFactory.Create(deploymentParameters, loggerFactory))
{
var deploymentResult = await deployer.DeployAsync();
var httpClientHandler = new HttpClientHandler();
var httpClient = new HttpClient(new LoggingHandler(loggerFactory, httpClientHandler)) { BaseAddress = new Uri(deploymentResult.ApplicationBaseUri) };
var httpClient = deploymentResult.HttpClient;
// Request to base address and check if various parts of the body are rendered & measure the cold startup time.
var response = await RetryHelper.RetryRequest(() =>
@ -109,8 +103,9 @@ namespace ServerComparison.FunctionalTests
Assert.Equal(HttpStatusCode.Forbidden, response.StatusCode);
logger.LogInformation("Enabling Default Credentials");
httpClientHandler = new HttpClientHandler() { UseDefaultCredentials = true };
httpClient = new HttpClient(httpClientHandler) { BaseAddress = new Uri(deploymentResult.ApplicationBaseUri) };
// Change the http client to one that uses default credentials
httpClient = deploymentResult.CreateHttpClient(new HttpClientHandler() { UseDefaultCredentials = true });
logger.LogInformation("Testing /AutoForbid");
response = await httpClient.GetAsync("/AutoForbid");
@ -155,9 +150,6 @@ namespace ServerComparison.FunctionalTests
}
}
}
Console.WriteLine($"Finished test: {testName}");
logger.LogInformation("Finished test: {testName}", testName);
}
}
}

View File

@ -11,6 +11,7 @@ using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Server.IntegrationTesting;
using Microsoft.AspNetCore.Server.IntegrationTesting.xunit;
using Microsoft.AspNetCore.Testing.xunit;
using Microsoft.DotNet.PlatformAbstractions;
using Microsoft.Extensions.Logging;
@ -21,15 +22,13 @@ using Xunit.Sdk;
namespace ServerComparison.FunctionalTests
{
public class ResponseCompressionTests
public class ResponseCompressionTests : LoggedTest
{
// NGinx's default min size is 20 bytes
private static readonly string HelloWorldBody = "Hello World;" + new string('a', 20);
private readonly ITestOutputHelper _output;
public ResponseCompressionTests(ITestOutputHelper output)
public ResponseCompressionTests(ITestOutputHelper output) : base(output)
{
_output = output;
}
[ConditionalTheory]
@ -127,13 +126,10 @@ namespace ServerComparison.FunctionalTests
public async Task ResponseCompression(ServerType serverType, RuntimeFlavor runtimeFlavor, RuntimeArchitecture architecture, Func<HttpClient, ILogger, Task> scenario, ApplicationType applicationType, bool hostCompression, [CallerMemberName] string testName = null)
{
testName = $"{testName}_{serverType}_{runtimeFlavor}_{architecture}_{applicationType}";
var loggerFactory = TestLoggingUtilities.SetUpLogging<ResponseCompressionTests>(_output, testName);
var logger = loggerFactory.CreateLogger("TestHarness");
Console.WriteLine($"Starting test: {testName}");
logger.LogInformation("Starting test: {testName}", testName);
using (logger.BeginScope("ResponseCompression"))
using (StartLog(out var loggerFactory, testName))
{
var logger = loggerFactory.CreateLogger("ResponseCompression");
var deploymentParameters = new DeploymentParameters(Helpers.GetApplicationPath(applicationType), serverType, runtimeFlavor, architecture)
{
EnvironmentName = "ResponseCompression",
@ -155,7 +151,7 @@ namespace ServerComparison.FunctionalTests
var deploymentResult = await deployer.DeployAsync();
var httpClientHandler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.None };
Assert.True(httpClientHandler.SupportsAutomaticDecompression);
var httpClient = new HttpClient(new LoggingHandler(loggerFactory, httpClientHandler)) { BaseAddress = new Uri(deploymentResult.ApplicationBaseUri) };
var httpClient = deploymentResult.CreateHttpClient(httpClientHandler);
// Request to base address and check if various parts of the body are rendered & measure the cold startup time.
var response = await RetryHelper.RetryRequest(() =>
@ -178,9 +174,6 @@ namespace ServerComparison.FunctionalTests
await scenario(httpClient, logger);
}
}
Console.WriteLine($"Finished test: {testName}");
logger.LogInformation("Finished test: {testName}", testName);
}
private static async Task CheckNoCompressionAsync(HttpClient client, ILogger logger)

View File

@ -8,6 +8,7 @@ using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Server.IntegrationTesting;
using Microsoft.AspNetCore.Server.IntegrationTesting.xunit;
using Microsoft.AspNetCore.Testing.xunit;
using Microsoft.DotNet.PlatformAbstractions;
using Microsoft.Extensions.Logging;
@ -18,13 +19,10 @@ using Xunit.Sdk;
namespace ServerComparison.FunctionalTests
{
public class ResponseTests
public class ResponseTests : LoggedTest
{
private readonly ITestOutputHelper _output;
public ResponseTests(ITestOutputHelper output)
public ResponseTests(ITestOutputHelper output) : base(output)
{
_output = output;
}
[ConditionalTheory]
@ -165,13 +163,10 @@ namespace ServerComparison.FunctionalTests
public async Task ResponseFormats(ServerType serverType, RuntimeFlavor runtimeFlavor, RuntimeArchitecture architecture, Func<HttpClient, ILogger, Task> scenario, ApplicationType applicationType, [CallerMemberName] string testName = null)
{
testName = $"{testName}_{serverType}_{runtimeFlavor}_{architecture}_{applicationType}";
var loggerFactory = TestLoggingUtilities.SetUpLogging<HelloWorldTests>(_output, testName);
var logger = loggerFactory.CreateLogger("TestHarness");
Console.WriteLine($"Starting test: {testName}");
logger.LogInformation("Starting test: {testName}", testName);
using (logger.BeginScope("ResponseFormats"))
using (StartLog(out var loggerFactory, testName))
{
var logger = loggerFactory.CreateLogger("ResponseFormats");
var deploymentParameters = new DeploymentParameters(Helpers.GetApplicationPath(applicationType), serverType, runtimeFlavor, architecture)
{
EnvironmentName = "Responses",
@ -189,13 +184,11 @@ namespace ServerComparison.FunctionalTests
using (var deployer = ApplicationDeployerFactory.Create(deploymentParameters, loggerFactory))
{
var deploymentResult = await deployer.DeployAsync();
var httpClientHandler = new HttpClientHandler();
var httpClient = new HttpClient(new LoggingHandler(loggerFactory, httpClientHandler)) { BaseAddress = new Uri(deploymentResult.ApplicationBaseUri) };
// Request to base address and check if various parts of the body are rendered & measure the cold startup time.
var response = await RetryHelper.RetryRequest(() =>
{
return httpClient.GetAsync(string.Empty);
return deploymentResult.HttpClient.GetAsync(string.Empty);
}, logger, deploymentResult.HostShutdownToken);
var responseText = await response.Content.ReadAsStringAsync();
@ -210,12 +203,9 @@ namespace ServerComparison.FunctionalTests
throw;
}
await scenario(httpClient, logger);
await scenario(deploymentResult.HttpClient, logger);
}
}
Console.WriteLine($"Finished test: {testName}");
logger.LogInformation("Finished test: {testName}", testName);
}
private static async Task CheckContentLengthAsync(HttpClient client, ILogger logger)

View File

@ -1,47 +0,0 @@
using System;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using Microsoft.Extensions.Logging;
using Serilog;
using Xunit.Abstractions;
namespace ServerComparison.FunctionalTests
{
public static class TestLoggingUtilities
{
public static readonly string TestOutputRoot = Environment.GetEnvironmentVariable("ASPNETCORE_TEST_LOG_DIR");
public static ILoggerFactory SetUpLogging<TTestClass>(ITestOutputHelper output, [CallerMemberName] string testName = null)
{
var loggerFactory = new LoggerFactory();
loggerFactory.AddXunit(output, LogLevel.Debug);
if (!string.IsNullOrEmpty(TestOutputRoot))
{
var testClass = typeof(TTestClass).GetTypeInfo();
var testOutputDir = Path.Combine(TestOutputRoot, testClass.Assembly.GetName().Name, testClass.FullName);
if (!Directory.Exists(testOutputDir))
{
Directory.CreateDirectory(testOutputDir);
}
var testOutputFile = Path.Combine(testOutputDir, $"{testName}.log");
if (File.Exists(testOutputFile))
{
File.Delete(testOutputFile);
}
var serilogger = new LoggerConfiguration()
.Enrich.FromLogContext()
.MinimumLevel.Verbose()
.WriteTo.File(testOutputFile, flushToDiskInterval: TimeSpan.FromSeconds(1))
.CreateLogger();
loggerFactory.AddSerilog(serilogger);
}
return loggerFactory;
}
}
}