Skip to content

Commit

Permalink
Solve 404 error in unit test (#5145)
Browse files Browse the repository at this point in the history
  • Loading branch information
ngruson committed Dec 8, 2023
1 parent f5c1160 commit f075128
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
18 changes: 10 additions & 8 deletions test/OpenTelemetry.Instrumentation.AspNetCore.Tests/BasicTests.cs
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;
}
}

0 comments on commit f075128

Please sign in to comment.