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

Remove Moq #1472

Merged
merged 1 commit into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.6.3" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="1.1.1" />
<PackageVersion Include="MinVer" Version="4.3.0" />
<PackageVersion Include="Moq" Version="4.18.4" />
<PackageVersion Include="NSubstitute" Version="5.0.0" />
<PackageVersion Include="Polly" Version="$(PollyVersion)" />
<PackageVersion Include="Polly.Core" Version="$(PollyVersion)" />
<PackageVersion Include="Polly.Extensions" Version="$(PollyVersion)" />
Expand Down
2 changes: 1 addition & 1 deletion eng/Test.targets
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<PackageReference Include="FluentAssertions" />
<PackageReference Include="GitHubActionsTestLogger" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Moq" />
<PackageReference Include="NSubstitute" />
<PackageReference Include="ReportGenerator" PrivateAssets="all" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio" PrivateAssets="all" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Microsoft.Extensions.Time.Testing;
using Moq;
using NSubstitute;
using Polly.CircuitBreaker;
using Polly.Telemetry;
using Polly.Utils;
Expand All @@ -9,23 +9,23 @@ namespace Polly.Core.Tests.CircuitBreaker;
public class CircuitBreakerResilienceStrategyTests : IDisposable
{
private readonly FakeTimeProvider _timeProvider;
private readonly Mock<CircuitBehavior> _behavior;
private readonly CircuitBehavior _behavior;
private readonly ResilienceStrategyTelemetry _telemetry;
private readonly CircuitBreakerStrategyOptions<int> _options;
private readonly CircuitStateController<int> _controller;

public CircuitBreakerResilienceStrategyTests()
{
_timeProvider = new FakeTimeProvider();
_behavior = new Mock<CircuitBehavior>(MockBehavior.Strict);
_telemetry = TestUtilities.CreateResilienceTelemetry(Mock.Of<DiagnosticSource>());
_behavior = Substitute.For<CircuitBehavior>();
_telemetry = TestUtilities.CreateResilienceTelemetry(Substitute.For<DiagnosticSource>());
_options = new CircuitBreakerStrategyOptions<int>();
_controller = new CircuitStateController<int>(
CircuitBreakerConstants.DefaultBreakDuration,
null,
null,
null,
_behavior.Object,
_behavior,
_timeProvider,
_telemetry);
}
Expand Down Expand Up @@ -58,16 +58,15 @@ public async Task Ctor_ManualControl_EnsureAttached()
await _options.ManualControl.IsolateAsync(CancellationToken.None);
strategy.Invoking(s => s.Execute(_ => 0)).Should().Throw<IsolatedCircuitException>();

_behavior.Setup(v => v.OnCircuitClosed());
await _options.ManualControl.CloseAsync(CancellationToken.None);

_behavior.Setup(v => v.OnActionSuccess(CircuitState.Closed));
strategy.Invoking(s => s.Execute(_ => 0)).Should().NotThrow();

_options.ManualControl.Dispose();
strategy.Invoking(s => s.Execute(_ => 0)).Should().Throw<ObjectDisposedException>();

_behavior.VerifyAll();
_behavior.Received().OnCircuitClosed();
_behavior.Received().OnActionSuccess(CircuitState.Closed);
}

[Fact]
Expand All @@ -77,10 +76,12 @@ public void Execute_HandledResult_OnFailureCalled()
var strategy = Create();
var shouldBreak = false;

_behavior.Setup(v => v.OnActionFailure(CircuitState.Closed, out shouldBreak));
_behavior.When(v => v.OnActionFailure(CircuitState.Closed, out Arg.Any<bool>()))
.Do(x => x[1] = shouldBreak);

strategy.Execute(_ => -1).Should().Be(-1);

_behavior.VerifyAll();
_behavior.Received().OnActionFailure(CircuitState.Closed, out Arg.Any<bool>());
}

[Fact]
Expand All @@ -89,10 +90,9 @@ public void Execute_UnhandledResult_OnActionSuccess()
_options.ShouldHandle = args => new ValueTask<bool>(args.Result is -1);
var strategy = Create();

_behavior.Setup(v => v.OnActionSuccess(CircuitState.Closed));
strategy.Execute(_ => 0).Should().Be(0);

_behavior.VerifyAll();
_behavior.Received(1).OnActionSuccess(CircuitState.Closed);
}

[Fact]
Expand All @@ -102,11 +102,12 @@ public void Execute_HandledException_OnFailureCalled()
var strategy = Create();
var shouldBreak = false;

_behavior.Setup(v => v.OnActionFailure(CircuitState.Closed, out shouldBreak));
_behavior.When(v => v.OnActionFailure(CircuitState.Closed, out Arg.Any<bool>()))
.Do(x => x[1] = shouldBreak);

strategy.Invoking(s => s.Execute<int>(_ => throw new InvalidOperationException())).Should().Throw<InvalidOperationException>();

_behavior.VerifyAll();
_behavior.Received().OnActionFailure(CircuitState.Closed, out Arg.Any<bool>());
}

[Fact]
Expand All @@ -117,7 +118,9 @@ public void Execute_UnhandledException_NoCalls()

strategy.Invoking(s => s.Execute(_ => throw new ArgumentException())).Should().Throw<ArgumentException>();

_behavior.VerifyNoOtherCalls();
_behavior.DidNotReceiveWithAnyArgs().OnActionFailure(default, out Arg.Any<bool>());
_behavior.DidNotReceiveWithAnyArgs().OnActionSuccess(default);
_behavior.DidNotReceiveWithAnyArgs().OnCircuitClosed();
}

public void Dispose() => _controller.Dispose();
Expand All @@ -126,10 +129,12 @@ public void Execute_UnhandledException_NoCalls()
public void Execute_Ok()
{
_options.ShouldHandle = _ => PredicateResult.False;
_behavior.Setup(v => v.OnActionSuccess(CircuitState.Closed));

Create().Invoking(s => s.Execute(_ => 0)).Should().NotThrow();

_behavior.Received(1).OnActionSuccess(CircuitState.Closed);
}

private ReactiveResilienceStrategyBridge<int> Create() => new(new CircuitBreakerResilienceStrategy<int>(_options.ShouldHandle!, _controller, _options.StateProvider, _options.ManualControl));
private ReactiveResilienceStrategyBridge<int> Create()
=> new(new CircuitBreakerResilienceStrategy<int>(_options.ShouldHandle!, _controller, _options.StateProvider, _options.ManualControl));
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using Moq;
using NSubstitute;
using Polly.CircuitBreaker;
using Polly.CircuitBreaker.Health;

namespace Polly.Core.Tests.CircuitBreaker.Controller;

public class AdvancedCircuitBehaviorTests
{
private Mock<HealthMetrics> _metrics = new(MockBehavior.Strict, TimeProvider.System);
private HealthMetrics _metrics = Substitute.For<HealthMetrics>(TimeProvider.System);

[InlineData(10, 10, 0.0, 0.1, false)]
[InlineData(10, 10, 0.1, 0.1, true)]
Expand All @@ -21,15 +21,14 @@ public void OnActionFailure_WhenClosed_EnsureCorrectBehavior(
double failureThreshold,
bool expectedShouldBreak)
{
_metrics.Setup(m => m.IncrementFailure());
_metrics.Setup(m => m.GetHealthInfo()).Returns(new HealthInfo(throughput, failureRate));
_metrics.GetHealthInfo().Returns(new HealthInfo(throughput, failureRate));

var behavior = new AdvancedCircuitBehavior(failureThreshold, minimumThruput, _metrics.Object);
var behavior = new AdvancedCircuitBehavior(failureThreshold, minimumThruput, _metrics);

behavior.OnActionFailure(CircuitState.Closed, out var shouldBreak);

shouldBreak.Should().Be(expectedShouldBreak);
_metrics.VerifyAll();
_metrics.Received(1).IncrementFailure();
}

[InlineData(CircuitState.Closed, true)]
Expand All @@ -39,7 +38,7 @@ public void OnActionFailure_WhenClosed_EnsureCorrectBehavior(
[Theory]
public void OnActionFailure_State_EnsureCorrectCalls(CircuitState state, bool shouldIncrementFailure)
{
_metrics = new(MockBehavior.Loose, TimeProvider.System);
_metrics = Substitute.For<HealthMetrics>(TimeProvider.System);

var sut = Create();

Expand All @@ -48,27 +47,27 @@ public void OnActionFailure_State_EnsureCorrectCalls(CircuitState state, bool sh
shouldBreak.Should().BeFalse();
if (shouldIncrementFailure)
{
_metrics.Verify(v => v.IncrementFailure(), Times.Once());
_metrics.Received(1).IncrementFailure();
}
else
{
_metrics.Verify(v => v.IncrementFailure(), Times.Never());
_metrics.DidNotReceive().IncrementFailure();
}
}

[Fact]
public void OnCircuitClosed_Ok()
{
_metrics = new(MockBehavior.Loose, TimeProvider.System);
_metrics = Substitute.For<HealthMetrics>(TimeProvider.System);
var sut = Create();

sut.OnCircuitClosed();

_metrics.Verify(v => v.Reset(), Times.Once());
_metrics.Received(1).Reset();
}

private AdvancedCircuitBehavior Create()
{
return new AdvancedCircuitBehavior(CircuitBreakerConstants.DefaultFailureRatio, CircuitBreakerConstants.DefaultMinimumThroughput, _metrics.Object);
return new(CircuitBreakerConstants.DefaultFailureRatio, CircuitBreakerConstants.DefaultMinimumThroughput, _metrics);
}
}
Loading