Skip to content

Commit

Permalink
Add new metering tests to cover uncovered lines (#1474)
Browse files Browse the repository at this point in the history
  • Loading branch information
martintmk committed Aug 10, 2023
1 parent 855ce01 commit c8e0366
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public class ResilienceTelemetryDiagnosticSourceTests : IDisposable
private readonly FakeLogger _logger;
private readonly ILoggerFactory _loggerFactory;
private readonly List<MeteringEvent> _events = new();
private readonly IDisposable _metering;
private Action<TelemetryEventArguments>? _onEvent;
private IDisposable _metering;

public ResilienceTelemetryDiagnosticSourceTests()
{
Expand Down Expand Up @@ -290,6 +290,20 @@ public void WriteExecutionAttemptEvent_Metering_Ok(bool noOutcome, bool exceptio
_events.Single(v => v.Name == "execution-attempt-duration").Measurement.Should().Be(50000);
}

[Fact]
public void WriteExecutionAttemptEvent_ShouldBeSkipped()
{
_metering.Dispose();
_metering = TestUtilities.EnablePollyMetering(_events, _ => false);

var telemetry = Create();
var attemptArg = new ExecutionAttemptArguments(5, TimeSpan.FromSeconds(50), true);
ReportEvent(telemetry, Outcome.FromResult<object>(true), context: ResilienceContextPool.Shared.Get("op-key").WithResultType<bool>(), arg: attemptArg);

var events = GetEvents("execution-attempt-duration");
events.Should().HaveCount(0);
}

[InlineData(1)]
[InlineData(100)]
[Theory]
Expand Down
7 changes: 6 additions & 1 deletion test/Polly.TestUtils/TestUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static ILoggerFactory CreateLoggerFactory(out FakeLogger logger)
return loggerFactory;
}

public static IDisposable EnablePollyMetering(ICollection<MeteringEvent> events)
public static IDisposable EnablePollyMetering(ICollection<MeteringEvent> events, Predicate<Instrument>? shouldListen = null)
{
var stateStr = Guid.NewGuid().ToString();
var meterListener = new MeterListener
Expand All @@ -61,6 +61,11 @@ public static IDisposable EnablePollyMetering(ICollection<MeteringEvent> events)
{
if (instrument.Meter.Name == "Polly")
{
if (shouldListen is not null && !shouldListen(instrument))
{
return;
}
listener.EnableMeasurementEvents(instrument, stateStr);
}
}
Expand Down

0 comments on commit c8e0366

Please sign in to comment.