Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solve 404 error in unit test #5145

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,7 @@ public async Task DiagnosticSourceExceptionCallBackIsNotReceivedForExceptionsHan
int numberOfUnSubscribedEvents = 0;
int numberOfSubscribedEvents = 0;
int numberOfExceptionCallbacks = 0;
bool exceptionHandled = false;

// configure SDK
this.tracerProvider = Sdk.CreateTracerProviderBuilder()
Expand Down Expand Up @@ -991,18 +992,18 @@ public async Task DiagnosticSourceExceptionCallBackIsNotReceivedForExceptionsHan
})
.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())
{
Expand All @@ -1020,6 +1021,7 @@ public async Task DiagnosticSourceExceptionCallBackIsNotReceivedForExceptionsHan
Assert.Equal(0, numberOfExceptionCallbacks);
Assert.Equal(0, numberOfUnSubscribedEvents);
Assert.Equal(2, numberOfSubscribedEvents);
Assert.True(exceptionHandled);
}

public void Dispose()
Expand Down
2 changes: 2 additions & 0 deletions test/TestApp.AspNetCore/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public static void Main(string[] args)

app.UseMiddleware<ActivityMiddleware>();

app.AddTestMiddleware();

app.Run();
}
}
24 changes: 24 additions & 0 deletions test/TestApp.AspNetCore/TestMiddleware.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}