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

Rename FakeTimeProvider #1349

Merged
merged 2 commits into from
Jun 24, 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
4 changes: 2 additions & 2 deletions src/Polly.Core/Utils/DisposeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public static async ValueTask TryDisposeSafeAsync<T>(T value, bool isSynchronous
}
}
}
catch (Exception e)
catch (Exception)
{
Debug.Assert(false, e.ToString());
// Swallow any exception
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void AddCircuitBreaker_IntegrationTest()
OnHalfOpened = (_) => { halfOpened++; return default; }
};

var timeProvider = new FakeTimeProvider();
var timeProvider = new MockTimeProvider();
var strategy = new ResilienceStrategyBuilder { TimeProvider = timeProvider.Object }.AddSimpleCircuitBreaker(options).Build();
var time = DateTime.UtcNow;
timeProvider.Setup(v => v.GetUtcNow()).Returns(() => time);
Expand Down Expand Up @@ -151,7 +151,7 @@ public void AddAdvancedCircuitBreaker_IntegrationTest()
OnHalfOpened = (_) => { halfOpened++; return default; }
};

var timeProvider = new FakeTimeProvider();
var timeProvider = new MockTimeProvider();
var strategy = new ResilienceStrategyBuilder { TimeProvider = timeProvider.Object }.AddAdvancedCircuitBreaker(options).Build();
var time = DateTime.UtcNow;
timeProvider.Setup(v => v.GetUtcNow()).Returns(() => time);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ namespace Polly.Core.Tests.CircuitBreaker;

public class CircuitBreakerResilienceStrategyTests : IDisposable
{
private readonly FakeTimeProvider _timeProvider;
private readonly MockTimeProvider _timeProvider;
private readonly Mock<CircuitBehavior> _behavior;
private readonly ResilienceStrategyTelemetry _telemetry;
private readonly SimpleCircuitBreakerStrategyOptions<int> _options;
private readonly CircuitStateController<int> _controller;

public CircuitBreakerResilienceStrategyTests()
{
_timeProvider = new FakeTimeProvider();
_timeProvider = new MockTimeProvider();
_timeProvider.Setup(v => v.GetUtcNow()).Returns(DateTime.UtcNow);
_behavior = new Mock<CircuitBehavior>(MockBehavior.Strict);
_telemetry = TestUtilities.CreateResilienceTelemetry(Mock.Of<DiagnosticSource>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Polly.Core.Tests.CircuitBreaker.Controller;
public class CircuitStateControllerTests
{
private readonly FakeTimeProvider _timeProvider = new();
private readonly MockTimeProvider _timeProvider = new();
private readonly CircuitBreakerStrategyOptions<int> _options = new SimpleCircuitBreakerStrategyOptions<int>();
private readonly Mock<CircuitBehavior> _circuitBehavior = new(MockBehavior.Strict);
private readonly Action<TelemetryEventArguments> _onTelemetry = _ => { };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace Polly.Core.Tests.CircuitBreaker.Health;
public class RollingHealthMetricsTests
{
private readonly FakeTimeProvider _timeProvider;
private readonly MockTimeProvider _timeProvider;
private readonly TimeSpan _samplingDuration = TimeSpan.FromSeconds(10);
private readonly short _windows = 10;

public RollingHealthMetricsTests() => _timeProvider = new FakeTimeProvider().SetupUtcNow();
public RollingHealthMetricsTests() => _timeProvider = new MockTimeProvider().SetupUtcNow();

[Fact]
public void Ctor_EnsureDefaults()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace Polly.Core.Tests.CircuitBreaker.Health;
public class SingleHealthMetricsTests
{
private readonly FakeTimeProvider _timeProvider;
private readonly MockTimeProvider _timeProvider;

public SingleHealthMetricsTests() => _timeProvider = new FakeTimeProvider().SetupUtcNow();
public SingleHealthMetricsTests() => _timeProvider = new MockTimeProvider().SetupUtcNow();

[Fact]
public void Ctor_EnsureDefaults()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void Properties_GetSet_Ok()
_builder.BuilderName = "dummy";
_builder.BuilderName.Should().Be("dummy");

var timeProvider = new FakeTimeProvider().Object;
var timeProvider = new MockTimeProvider().Object;
_builder.TimeProvider = timeProvider;
_builder.TimeProvider.Should().Be(timeProvider);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@

namespace Polly.Core.Tests.Helpers;

internal class FakeTimeProvider : Mock<TimeProvider>
internal class MockTimeProvider : Mock<TimeProvider>
{
private DateTimeOffset? _time;

public FakeTimeProvider()
public MockTimeProvider()
: base(MockBehavior.Strict)
{
}

public FakeTimeProvider SetupUtcNow(DateTimeOffset? time = null)
public MockTimeProvider SetupUtcNow(DateTimeOffset? time = null)
{
_time = time ?? DateTimeOffset.UtcNow;
Setup(x => x.GetUtcNow()).Returns(() => _time.Value);
return this;
}

public FakeTimeProvider AdvanceTime(TimeSpan time)
public MockTimeProvider AdvanceTime(TimeSpan time)
{
if (_time == null)
{
Expand All @@ -29,37 +29,37 @@ public FakeTimeProvider AdvanceTime(TimeSpan time)
return this;
}

public FakeTimeProvider SetupTimestampFrequency()
public MockTimeProvider SetupTimestampFrequency()
{
Setup(x => x.TimestampFrequency).Returns(Stopwatch.Frequency);
return this;
}

public FakeTimeProvider SetupAnyDelay(CancellationToken cancellationToken = default)
public MockTimeProvider SetupAnyDelay(CancellationToken cancellationToken = default)
{
Setup(x => x.Delay(It.IsAny<TimeSpan>(), cancellationToken)).Returns(Task.CompletedTask);
return this;
}

public FakeTimeProvider SetupGetTimestamp()
public MockTimeProvider SetupGetTimestamp()
{
Setup(x => x.GetTimestamp()).Returns(0);
return this;
}

public FakeTimeProvider SetupDelay(TimeSpan delay, CancellationToken cancellationToken = default)
public MockTimeProvider SetupDelay(TimeSpan delay, CancellationToken cancellationToken = default)
{
Setup(x => x.Delay(delay, cancellationToken)).Returns(Task.CompletedTask);
return this;
}

public FakeTimeProvider SetupDelayCancelled(TimeSpan delay, CancellationToken cancellationToken = default)
public MockTimeProvider SetupDelayCancelled(TimeSpan delay, CancellationToken cancellationToken = default)
{
Setup(x => x.Delay(delay, cancellationToken)).ThrowsAsync(new OperationCanceledException());
return this;
}

public FakeTimeProvider SetupCancelAfterNow(TimeSpan delay)
public MockTimeProvider SetupCancelAfterNow(TimeSpan delay)
{
Setup(v => v.CancelAfter(It.IsAny<CancellationTokenSource>(), delay)).Callback<CancellationTokenSource, TimeSpan>((cts, _) => cts.Cancel());
return this;
Expand Down
2 changes: 1 addition & 1 deletion test/Polly.Core.Tests/Issues/IssuesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ namespace Polly.Core.Tests.Issues;

public partial class IssuesTests
{
private FakeTimeProvider TimeProvider { get; } = new FakeTimeProvider().SetupUtcNow().SetupAnyDelay().SetupGetTimestamp().SetupTimestampFrequency();
private MockTimeProvider TimeProvider { get; } = new MockTimeProvider().SetupUtcNow().SetupAnyDelay().SetupGetTimestamp().SetupTimestampFrequency();
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class ResilienceStrategyBuilderContextTests
public void Ctor_EnsureDefaults()
{
var properties = new ResilienceProperties();
var timeProvider = new FakeTimeProvider();
var timeProvider = new MockTimeProvider();
var context = new ResilienceStrategyBuilderContext("builder-name", properties, "strategy-name", "strategy-type", timeProvider.Object, true, Mock.Of<DiagnosticSource>(), () => 1.0);

context.IsGenericBuilder.Should().BeTrue();
Expand Down
2 changes: 1 addition & 1 deletion test/Polly.Core.Tests/ResilienceStrategyBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public void BuildStrategy_EnsureCorrectContext()
var builder = new ResilienceStrategyBuilder
{
BuilderName = "builder-name",
TimeProvider = new FakeTimeProvider().Object,
TimeProvider = new MockTimeProvider().Object,
};

builder.AddStrategy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Polly.Core.Tests.Retry;
public class RetryResilienceStrategyTests
{
private readonly RetryStrategyOptions _options = new();
private readonly FakeTimeProvider _timeProvider = new();
private readonly MockTimeProvider _timeProvider = new();
private readonly Mock<DiagnosticSource> _diagnosticSource = new();
private ResilienceStrategyTelemetry _telemetry;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Polly.Core.Tests.Timeout;
public class TimeoutResilienceStrategyTests : IDisposable
{
private readonly ResilienceStrategyTelemetry _telemetry;
private readonly FakeTimeProvider _timeProvider;
private readonly MockTimeProvider _timeProvider;
private readonly TimeoutStrategyOptions _options;
private readonly CancellationTokenSource _cancellationSource;
private readonly TimeSpan _delay = TimeSpan.FromSeconds(12);
Expand All @@ -16,7 +16,7 @@ public class TimeoutResilienceStrategyTests : IDisposable
public TimeoutResilienceStrategyTests()
{
_telemetry = TestUtilities.CreateResilienceTelemetry(_diagnosticSource.Object);
_timeProvider = new FakeTimeProvider();
_timeProvider = new MockTimeProvider();
_options = new TimeoutStrategyOptions();
_cancellationSource = new CancellationTokenSource();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class CancellationTokenSourcePoolTests
public static IEnumerable<object[]> TimeProviders()
{
yield return new object[] { TimeProvider.System };
yield return new object[] { new FakeTimeProvider() };
yield return new object[] { new MockTimeProvider() };
}

[Fact]
Expand Down
6 changes: 3 additions & 3 deletions test/Polly.Core.Tests/Utils/TimeProviderExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public async Task DelayAsync_System_Ok(bool synchronous, bool mocked, bool hasCa
using var tcs = new CancellationTokenSource();
var token = hasCancellation ? tcs.Token : default;
var delay = TimeSpan.FromMilliseconds(10);
var mock = new FakeTimeProvider();
var mock = new MockTimeProvider();
var timeProvider = mocked ? mock.Object : TimeProvider.System;
var context = ResilienceContext.Get();
context.Initialize<VoidResult>(isSynchronous: synchronous);
Expand Down Expand Up @@ -83,7 +83,7 @@ public async Task DelayAsync_CancellationRequestedBefore_Throws(bool synchronous
tcs.Cancel();
var token = tcs.Token;
var delay = TimeSpan.FromMilliseconds(10);
var mock = new FakeTimeProvider();
var mock = new MockTimeProvider();
var timeProvider = mocked ? mock.Object : TimeProvider.System;
var context = ResilienceContext.Get();
context.Initialize<VoidResult>(isSynchronous: synchronous);
Expand All @@ -104,7 +104,7 @@ public async Task DelayAsync_CancellationAfter_Throws(bool synchronous, bool moc

await TestUtilities.AssertWithTimeoutAsync(async () =>
{
var mock = new FakeTimeProvider();
var mock = new MockTimeProvider();
using var tcs = new CancellationTokenSource();
var token = tcs.Token;
var timeProvider = mocked ? mock.Object : TimeProvider.System;
Expand Down
2 changes: 1 addition & 1 deletion test/Polly.Extensions.Tests/Polly.Extensions.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="..\Polly.Core.Tests\Helpers\FakeTimeProvider.cs" Link="Utils\FakeTimeProvider.cs" />
<Compile Include="..\Polly.Core.Tests\Helpers\MockTimeProvider.cs" Link="Utils\MockTimeProvider.cs" />
<Compile Include="..\Polly.Core.Tests\Utils\ObjectPoolTests.cs" Link="Utils\ObjectPoolTests.cs" />
</ItemGroup>

Expand Down