Solve 404 error in unit test (#5145)

This commit is contained in:
Nils Gruson 2023-12-08 20:43:00 +01:00 committed by GitHub
parent f5c116001f
commit f075128b12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 8 deletions

View File

@ -947,6 +947,7 @@ public sealed class BasicTests
int numberOfUnSubscribedEvents = 0;
int numberOfSubscribedEvents = 0;
int numberOfExceptionCallbacks = 0;
bool exceptionHandled = false;
// configure SDK
this.tracerProvider = Sdk.CreateTracerProviderBuilder()
@ -991,18 +992,18 @@ public sealed class BasicTests
})
.Build();
TestMiddleware.Create(builder => builder
.UseExceptionHandler(handler =>
handler.Run(async (ctx) =>
{
exceptionHandled = true;
await ctx.Response.WriteAsync("handled");
})));
using (var client = this.factory
.WithWebHostBuilder(builder =>
{
builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders());
builder.Configure(app => app
.UseExceptionHandler(handler =>
{
handler.Run(async (ctx) =>
{
await ctx.Response.WriteAsync("handled");
});
}));
})
.CreateClient())
{
@ -1020,6 +1021,7 @@ public sealed class BasicTests
Assert.Equal(0, numberOfExceptionCallbacks);
Assert.Equal(0, numberOfUnSubscribedEvents);
Assert.Equal(2, numberOfSubscribedEvents);
Assert.True(exceptionHandled);
}
public void Dispose()

View File

@ -47,6 +47,8 @@ public class Program
app.UseMiddleware<ActivityMiddleware>();
app.AddTestMiddleware();
app.Run();
}
}

View File

@ -0,0 +1,24 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
namespace TestApp.AspNetCore;
public static class TestMiddleware
{
private static readonly AsyncLocal<Action<IApplicationBuilder>?> Current = new();
public static IApplicationBuilder AddTestMiddleware(this IApplicationBuilder builder)
{
if (Current.Value is { } configure)
{
configure(builder);
}
return builder;
}
public static void Create(Action<IApplicationBuilder> action)
{
Current.Value = action;
}
}