-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
674 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/Polly.Core.Tests/Retry/RetryStrategyOptionsTResultTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using Polly.Retry; | ||
using Polly.Strategy; | ||
using Polly.Utils; | ||
|
||
namespace Polly.Core.Tests.Retry; | ||
|
||
public class RetryStrategyOptionsTResultTests | ||
{ | ||
[Fact] | ||
public void Ctor_Ok() | ||
{ | ||
var options = new RetryStrategyOptions<int>(); | ||
|
||
options.ShouldRetry.Should().NotBeNull(); | ||
options.ShouldRetry.IsEmpty.Should().BeTrue(); | ||
|
||
options.RetryDelayGenerator.Should().NotBeNull(); | ||
options.RetryDelayGenerator.IsEmpty.Should().BeTrue(); | ||
|
||
options.OnRetry.Should().NotBeNull(); | ||
options.OnRetry.IsEmpty.Should().BeTrue(); | ||
|
||
options.RetryCount.Should().Be(3); | ||
options.BackoffType.Should().Be(RetryBackoffType.Exponential); | ||
options.BaseDelay.Should().Be(TimeSpan.FromSeconds(2)); | ||
} | ||
|
||
[Fact] | ||
public void InvalidOptions() | ||
{ | ||
var options = new RetryStrategyOptions<int> | ||
{ | ||
ShouldRetry = null!, | ||
RetryDelayGenerator = null!, | ||
OnRetry = null!, | ||
RetryCount = -3, | ||
BaseDelay = TimeSpan.MinValue | ||
}; | ||
|
||
options.Invoking(o => ValidationHelper.ValidateObject(o, "Invalid Options")) | ||
.Should() | ||
.Throw<ValidationException>() | ||
.WithMessage(""" | ||
Invalid Options | ||
|
||
Validation Errors: | ||
The field RetryCount must be between -1 and 100. | ||
The field BaseDelay must be >= to 00:00:00. | ||
The ShouldRetry field is required. | ||
The RetryDelayGenerator field is required. | ||
The OnRetry field is required. | ||
"""); | ||
} | ||
|
||
[Fact] | ||
public async Task AsNonGenericOptions_Ok() | ||
{ | ||
var called = false; | ||
var options = new RetryStrategyOptions<int> | ||
{ | ||
BackoffType = RetryBackoffType.Constant, | ||
BaseDelay = TimeSpan.FromMilliseconds(555), | ||
RetryCount = 7, | ||
StrategyName = "my-name", | ||
StrategyType = "my-type", | ||
}; | ||
|
||
options.ShouldRetry.HandleResult(999); | ||
options.OnRetry.Register(() => called = true); | ||
options.RetryDelayGenerator.SetGenerator((_, _) => TimeSpan.FromSeconds(123)); | ||
var nonGenericOptions = options.AsNonGenericOptions(); | ||
|
||
nonGenericOptions.BackoffType.Should().Be(RetryBackoffType.Constant); | ||
nonGenericOptions.BaseDelay.Should().Be(TimeSpan.FromMilliseconds(555)); | ||
nonGenericOptions.RetryCount.Should().Be(7); | ||
nonGenericOptions.StrategyName.Should().Be("my-name"); | ||
nonGenericOptions.StrategyType.Should().Be("my-type"); | ||
|
||
(await nonGenericOptions.ShouldRetry.CreateHandler()!.ShouldHandleAsync(new Outcome<int>(999), default)).Should().BeTrue(); | ||
await nonGenericOptions.OnRetry.CreateHandler()!.HandleAsync(new Outcome<int>(999), default); | ||
called.Should().BeTrue(); | ||
var args = new RetryDelayArguments(ResilienceContext.Get(), 2, TimeSpan.FromMinutes(1)); | ||
(await nonGenericOptions.RetryDelayGenerator.CreateHandler(default, _ => true)!.GenerateAsync(new Outcome<int>(999), args)).Should().Be(TimeSpan.FromSeconds(123)); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using Polly.Strategy; | ||
|
||
namespace Polly.Core.Tests.Timeout; | ||
|
||
public class NoOutcomeGeneratorTests | ||
{ | ||
[Fact] | ||
public void Default_EnsureCorrectValue() | ||
{ | ||
var generator = new NoOutcomeGenerator<TestArguments, TimeSpan>(); | ||
|
||
generator.CreateHandler(default, _ => true).Should().BeNull(); | ||
} | ||
|
||
[Fact] | ||
public async Task SetGenerator_Callback_Ok() | ||
{ | ||
var generator = new NoOutcomeGenerator<TestArguments, TimeSpan>(); | ||
var delay = TimeSpan.FromSeconds(1); | ||
|
||
generator.SetGenerator(args => delay); | ||
|
||
var actualDelay = await generator.CreateHandler(default, _ => true)!(new TestArguments()); | ||
|
||
actualDelay.Should().Be(delay); | ||
} | ||
|
||
[Fact] | ||
public async Task SetGenerator_AsyncCallback_Ok() | ||
{ | ||
var generator = new NoOutcomeGenerator<TestArguments, TimeSpan>(); | ||
var delay = TimeSpan.FromSeconds(1); | ||
|
||
generator.SetGenerator(args => new ValueTask<TimeSpan>(delay)); | ||
|
||
var actualDelay = await generator.CreateHandler(default, _ => true)!(new TestArguments()); | ||
|
||
actualDelay.Should().Be(delay); | ||
} | ||
|
||
[Fact] | ||
public async Task CreateHandler_EnsureValidation() | ||
{ | ||
var generator = new NoOutcomeGenerator<TestArguments, TimeSpan>(); | ||
var delay = TimeSpan.FromSeconds(1); | ||
|
||
generator.SetGenerator(args => new ValueTask<TimeSpan>(delay)); | ||
|
||
var actualDelay = await generator.CreateHandler(TimeSpan.FromMilliseconds(123), _ => false)!(new TestArguments()); | ||
|
||
actualDelay.Should().Be(TimeSpan.FromMilliseconds(123)); | ||
} | ||
|
||
[Fact] | ||
public void ArgValidation_EnsureThrows() | ||
{ | ||
var generator = new NoOutcomeGenerator<TestArguments, TimeSpan>(); | ||
|
||
Assert.Throws<ArgumentNullException>(() => generator.SetGenerator((Func<TestArguments, TimeSpan>)null!)); | ||
Assert.Throws<ArgumentNullException>(() => generator.SetGenerator((Func<TestArguments, ValueTask<TimeSpan>>)null!)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.