Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix metering tests #1488

Merged
merged 1 commit into from
Aug 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading