Code Monkey home page Code Monkey logo

Comments (5)

peter-csala avatar peter-csala commented on September 28, 2024 2

I was playing a bit with your MRE and here are my observations:

  1. 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));
  1. 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;
}
  1. Rewriting the previous sample code to do not utilizeCancellationTokenSource: 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.

martintmk avatar martintmk commented on September 28, 2024 2

Thanks @peter-csala for confirming. I think we should close this in favor of dotnet/extensions#4912

from polly.

rcollette avatar rcollette commented on September 28, 2024 2

@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.

rcollette avatar rcollette commented on September 28, 2024

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.

martintmk avatar martintmk commented on September 28, 2024

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:

await _timeProvider.DelayAsync(delay, context).ConfigureAwait(context.ContinueOnCapturedContext);

Which in turn calls:

public static Task DelayAsync(this TimeProvider timeProvider, TimeSpan delay, ResilienceContext context)

I suspect the problem might be in FakeTimeProvider in https://github.com/dotnet/extensions

from polly.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.