Skip to content

Commit

Permalink
update tests to more explicitly wait for payloadsender
Browse files Browse the repository at this point in the history
  • Loading branch information
Mpdreamz committed Nov 30, 2023
1 parent 069171f commit 187f6f7
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
using Elastic.Apm.Model;
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;

namespace Elastic.Apm.AspNetCore.Tests;

[Collection("DiagnosticListenerTest")]
public class BaggageAspNetCoreTests : MultiApplicationTestBase
{

public BaggageAspNetCoreTests(ITestOutputHelper output) : base(output) { }

private void ValidateOtelAttribute(Transaction transaction, string key, string value) =>
transaction.Otel.Attributes.Should().Contain(new KeyValuePair<string, object>($"baggage.{key}", value));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information

using System;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
Expand All @@ -18,6 +19,8 @@ namespace Elastic.Apm.AspNetCore.Tests
[Collection("DiagnosticListenerTest")]
public class DistributedTracingAspNetCoreTests : MultiApplicationTestBase
{
public DistributedTracingAspNetCoreTests(ITestOutputHelper output) : base(output) { }

/// <summary>
/// Distributed tracing integration test.
/// It starts <see cref="SampleAspNetCoreApp" /> with 1 agent and <see cref="WebApiSample" /> with another agent.
Expand Down Expand Up @@ -112,6 +115,8 @@ public async Task DistributedTraceAcross2ServicesWithTraceState()
var res = await client.GetAsync("http://localhost:5901/Home/DistributedTracingMiniSample");
res.IsSuccessStatusCode.Should().BeTrue();

_payloadSender1.WaitForTransactions(TimeSpan.FromSeconds(10));

_payloadSender1.FirstTransaction.IsSampled.Should().BeTrue();
_payloadSender2.FirstTransaction.IsSampled.Should().BeTrue();

Expand All @@ -136,6 +141,8 @@ public async Task PreferW3CTraceHeaderOverElasticTraceHeader()
var res = await client.GetAsync("http://localhost:5901/Home/Index");
res.IsSuccessStatusCode.Should().BeTrue();

_payloadSender1.WaitForTransactions(TimeSpan.FromSeconds(10));

_payloadSender1.FirstTransaction.TraceId.Should().Be(expectedTraceId);
_payloadSender1.FirstTransaction.ParentId.Should().Be(expectedParentId);
}
Expand All @@ -158,6 +165,8 @@ public async Task TraceContextIgnoreSampledFalse_WithNoTraceState()
var res = await client.GetAsync("http://localhost:5901/Home/Index");
res.IsSuccessStatusCode.Should().BeTrue();

_payloadSender1.WaitForTransactions(TimeSpan.FromSeconds(10));

// Assert that the transaction is sampled and the traceparent header was ignored
_payloadSender1.FirstTransaction.IsSampled.Should().BeTrue();

Expand Down Expand Up @@ -185,6 +194,8 @@ public async Task TraceContextIgnoreSampledFalse_WithEsTraceState_NotSampled()
var res = await client.GetAsync("http://localhost:5901/Home/Index");
res.IsSuccessStatusCode.Should().BeTrue();

_payloadSender1.WaitForTransactions(TimeSpan.FromSeconds(10));

// Assert that the transaction is not sampled, so the traceparent header was not ignored
_payloadSender1.FirstTransaction.IsSampled.Should().BeFalse();

Expand Down Expand Up @@ -212,6 +223,8 @@ public async Task TraceContextIgnoreSampledFalse_WithNonEsTraceState_NotSampled(
var res = await client.GetAsync("http://localhost:5901/Home/Index");
res.IsSuccessStatusCode.Should().BeTrue();

_payloadSender1.WaitForTransactions(TimeSpan.FromSeconds(10));

// Assert that the transaction is sampled and the traceparent header was ignored
_payloadSender1.FirstTransaction.IsSampled.Should().BeTrue();

Expand Down Expand Up @@ -240,6 +253,8 @@ public async Task TraceContextIgnoreSampledFalse_NotSet_WithNonEsTraceState_NotS
var res = await client.GetAsync("http://localhost:5901/Home/Index");
res.IsSuccessStatusCode.Should().BeTrue();

_payloadSender1.WaitForTransactions(TimeSpan.FromSeconds(10));

// Assert that the transaction is not sampled and the traceparent header was not ignored
_payloadSender1.FirstTransaction.IsSampled.Should().BeFalse();

Expand All @@ -262,6 +277,8 @@ public async Task TraceContinuationStrategyContinue()
var res = await client.GetAsync("http://localhost:5901/Home/Index");
res.IsSuccessStatusCode.Should().BeTrue();

_payloadSender1.WaitForTransactions(TimeSpan.FromSeconds(10));

_payloadSender1.FirstTransaction.IsSampled.Should().BeTrue();
_payloadSender1.FirstTransaction.ParentId.Should().Be("b7ad6b7169203331");
_payloadSender1.FirstTransaction.TraceId.Should().Be("0af7651916cd43dd8448eb211c80319c");
Expand All @@ -284,6 +301,8 @@ public async Task TraceContinuationStrategyDefault()
var res = await client.GetAsync("http://localhost:5901/Home/Index");
res.IsSuccessStatusCode.Should().BeTrue();

_payloadSender1.WaitForTransactions(TimeSpan.FromSeconds(10));

_payloadSender1.FirstTransaction.IsSampled.Should().BeTrue();
_payloadSender1.FirstTransaction.ParentId.Should().Be("b7ad6b7169203331");
_payloadSender1.FirstTransaction.TraceId.Should().Be("0af7651916cd43dd8448eb211c80319c");
Expand All @@ -306,6 +325,8 @@ public async Task TraceContinuationStrategyRestartExternalWithNoEsTag()
var res = await client.GetAsync("http://localhost:5901/Home/Index");
res.IsSuccessStatusCode.Should().BeTrue();

_payloadSender1.WaitForTransactions(TimeSpan.FromSeconds(10));

_payloadSender1.FirstTransaction.IsSampled.Should().BeTrue();
_payloadSender1.FirstTransaction.ParentId.Should().NotBe("b7ad6b7169203331");
_payloadSender1.FirstTransaction.TraceId.Should().NotBe("0af7651916cd43dd8448eb211c80319c");
Expand All @@ -330,6 +351,8 @@ public async Task TraceContinuationStrategyRestartExternalWithEsTag()
var res = await client.GetAsync("http://localhost:5901/Home/Index");
res.IsSuccessStatusCode.Should().BeTrue();

_payloadSender1.WaitForTransactions(TimeSpan.FromSeconds(10));

_payloadSender1.FirstTransaction.IsSampled.Should().BeTrue();
_payloadSender1.FirstTransaction.ParentId.Should().Be("b7ad6b7169203331");
_payloadSender1.FirstTransaction.TraceId.Should().Be("0af7651916cd43dd8448eb211c80319c");
Expand All @@ -352,6 +375,8 @@ public async Task TraceContinuationStrategyRestartWithEsTag()
var res = await client.GetAsync("http://localhost:5901/Home/Index");
res.IsSuccessStatusCode.Should().BeTrue();

_payloadSender1.WaitForTransactions(TimeSpan.FromSeconds(10));

_payloadSender1.FirstTransaction.IsSampled.Should().BeTrue();
_payloadSender1.FirstTransaction.ParentId.Should().NotBe("b7ad6b7169203331");
_payloadSender1.FirstTransaction.TraceId.Should().NotBe("0af7651916cd43dd8448eb211c80319c");
Expand All @@ -376,6 +401,8 @@ public async Task TraceContinuationStrategyRestartWithoutEsTag()
var res = await client.GetAsync("http://localhost:5901/Home/Index");
res.IsSuccessStatusCode.Should().BeTrue();

_payloadSender1.WaitForTransactions(TimeSpan.FromSeconds(10));

_payloadSender1.FirstTransaction.IsSampled.Should().BeTrue();
_payloadSender1.FirstTransaction.ParentId.Should().NotBe("b7ad6b7169203331");
_payloadSender1.FirstTransaction.TraceId.Should().NotBe("0af7651916cd43dd8448eb211c80319c");
Expand All @@ -401,6 +428,8 @@ public async Task TraceContinuationStrategyRestartExternalAndNoTraceParent()
var res = await client.GetAsync("http://localhost:5901/Home/Index");
res.IsSuccessStatusCode.Should().BeTrue();

_payloadSender1.WaitForTransactions(TimeSpan.FromSeconds(10));

_payloadSender1.Transactions.Should().NotBeNullOrEmpty();
}

Expand All @@ -423,6 +452,8 @@ public async Task TraceContinuationStrategyRestartExternalAndNoTraceState()
var res = await client.GetAsync("http://localhost:5901/Home/Index");
res.IsSuccessStatusCode.Should().BeTrue();

_payloadSender1.WaitForTransactions(TimeSpan.FromSeconds(10));

_payloadSender1.Transactions.Should().NotBeNullOrEmpty();

// The trace is restarted (due to `traceContinuationStrategy=restart_external`), so assert that the traceId and
Expand Down Expand Up @@ -454,6 +485,9 @@ private async Task ExecuteAndCheckDistributedCall(bool startActivityBeforeHttpCa
var res = await client.GetAsync("http://localhost:5901/Home/DistributedTracingMiniSample");
res.IsSuccessStatusCode.Should().BeTrue();

_payloadSender1.WaitForTransactions(TimeSpan.FromSeconds(10));
_payloadSender2.WaitForTransactions(TimeSpan.FromSeconds(10));

_payloadSender1.Transactions.Count.Should().Be(1);
_payloadSender2.Transactions.Count.Should().Be(1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
using Elastic.Apm.Api;
using Elastic.Apm.DiagnosticSource;
using Elastic.Apm.EntityFrameworkCore;
using Elastic.Apm.Logging;
using Elastic.Apm.Tests.Utilities;
using Elastic.Apm.Tests.Utilities.XUnit;
using FluentAssertions;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -24,7 +26,7 @@ public class FailedRequestTests : IAsyncLifetime
{
private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();

private readonly MockPayloadSender _payloadSender1 = new MockPayloadSender();
private MockPayloadSender _payloadSender1;
private ApmAgent _agent1;

private Task _taskForApp1;
Expand All @@ -34,7 +36,13 @@ public class FailedRequestTests : IAsyncLifetime

public Task InitializeAsync()
{
_agent1 = new ApmAgent(new TestAgentComponents(payloadSender: _payloadSender1, configuration: new MockConfiguration(exitSpanMinDuration: "0")));
var logger = new XUnitLogger(LogLevel.Trace, _output);
_payloadSender1 = new MockPayloadSender(logger);
_agent1 = new ApmAgent(new TestAgentComponents(
payloadSender: _payloadSender1,
configuration: new MockConfiguration(exitSpanMinDuration: "0", flushInterval: "0"),
logger: logger
));

_taskForApp1 = Program.CreateWebHostBuilder(null)
.ConfigureLogging(logging => logging.AddXunit(_output))
Expand Down Expand Up @@ -71,6 +79,7 @@ public async Task DistributedTraceAcross2Service()
var client = new HttpClient();
var res = await client.GetAsync("http://localhost:5901/Home/TriggerError");
res.IsSuccessStatusCode.Should().BeFalse();
_payloadSender1.WaitForAny();

_payloadSender1.Transactions.Count.Should().Be(1);
_payloadSender1.FirstTransaction.Should().NotBeNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
using System.Threading.Tasks;
using Elastic.Apm.DiagnosticSource;
using Elastic.Apm.EntityFrameworkCore;
using Elastic.Apm.Logging;
using Elastic.Apm.Tests.Utilities;
using Elastic.Apm.Tests.Utilities.XUnit;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using SampleAspNetCoreApp;
using Xunit;
using Xunit.Abstractions;

namespace Elastic.Apm.AspNetCore.Tests;

Expand All @@ -21,21 +24,28 @@ namespace Elastic.Apm.AspNetCore.Tests;
/// </summary>
public abstract class MultiApplicationTestBase : IAsyncLifetime
{
internal readonly MockPayloadSender _payloadSender1 = new();
internal readonly MockPayloadSender _payloadSender2 = new();
private readonly ITestOutputHelper _output;
internal MockPayloadSender _payloadSender1;
internal MockPayloadSender _payloadSender2;
private readonly CancellationTokenSource _cancellationTokenSource = new();
internal ApmAgent _agent1;
internal ApmAgent _agent2;

private Task _taskForApp1;
private Task _taskForApp2;

public MultiApplicationTestBase(ITestOutputHelper output) => _output = output;

public Task InitializeAsync()
{
_agent1 = new ApmAgent(new TestAgentComponents(payloadSender: _payloadSender1,
configuration: new MockConfiguration(exitSpanMinDuration: "0")));
_agent2 = new ApmAgent(new TestAgentComponents(payloadSender: _payloadSender2,
configuration: new MockConfiguration(exitSpanMinDuration: "0")));
var logger1 = new XUnitLogger(LogLevel.Trace, _output, "Sender 1");
_payloadSender1 = new MockPayloadSender(logger1);
var logger2 = new XUnitLogger(LogLevel.Trace, _output, "Sender 2");
_payloadSender2 = new MockPayloadSender(logger2);

var configuration = new MockConfiguration(exitSpanMinDuration: "0", flushInterval: "3s");
_agent1 = new ApmAgent(new TestAgentComponents(payloadSender: _payloadSender1, configuration: configuration, logger: logger1));
_agent2 = new ApmAgent(new TestAgentComponents(payloadSender: _payloadSender2, configuration: configuration, logger: logger2));

_taskForApp1 = Program.CreateWebHostBuilder(null)
.ConfigureServices(services =>
Expand Down

0 comments on commit 187f6f7

Please sign in to comment.