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

Cleanup of callback API #1125

Merged
merged 6 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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 src/Polly.Core.Benchmarks/Internals/Helper.Pipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal static partial class Helper
builder
.AddTimeout(TimeSpan.FromSeconds(1))
.AddRetry(
predicate => predicate.Exception<InvalidOperationException>().Result(10),
predicate => predicate.HandleException<InvalidOperationException>().HandleResult(10),
RetryBackoffType.Constant,
3,
TimeSpan.FromSeconds(1))
Expand Down
4 changes: 2 additions & 2 deletions src/Polly.Core.Benchmarks/Internals/Helper.Retry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public static object CreateRetry(PollyVersion technology)
BaseDelay = delay
};

options.ShouldRetry.Outcome<int>((outcome, _) => outcome.Result == 10 || outcome.Exception is InvalidOperationException);
options.OnRetry.Add<int>(() => { });
options.ShouldRetry.HandleOutcome<int>((outcome, _) => outcome.Result == 10 || outcome.Exception is InvalidOperationException);
options.OnRetry.Register<int>(() => { });

builder.AddRetry(options);
}),
Expand Down
32 changes: 0 additions & 32 deletions src/Polly.Core.Tests/Retry/RetryDelayGeneratorTests.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,38 @@

namespace Polly.Core.Tests.Retry;

#pragma warning disable CA2012 // Use ValueTasks correctly

public class RetryResilienceStrategyBuilderExtensionsTests
{
public static readonly TheoryData<Action<ResilienceStrategyBuilder>> OverloadsData = new()
{
builder =>
{
builder.AddRetry(retry=>retry.Result(10));
builder.AddRetry(retry=>retry.HandleResult(10));
AssertStrategy(builder, RetryBackoffType.Exponential, 3, TimeSpan.FromSeconds(2));
},
builder =>
{
builder.AddRetry(retry=>retry.Result(10), RetryBackoffType.Linear);
builder.AddRetry(retry=>retry.HandleResult(10), RetryBackoffType.Linear);
AssertStrategy(builder, RetryBackoffType.Linear, 3, TimeSpan.FromSeconds(2));
},
builder =>
{
builder.AddRetry(retry=>retry.Result(10), RetryBackoffType.Linear, 2, TimeSpan.FromSeconds(1));
builder.AddRetry(retry=>retry.HandleResult(10), RetryBackoffType.Linear, 2, TimeSpan.FromSeconds(1));
AssertStrategy(builder, RetryBackoffType.Linear, 2, TimeSpan.FromSeconds(1));
},
builder =>
{
builder.AddRetry(retry => retry.HandleResult(10), attempt => TimeSpan.FromMilliseconds(attempt));

AssertStrategy(builder, RetryBackoffType.Exponential, 3, TimeSpan.FromSeconds(2), strategy =>
{
var args = new RetryDelayArguments(ResilienceContext.Get(), 8, TimeSpan.Zero);

strategy.DelayGenerator!.Generate(new Polly.Strategy.Outcome<bool>(new InvalidOperationException()), args).Result.Should().Be(TimeSpan.FromMilliseconds(8));
});
},
};

[MemberData(nameof(OverloadsData))]
Expand All @@ -46,13 +59,15 @@ public void AddRetry_DefaultOptions_Ok()
AssertStrategy(builder, options.BackoffType, options.RetryCount, options.BaseDelay);
}

private static void AssertStrategy(ResilienceStrategyBuilder builder, RetryBackoffType type, int retries, TimeSpan delay)
private static void AssertStrategy(ResilienceStrategyBuilder builder, RetryBackoffType type, int retries, TimeSpan delay, Action<RetryResilienceStrategy>? assert = null)
{
var strategy = (RetryResilienceStrategy)builder.Build();

strategy.BackoffType.Should().Be(type);
strategy.RetryCount.Should().Be(retries);
strategy.BaseDelay.Should().Be(delay);

assert?.Invoke(strategy);
}

[Fact]
Expand Down
26 changes: 13 additions & 13 deletions src/Polly.Core.Tests/Retry/RetryResilienceStrategyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class RetryResilienceStrategyTests
public void ShouldRetryEmpty_Skipped()
{
bool called = false;
_options.OnRetry.Add<int>(() => called = true);
_options.OnRetry.Register<int>(() => called = true);
SetupNoDelay();
var sut = CreateSut();

Expand All @@ -30,8 +30,8 @@ public void ShouldRetryEmpty_Skipped()
public void Retry_RetryCount_Respected()
{
int calls = 0;
_options.OnRetry.Add<int>(() => calls++);
_options.ShouldRetry.Result<int>(0);
_options.OnRetry.Register<int>(() => calls++);
_options.ShouldRetry.HandleResult<int>(0);
_options.RetryCount = 12;
SetupNoDelay();
var sut = CreateSut();
Expand All @@ -45,12 +45,12 @@ public void Retry_RetryCount_Respected()
public void RetryException_RetryCount_Respected()
{
int calls = 0;
_options.OnRetry.Add<int>((args, _) =>
_options.OnRetry.Register<int>((args, _) =>
{
args.Exception.Should().BeOfType<InvalidOperationException>();
calls++;
});
_options.ShouldRetry.Exception<InvalidOperationException>();
_options.ShouldRetry.HandleException<InvalidOperationException>();
_options.RetryCount = 3;
SetupNoDelay();
var sut = CreateSut();
Expand All @@ -65,7 +65,7 @@ public void Retry_Infinite_Respected()
{
int calls = 0;
_options.BackoffType = RetryBackoffType.Constant;
_options.OnRetry.Add<int>((_, args) =>
_options.OnRetry.Register<int>((_, args) =>
{
if (args.Attempt > RetryConstants.MaxRetryCount)
{
Expand All @@ -74,7 +74,7 @@ public void Retry_Infinite_Respected()

calls++;
});
_options.ShouldRetry.Result(0);
_options.ShouldRetry.HandleResult(0);
_options.RetryCount = RetryStrategyOptions.InfiniteRetryCount;
SetupNoDelay();
var sut = CreateSut();
Expand All @@ -88,8 +88,8 @@ public void Retry_Infinite_Respected()
public void RetryDelayGenerator_Respected()
{
int calls = 0;
_options.OnRetry.Add<int>(() => calls++);
_options.ShouldRetry.Result<int>(0);
_options.OnRetry.Register<int>(() => calls++);
_options.ShouldRetry.HandleResult<int>(0);
_options.RetryCount = 3;
_options.BackoffType = RetryBackoffType.Constant;
_options.RetryDelayGenerator.SetGenerator<int>((_, _) => TimeSpan.FromMilliseconds(123));
Expand All @@ -107,7 +107,7 @@ public void OnRetry_EnsureCorrectArguments()
{
var attempts = new List<int>();
var delays = new List<TimeSpan>();
_options.OnRetry.Add<int>((outcome, args) =>
_options.OnRetry.Register<int>((outcome, args) =>
{
attempts.Add(args.Attempt);
delays.Add(args.RetryDelay);
Expand All @@ -116,7 +116,7 @@ public void OnRetry_EnsureCorrectArguments()
outcome.Result.Should().Be(0);
});

_options.ShouldRetry.Result<int>(0);
_options.ShouldRetry.HandleResult<int>(0);
_options.RetryCount = 3;
_options.BackoffType = RetryBackoffType.Linear;
_timeProvider.SetupAnyDelay();
Expand All @@ -143,7 +143,7 @@ public void OnRetry_EnsureTelemetry()

_diagnosticSource.Setup(v => v.IsEnabled("OnRetry")).Returns(true);

_options.ShouldRetry.Result<int>(0);
_options.ShouldRetry.HandleResult(0);
_options.RetryCount = 3;
_options.BackoffType = RetryBackoffType.Linear;
_timeProvider.SetupAnyDelay();
Expand Down Expand Up @@ -171,7 +171,7 @@ public void RetryDelayGenerator_EnsureCorrectArguments()
return TimeSpan.Zero;
});

_options.ShouldRetry.Result<int>(0);
_options.ShouldRetry.HandleResult<int>(0);
_options.RetryCount = 3;
_options.BackoffType = RetryBackoffType.Linear;
_timeProvider.SetupAnyDelay();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

namespace Polly.Core.Tests.Strategy;

public class SimpleEventTests
public class NoOutcomeEventTests
{
[Fact]
public async Task Add_EnsureOrdering()
{
var ev = new DummyEvent();
var ev = new NoOutcomeEvent<TestArguments>();
var raisedEvents = new List<int>();

ev.Add(() => raisedEvents.Add(1));
ev.Add(args => raisedEvents.Add(2));
ev.Add(args => { raisedEvents.Add(3); return default; });
ev.Register(() => raisedEvents.Add(1));
ev.Register(args => raisedEvents.Add(2));
ev.Register(args => { raisedEvents.Add(3); return default; });

var handler = ev.CreateHandler()!;

Expand All @@ -25,11 +25,11 @@ public async Task Add_EnsureOrdering()
[Fact]
public void Add_EnsureValidation()
{
var ev = new DummyEvent();
var ev = new NoOutcomeEvent<TestArguments>();

Assert.Throws<ArgumentNullException>(() => ev.Add((Action)null!));
Assert.Throws<ArgumentNullException>(() => ev.Add((Action<TestArguments>)null!));
Assert.Throws<ArgumentNullException>(() => ev.Add(null!));
Assert.Throws<ArgumentNullException>(() => ev.Register((Action)null!));
Assert.Throws<ArgumentNullException>(() => ev.Register((Action<TestArguments>)null!));
Assert.Throws<ArgumentNullException>(() => ev.Register(null!));
}

[InlineData(1)]
Expand All @@ -38,12 +38,12 @@ public void Add_EnsureValidation()
[Theory]
public async Task CreateHandler_Ok(int count)
{
var ev = new DummyEvent();
var ev = new NoOutcomeEvent<TestArguments>();
var events = new List<int>();

for (var i = 0; i < count; i++)
{
ev.Add(() => events.Add(i));
ev.Register(() => events.Add(i));
}

await ev.CreateHandler()!(new TestArguments());
Expand All @@ -55,14 +55,10 @@ public async Task CreateHandler_Ok(int count)
[Fact]
public void CreateHandler_NoEvents_Null()
{
var ev = new DummyEvent();
var ev = new NoOutcomeEvent<TestArguments>();

var handler = ev.CreateHandler();

handler.Should().BeNull();
}

private class DummyEvent : SimpleEvent<TestArguments, DummyEvent>
{
}
}
Loading