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

timeout policy not working #18

Closed
janeliu567 opened this issue Apr 1, 2019 · 2 comments
Closed

timeout policy not working #18

janeliu567 opened this issue Apr 1, 2019 · 2 comments

Comments

@janeliu567
Copy link

I'm trying out the timeout policy with polly. I expect an exception to be thrown after 1 sec passed because that is the timeout upper limit I set up. But currently, no exception will be thrown and the method LongOperation() returns normally after around 5 secs.

Why does the timeout policy not work in this case?

static void Main(string[] args)
{
    Stopwatch stopwatch = new Stopwatch();
    stopwatch.Start();

    timeoutPolicy().GetAwaiter().GetResult();

    stopwatch.Stop();
    Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed);
    Console.ReadKey();
}

static async Task timeoutPolicy()
{
    AsyncTimeoutPolicy<HttpResponseMessage> timeoutPolicy = Policy.TimeoutAsync<HttpResponseMessage>(1); // setup the timeout limit to be 1 sec

    HttpResponseMessage response = await timeoutPolicy.ExecuteAsync((ct) => LongOperation(), CancellationToken.None);

    Console.WriteLine(response);



}

static Task<HttpResponseMessage> LongOperation()
{
    return Task<HttpResponseMessage>.Factory.StartNew(() =>
    {
        Thread.Sleep(5000); // Sleep 5 seconds
        return new HttpResponseMessage()
        {
            StatusCode = HttpStatusCode.BadRequest
        };

    });
}
@reisenberger
Copy link
Member

@reisenberger
Copy link
Member

Closing because sufficiently answered on StackOverflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants