Skip to content

Commit

Permalink
Fix metering tests (#1488)
Browse files Browse the repository at this point in the history
  • Loading branch information
martintmk authored Aug 15, 2023
1 parent 72e5599 commit 02687e8
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions test/Polly.Extensions.Tests/Telemetry/TelemetryListenerImplTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,10 @@ public class TelemetryListenerImplTests : IDisposable
private readonly ILoggerFactory _loggerFactory;
private readonly List<MeteringEvent> _events = new();
private Action<TelemetryEventArguments<object, object>>? _onEvent;
private IDisposable _metering;

public TelemetryListenerImplTests()
{
_metering = TestUtilities.EnablePollyMetering(_events);
_loggerFactory = TestUtilities.CreateLoggerFactory(out _logger);
}
public TelemetryListenerImplTests() => _loggerFactory = TestUtilities.CreateLoggerFactory(out _logger);

public void Dispose()
{
_metering.Dispose();
_loggerFactory.Dispose();
}
public void Dispose() => _loggerFactory.Dispose();

[Fact]
public void Meter_Ok()
Expand Down Expand Up @@ -113,6 +104,8 @@ public void WriteEvent_LoggingWithoutInstanceName_Ok()
[Theory]
public void WriteEvent_EnsureSeverityRespected(ResilienceEventSeverity severity, LogLevel logLevel)
{
using var metering = TestUtilities.EnablePollyMetering(_events);

var telemetry = Create();
ReportEvent(telemetry, null, severity: severity);

Expand Down Expand Up @@ -184,6 +177,7 @@ public void WriteExecutionAttempt_NotEnabled_EnsureNotLogged()
[Theory]
public void WriteEvent_MeteringWithoutEnrichers_Ok(bool noOutcome, bool exception)
{
using var metering = TestUtilities.EnablePollyMetering(_events);
var telemetry = Create();
Outcome<object>? outcome = noOutcome switch
{
Expand Down Expand Up @@ -231,6 +225,7 @@ public void WriteEvent_MeteringWithoutEnrichers_Ok(bool noOutcome, bool exceptio
[Theory]
public void WriteExecutionAttemptEvent_Metering_Ok(bool noOutcome, bool exception)
{
using var metering = TestUtilities.EnablePollyMetering(_events);
var telemetry = Create();
var attemptArg = new ExecutionAttemptArguments(5, TimeSpan.FromSeconds(50), true);
Outcome<object>? outcome = noOutcome switch
Expand Down Expand Up @@ -279,8 +274,7 @@ public void WriteExecutionAttemptEvent_Metering_Ok(bool noOutcome, bool exceptio
[Fact]
public void WriteExecutionAttemptEvent_ShouldBeSkipped()
{
_metering.Dispose();
_metering = TestUtilities.EnablePollyMetering(_events, _ => false);
using var metering = TestUtilities.EnablePollyMetering(_events, _ => false);

var telemetry = Create();
var attemptArg = new ExecutionAttemptArguments(5, TimeSpan.FromSeconds(50), true);
Expand All @@ -295,6 +289,8 @@ public void WriteExecutionAttemptEvent_ShouldBeSkipped()
[Theory]
public void WriteEvent_MeteringWithEnrichers_Ok(int count)
{
using var metering = TestUtilities.EnablePollyMetering(_events);

const int DefaultDimensions = 7;

var telemetry = Create(new[]
Expand Down Expand Up @@ -329,6 +325,7 @@ public void WriteEvent_MeteringWithEnrichers_Ok(int count)
[Fact]
public void WriteEvent_MeteringWithoutBuilderInstance_Ok()
{
using var metering = TestUtilities.EnablePollyMetering(_events);
var telemetry = Create();
ReportEvent(telemetry, null, instanceName: null);
var events = GetEvents("resilience-events")[0].Should().NotContainKey("pipeline-instance");
Expand Down Expand Up @@ -413,6 +410,8 @@ public void PipelineExecution_NoOutcome_Logged()
[Theory]
public void PipelineExecution_Metered(bool healthy, bool exception)
{
using var metering = TestUtilities.EnablePollyMetering(_events);

var healthString = healthy ? "Healthy" : "Unhealthy";
var context = ResilienceContextPool.Shared.Get("op-key").WithResultType<int>();
var outcome = exception ? Outcome.FromException<object>(new InvalidOperationException("dummy message")) : Outcome.FromResult((object)10);
Expand Down Expand Up @@ -472,8 +471,7 @@ public void PipelineExecution_Metered(bool healthy, bool exception)
[Fact]
public void PipelineExecuted_ShouldBeSkipped()
{
_metering.Dispose();
_metering = TestUtilities.EnablePollyMetering(_events, _ => false);
using var metering = TestUtilities.EnablePollyMetering(_events, _ => false);

var telemetry = Create();
var attemptArg = new PipelineExecutedArguments(TimeSpan.FromSeconds(50));
Expand Down

0 comments on commit 02687e8

Please sign in to comment.