Skip to content

Commit

Permalink
Test
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK committed Dec 12, 2023
1 parent fa06bfc commit a4bf039
Showing 1 changed file with 71 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -295,11 +295,11 @@ public async Task Metrics_ExceptionThrown_DefaultSettings_Handled_Reported()
}

[Fact]
public async Task Metrics_ExceptionThrown_Handled_RouteAvailable()
public async Task Metrics_ExceptionThrown_Handled_UseOriginalRoute()
{
// Arrange
var builder = new RouteEndpointBuilder(c => Task.CompletedTask, RoutePatternFactory.Parse("/path"), 0);
var endpoint = builder.Build();
var originalEndpointBuilder = new RouteEndpointBuilder(c => Task.CompletedTask, RoutePatternFactory.Parse("/path"), 0);
var originalEndpoint = originalEndpointBuilder.Build();

var meterFactory = new TestMeterFactory();
using var requestDurationCollector = new MetricCollector<double>(meterFactory, "Microsoft.AspNetCore.Hosting", "http.server.request.duration");
Expand All @@ -323,7 +323,7 @@ public async Task Metrics_ExceptionThrown_Handled_RouteAvailable()
});
app.Run(context =>
{
context.SetEndpoint(endpoint);
context.SetEndpoint(originalEndpoint);
throw new Exception("Test exception");
});
Expand Down Expand Up @@ -354,6 +354,73 @@ public async Task Metrics_ExceptionThrown_Handled_RouteAvailable()
m => AssertRequestException(m, "System.Exception", "handled"));
}

[Fact]
public async Task Metrics_ExceptionThrown_Handled_UseNewRoute()
{
// Arrange
var originalEndpointBuilder = new RouteEndpointBuilder(c => Task.CompletedTask, RoutePatternFactory.Parse("/path"), 0);
var originalEndpoint = originalEndpointBuilder.Build();

var newEndpointBuilder = new RouteEndpointBuilder(c => Task.CompletedTask, RoutePatternFactory.Parse("/new"), 0);
var newEndpoint = newEndpointBuilder.Build();

var meterFactory = new TestMeterFactory();
using var requestDurationCollector = new MetricCollector<double>(meterFactory, "Microsoft.AspNetCore.Hosting", "http.server.request.duration");
using var requestExceptionCollector = new MetricCollector<long>(meterFactory, DiagnosticsMetrics.MeterName, "aspnetcore.diagnostics.exceptions");

using var host = new HostBuilder()
.ConfigureServices(s =>
{
s.AddSingleton<IMeterFactory>(meterFactory);
s.AddSingleton(LoggerFactory);
})
.ConfigureWebHost(webHostBuilder =>
{
webHostBuilder
.UseTestServer()
.Configure(app =>
{
app.UseExceptionHandler(new ExceptionHandlerOptions
{
ExceptionHandler = (c) =>
{
c.SetEndpoint(newEndpoint);
return Task.CompletedTask;
}
});
app.Run(context =>
{
context.SetEndpoint(originalEndpoint);
throw new Exception("Test exception");
});
});
}).Build();

await host.StartAsync();

var server = host.GetTestServer();

// Act
var response = await server.CreateClient().GetAsync("/path");
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);

await requestDurationCollector.WaitForMeasurementsAsync(minCount: 1).DefaultTimeout();

// Assert
Assert.Collection(
requestDurationCollector.GetMeasurementSnapshot(),
m =>
{
Assert.True(m.Value > 0);
Assert.Equal(500, (int)m.Tags["http.response.status_code"]);
Assert.Equal("System.Exception", (string)m.Tags["error.type"]);
Assert.Equal("/new", (string)m.Tags["http.route"]);
});
Assert.Collection(requestExceptionCollector.GetMeasurementSnapshot(),
m => AssertRequestException(m, "System.Exception", "handled"));
}

[Fact]
public async Task Metrics_ExceptionThrown_Unhandled_Reported()
{
Expand Down

0 comments on commit a4bf039

Please sign in to comment.