question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unit testing retry policies with timeout intervals

See original GitHub issue

Currently I don’t see a way to unit test a retry policy if you use the time-based retry policy.

Let’s say you use the following approach, and this code below is part of your method that does a few more things than executing the policy itself.

Some code here…

Policy
  .Handle<DivideByZeroException>()
  .WaitAndRetry(new[]
  {
    TimeSpan.FromSeconds(30),
    TimeSpan.FromSeconds(60),
    TimeSpan.FromSeconds(120)
  }, (exception, timeSpan) => {
    // do something    
  }); 

Some code here…

How would I test what happens after we have re-tried 3 times? I don’t want to wait more than one minute in my tests. Thanks for your suggestions.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
jiimahocommented, Sep 19, 2016

Hi @reisenberger and thanks for your quick reply. I actually just found what I was looking for in Polly itself!

SystemClock.Sleep allows me to mock the internal timer for Polly, which causes the sleeps to really not sleep.

I closed the my issue as it’s not relevant anymore.

0reactions
reisenbergercommented, Aug 1, 2018

Thanks for that @rog1039 . The ability to manipulate Polly’s abstracted, ambient-context SystemClock is intended to provide exactly this: you can manipulate time so that tests which would otherwise incur a time delay, don’t. See the many tests within the existing codebase which do this.

Let us know how you get on with that - or if you can envisage ways it could be improved (I can envisage some - as ever, with trade-offs)

EDIT: Improved the Unit-testing wiki to highlight this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unit testing async pessimistic TimeoutPolicy · Issue #623
I am trying to test whether my pessimistic timeout policies work as expected. For this, I tried to mock the function on which...
Read more >
Polly verify how many times httpclient timed out/retried in a ...
1 Answer 1 ... The issue is that the test code posted does not return a cancellable Task, at the value mocked for...
Read more >
Unit testing HTTP retry strategies | by Madhur
Unit testing HTTP retry strategies ... For example, a very naive HTTP retry strategy to be to retry 3 times at an interval...
Read more >
Unit testing Rx methods Timeout & Retry with moq
So I wanted to put Timeout & Retry under test, to do this we use moq for mocking out our dependencies, we also...
Read more >
Timeouts, retries and backoff with jitter
The preferred solution that we use in Amazon is a backoff. Instead of retrying immediately and aggressively, the client waits some amount of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found