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

[ASP.NET Core] Fix few tests #3906

Merged
Merged
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
108 changes: 80 additions & 28 deletions test/OpenTelemetry.Instrumentation.AspNetCore.Tests/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -757,11 +757,10 @@ public async Task ShouldExportActivityWithOneOrMoreExceptionFilters(int mode)
}

[Fact]
public async Task DiagnosticSourceCustomCallbacksAreReceivedOnlyForSubscribedEvents()
public async Task DiagnosticSourceCallbacksAreReceivedOnlyForSubscribedEvents()
{
int numberOfCustomCallbacks = 0;
string expectedCustomEventName = "Microsoft.AspNetCore.Mvc.BeforeAction";
string actualCustomEventName = null;
int numberOfUnSubscribedEvents = 0;
int numberofSubscribedEvents = 0;
void ConfigureTestServices(IServiceCollection services)
{
this.tracerProvider = Sdk.CreateTracerProviderBuilder()
Expand All @@ -772,10 +771,27 @@ void ConfigureTestServices(IServiceCollection services)
{
switch (name)
{
case HttpInListener.OnStartEvent:
{
numberofSubscribedEvents++;
}

break;
case HttpInListener.OnStopEvent:
{
numberofSubscribedEvents++;
}

break;
case HttpInListener.OnMvcBeforeActionEvent:
{
actualCustomEventName = name;
numberOfCustomCallbacks++;
numberofSubscribedEvents++;
}

break;
default:
{
numberOfUnSubscribedEvents++;
}

break;
Expand All @@ -800,13 +816,15 @@ void ConfigureTestServices(IServiceCollection services)
using var response = await client.SendAsync(request);
}

Assert.Equal(1, numberOfCustomCallbacks);
Assert.Equal(expectedCustomEventName, actualCustomEventName);
Assert.Equal(0, numberOfUnSubscribedEvents);
Assert.Equal(3, numberofSubscribedEvents);
}

[Fact]
public async Task DiagnosticSourceExceptionCallbackIsReceivedForUnHandledException()
{
int numberOfUnSubscribedEvents = 0;
int numberofSubscribedEvents = 0;
int numberOfExceptionCallbacks = 0;
void ConfigureTestServices(IServiceCollection services)
{
Expand All @@ -818,14 +836,40 @@ void ConfigureTestServices(IServiceCollection services)
{
switch (name)
{
case HttpInListener.OnStartEvent:
{
numberofSubscribedEvents++;
}

break;
case HttpInListener.OnStopEvent:
{
numberofSubscribedEvents++;
}

break;
case HttpInListener.OnMvcBeforeActionEvent:
{
numberofSubscribedEvents++;
}

break;

// TODO: Add test case for validating name for both the types
// of exception event.
case HttpInListener.OnUnhandledHostingExceptionEvent:
case HttpInListener.OnUnHandledDiagnosticsExceptionEvent:
{
numberofSubscribedEvents++;
numberOfExceptionCallbacks++;
}

break;
default:
{
numberOfUnSubscribedEvents++;
}

break;
}
},
Expand Down Expand Up @@ -856,11 +900,15 @@ void ConfigureTestServices(IServiceCollection services)
}

Assert.Equal(1, numberOfExceptionCallbacks);
Assert.Equal(0, numberOfUnSubscribedEvents);
Assert.Equal(4, numberofSubscribedEvents);
}

[Fact]
public async Task DiagnosticSourceExceptionCallBackIsNotReceivedForExceptionsHandledInMiddleware()
{
int numberOfUnSubscribedEvents = 0;
int numberofSubscribedEvents = 0;
int numberOfExceptionCallbacks = 0;

// configure SDK
Expand All @@ -872,12 +920,34 @@ public async Task DiagnosticSourceExceptionCallBackIsNotReceivedForExceptionsHan
{
switch (name)
{
case HttpInListener.OnStartEvent:
{
numberofSubscribedEvents++;
}

break;
case HttpInListener.OnStopEvent:
{
numberofSubscribedEvents++;
}

break;

// TODO: Add test case for validating name for both the types
// of exception event.
case HttpInListener.OnUnhandledHostingExceptionEvent:
case HttpInListener.OnUnHandledDiagnosticsExceptionEvent:
{
numberofSubscribedEvents++;
numberOfExceptionCallbacks++;
}

break;
default:
{
numberOfUnSubscribedEvents++;
}

break;
}
},
Expand Down Expand Up @@ -919,6 +989,8 @@ static void ThrowException(IApplicationBuilder app)
}

Assert.Equal(0, numberOfExceptionCallbacks);
Assert.Equal(0, numberOfUnSubscribedEvents);
Assert.Equal(2, numberofSubscribedEvents);

await app.DisposeAsync();
}
Expand Down Expand Up @@ -955,26 +1027,6 @@ private static void ValidateAspNetCoreActivity(Activity activityToValidate, stri
Assert.Equal(expectedHttpPath, activityToValidate.GetTagValue(SemanticConventions.AttributeHttpTarget) as string);
}

private static void ActivityEnrichment(Activity activity, string method, object obj)
{
Assert.True(activity.IsAllDataRequested);
switch (method)
{
case "OnStartActivity":
Assert.True(obj is HttpRequest);
break;

case "OnStopActivity":
Assert.True(obj is HttpResponse);
break;

default:
break;
}

activity.SetTag("enriched", "yes");
}

private static void AssertException(List<Activity> exportedItems)
{
Assert.Single(exportedItems);
Expand Down