Skip to content

Commit

Permalink
fix tests (#3906)
Browse files Browse the repository at this point in the history
  • Loading branch information
vishweshbankwar authored Nov 15, 2022
1 parent f770047 commit a758c60
Showing 1 changed file with 80 additions and 28 deletions.
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

0 comments on commit a758c60

Please sign in to comment.