-
-
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.
Introduce unit-tests for issues fixed in v8 (#1157)
- Loading branch information
Showing
11 changed files
with
253 additions
and
24 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
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
43 changes: 43 additions & 0 deletions
43
src/Polly.Core.Tests/Issues/IssuesTests.CircuitBreakerStateSharing_959.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,43 @@ | ||
using Polly.CircuitBreaker; | ||
|
||
namespace Polly.Core.Tests.Issues; | ||
|
||
public partial class IssuesTests | ||
{ | ||
[Fact] | ||
public void CircuitBreakerStateSharing_959() | ||
{ | ||
var options = new AdvancedCircuitBreakerStrategyOptions | ||
{ | ||
FailureThreshold = 1, | ||
MinimumThroughput = 10 | ||
}; | ||
|
||
// handle int results | ||
options.ShouldHandle.HandleResult(-1); | ||
|
||
// handle string results | ||
options.ShouldHandle.HandleResult("error"); | ||
|
||
// create the strategy | ||
var strategy = new ResilienceStrategyBuilder { TimeProvider = TimeProvider.Object }.AddAdvancedCircuitBreaker(options).Build(); | ||
|
||
// now trigger the circuit breaker by evaluating multiple result types | ||
for (int i = 0; i < 5; i++) | ||
{ | ||
strategy.Execute(_ => -1); | ||
strategy.Execute(_ => "error"); | ||
} | ||
|
||
// now the circuit breaker should be open | ||
strategy.Invoking(s => s.Execute(_ => 0)).Should().Throw<BrokenCircuitException>(); | ||
strategy.Invoking(s => s.Execute(_ => "valid-result")).Should().Throw<BrokenCircuitException>(); | ||
|
||
// now wait for recovery | ||
TimeProvider.AdvanceTime(options.BreakDuration); | ||
|
||
// OK, circuit is closed now | ||
strategy.Execute(_ => 0); | ||
strategy.Execute(_ => "valid-result"); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/Polly.Core.Tests/Issues/IssuesTests.FlowingContext_849.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,30 @@ | ||
using Polly.Retry; | ||
|
||
namespace Polly.Core.Tests.Issues; | ||
|
||
public partial class IssuesTests | ||
{ | ||
[Fact] | ||
public void FlowingContext_849() | ||
{ | ||
var contextChecked = false; | ||
var retryOptions = new RetryStrategyOptions(); | ||
|
||
// configure the predicate and use the context | ||
retryOptions.ShouldRetry.HandleResult<int>((_, args) => | ||
{ | ||
// access the context to evaluate the retry | ||
ResilienceContext context = args.Context; | ||
context.Should().NotBeNull(); | ||
contextChecked = true; | ||
return false; | ||
}); | ||
|
||
var strategy = new ResilienceStrategyBuilder().AddRetry(retryOptions).Build(); | ||
|
||
// execute the retry | ||
strategy.Execute(_ => 0); | ||
|
||
contextChecked.Should().BeTrue(); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/Polly.Core.Tests/Issues/IssuesTests.HandleMultipleResults_898.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,56 @@ | ||
using Polly.Retry; | ||
|
||
namespace Polly.Core.Tests.Issues; | ||
|
||
public partial class IssuesTests | ||
{ | ||
[Fact] | ||
public void HandleMultipleResults_898() | ||
{ | ||
var isRetryKey = new ResiliencePropertyKey<bool>("is-retry"); | ||
var options = new RetryStrategyOptions | ||
{ | ||
BackoffType = RetryBackoffType.Constant, | ||
RetryCount = 1, | ||
BaseDelay = TimeSpan.FromMilliseconds(1), | ||
}; | ||
|
||
// now add a callback updates the resilience context with the retry marker | ||
options.OnRetry.Register((_, args) => args.Context.Properties.Set(isRetryKey, true)); | ||
|
||
// handle int results | ||
options.ShouldRetry.HandleResult(-1); | ||
|
||
// handle string results | ||
options.ShouldRetry.HandleResult("error"); | ||
|
||
// create the strategy | ||
var strategy = new ResilienceStrategyBuilder { TimeProvider = TimeProvider.Object }.AddRetry(options).Build(); | ||
|
||
// check that int-based results is retried | ||
bool isRetry = false; | ||
strategy.Execute(_ => | ||
{ | ||
if (isRetry) | ||
{ | ||
return 0; | ||
} | ||
|
||
isRetry = true; | ||
return -1; | ||
}).Should().Be(0); | ||
|
||
// check that string-based results is retried | ||
isRetry = false; | ||
strategy.Execute(_ => | ||
{ | ||
if (isRetry) | ||
{ | ||
return "no-error"; | ||
} | ||
|
||
isRetry = true; | ||
return "error"; | ||
}).Should().Be("no-error"); | ||
} | ||
} |
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,6 @@ | ||
namespace Polly.Core.Tests.Issues; | ||
|
||
public partial class IssuesTests | ||
{ | ||
private FakeTimeProvider TimeProvider { get; } = new FakeTimeProvider().SetupUtcNow().SetupAnyDelay(); | ||
} |
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.