Skip to content

Commit

Permalink
Use WebApplicationFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
ngruson committed Dec 7, 2023
1 parent a3c6d65 commit a76e470
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 26 deletions.
47 changes: 21 additions & 26 deletions test/OpenTelemetry.Instrumentation.AspNetCore.Tests/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
#if NET7_0_OR_GREATER
using Microsoft.AspNetCore.Http.Features;
#endif
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -1038,39 +1035,37 @@ public async Task DiagnosticSourceExceptionCallBackIsNotReceivedForExceptionsHan
Assert.Equal(2, numberOfSubscribedEvents);
}

#if NET7_0_OR_GREATER
#if NET6_0_OR_GREATER
[Fact]
public async Task NoSiblingActivityCreatedWhenIdFormatIsW3CWithTraceFlagsNone()
public async Task NoSiblingActivityCreatedWhenTraceFlagsNone()
{
this.tracerProvider = Sdk.CreateTracerProviderBuilder()
.SetSampler(new AlwaysOnSampler())
.AddAspNetCoreInstrumentation()
.Build();

var builder = WebApplication.CreateBuilder();
builder.WebHost.UseUrls("http://*:0");
var app = builder.Build();

app.MapGet("/values", (HttpContext context) =>
{
var activity = context.Features.GetRequiredFeature<IHttpActivityFeature>().Activity;
var equal = Activity.Current.Id == activity.Id;

return Results.Ok(equal);
});

_ = app.RunAsync();
using var testFactory = this.factory
.WithWebHostBuilder(builder =>
{
builder.ConfigureTestServices(services =>
{
this.tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddAspNetCoreInstrumentation()
.Build();
});

var url = app.Urls.ToArray()[0];
var portNumber = url.Substring(url.LastIndexOf(':') + 1);
builder.ConfigureLogging(loggingBuilder => loggingBuilder.ClearProviders());
});
using var client = testFactory.CreateClient();
var request = new HttpRequestMessage(HttpMethod.Get, "/api/GetActivityEquality");
var traceId = ActivityTraceId.CreateRandom();
var spanId = ActivitySpanId.CreateRandom();
request.Headers.Add("traceparent", $"00-{traceId}-{spanId}-00");

using var client = new HttpClient();
var req = new HttpRequestMessage(HttpMethod.Get, $"http://localhost:{portNumber}/values");
req.Headers.Add("traceparent", "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-00");
var res = await client.SendAsync(req);
var result = bool.Parse(await res.Content.ReadAsStringAsync());
var response = await client.SendAsync(request);
var result = bool.Parse(await response.Content.ReadAsStringAsync());

Assert.True(res.IsSuccessStatusCode);
Assert.True(response.IsSuccessStatusCode);
Assert.True(result);
}
#endif
Expand Down
10 changes: 10 additions & 0 deletions test/TestApp.AspNetCore/Controllers/ChildActivityController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// </copyright>

using System.Diagnostics;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc;
using OpenTelemetry;

Expand Down Expand Up @@ -47,4 +48,13 @@ public IReadOnlyDictionary<string, string> GetChildActivityBaggageContext()
var result = Baggage.Current.GetBaggage();
return result;
}

[HttpGet]
[Route("api/GetActivityEquality")]
public bool GetActivityEquality()
{
var activity = this.HttpContext.Features.GetRequiredFeature<IHttpActivityFeature>().Activity;
var equal = Activity.Current?.Id == activity.Id;
return equal;
}
}
1 change: 1 addition & 0 deletions test/TestApp.AspNetCore/TestApp.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Features" />
<PackageReference Include="Swashbuckle.AspNetCore" />
</ItemGroup>

Expand Down

0 comments on commit a76e470

Please sign in to comment.