Comments (5)
I was playing a bit with your MRE and here are my observations:
- Changing the
_timeProvider.Advance
parameter or the number of calls: your test still fails
// Option A
_timeProvider.Advance(TimeSpan.FromMilliseconds(1));
// Option B
_timeProvider.Advance(TimeSpan.FromMilliseconds(1_000_000));
// Option C
for(int i=0; i < 10; i++)
_timeProvider.Advance(TimeSpan.FromMilliseconds(1_000));
//...
// Option Z
while(!result.IsCompleted)
_timeProvider.Advance(TimeSpan.FromMilliseconds(1_000_000));
- Rewriting your
PollyRetry
to use C# built-in features: your test still fails
public async Task<int> RetryWithoutPolly(double delaySeconds, double cancellationSeconds)
{
CancellationTokenSource cts = new(TimeSpan.FromSeconds(cancellationSeconds), timeProvider);
var callback = async () => {
attempts++;
await Task.Delay(TimeSpan.FromSeconds(delaySeconds), timeProvider, cts.Token);
if (attempts < 2)
{
throw new InvalidOperationException();
}
};
attempts = 0;
for(int attempt = 0; attempt < 3; attempt++) //MaxRetryAttempts
{
try
{
await callback();
}
catch(InvalidOperationException) // ShouldHandle
{
// Trigger retry and sleep before new attempt
await Task.Delay(TimeSpan.FromSeconds(attempts), cts.Token); //Delay with Linear backoff
}
}
return attempts;
}
- Rewriting the previous sample code to do not utilize
CancellationTokenSource
: your test still fails
public async Task<int> RetryWithoutPolly(double delaySeconds)
{
var callback = async () => {
attempts++;
await Task.Delay(TimeSpan.FromSeconds(delaySeconds), timeProvider, CancellationToken.None);
if (attempts < 2)
{
throw new InvalidOperationException();
}
};
attempts = 0;
for(int attempt = 0; attempt < 3; attempt++) //MaxRetryAttempts
{
try
{
await callback();
}
catch(InvalidOperationException) // ShouldHandle
{
// Trigger retry and sleep before new attempt
await Task.Delay(TimeSpan.FromSeconds(attempts)); //Delay with Linear backoff
}
}
return attempts;
}
So, I think this bug is not related to Polly.
from polly.
Thanks @peter-csala for confirming. I think we should close this in favor of dotnet/extensions#4912
from polly.
@martintmk @martincostello @peter-csala - Would be helpful to thumb up dotnet/extensions#4912, so that someone doesn't come along and create a duplicate of this issue here since it won't be as visible in the closed state.
The sooner that is resolved the better for all.
from polly.
Asked on Stackoverflow as well.
https://stackoverflow.com/questions/77876331/why-is-task-delay1-necessary-to-advance-clock-when-unit-testing-with-net-time
from polly.
I believe I encountered similar issue where the time won't advance. If you look at our code, there is really nothing special, we are just calling Delay
:
Which in turn calls:
I suspect the problem might be in FakeTimeProvider
in https://github.com/dotnet/extensions
from polly.
Related Issues (20)
- Deprecate all packages which are no longer maintained HOT 5
- Consider making Polly easier to consume from F# and Visual Basic HOT 2
- Fix CI for macos-latest HOT 1
- [Feature request]: Conditionally set log levels for specific resilience policy HOT 1
- [Bug]: README has improper grammar, which makes the meaning unclear. HOT 2
- [Question]: Polly v8 AddPolicyHandler HOT 3
- Is there a way to execute an action AFTER a retry in Polly v8? HOT 7
- [Bug]: System.InvalidOperationException thrown when registering with AddResiliencePipeline in .NET 8 Isolated Function App HOT 3
- [Feature request]: Simplified and faster alternative to ExecuteOutcomeAsync HOT 7
- [Docs]: The main category pages are only available through the breadcrumbs HOT 5
- What's the origin of the name Polly? HOT 4
- [Bug]: DelayBackoffType Exponential Broken Sequence HOT 3
- [Bug]: Undocumented breaking changes from Policy.HandleInner to PredicateBuilder.HandleInner HOT 5
- [Feature request]: Resilience Event occured logs should be configurable as Warning unless it's the final attempt that failed HOT 11
- [Bug]: `GetRetryDelay` goes NEGATIVE after 1025 attempts [High Risk Issue] HOT 6
- [Question]: How to use a CircuitBreaker(basic or advanced) policy with multiple retry policies HOT 17
- [Cleanup the Polly codebase] Warning CA1062 HOT 1
- [Question]: How to get the HttpRequest in a callback supplied to stateful policy (e.g. circuit breaker) HOT 7
- [Feature request]: Simpler way to do basic ShouldHandle delegates for retry HOT 3
- TimeoutStrategyOptions.TimeoutGenerator docs are incorrect HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from polly.