unit test polly retry c#
To test that the retry policy is invoked, you could make the test setup configure a fake/mock ILog implementation, and (for example) assert that the expected call .Error("Delaying for {delay}ms, ") in your onRetry delegate is made on the fake logger. So heres an example of writing a unit test for test scenario 2. This retry policy means when an exception of type TransientException is caught, it will delay 1 second and then retry. How can I unit test polly retry? The unit test itself does not look so sophisticated as it would be as if you would wrap HttpClient class to implementation of an interface, but this way you get to keep using IHttpClientFactorywhich is more beneficial for your application than adapting it to much to have simpler unit tests. Did the drapes in old theatres actually say "ASBESTOS" on them? This is more general ASP.NET Core support rather than Polly, but some pointers: Options.Create<>() if you want the options to be entirely self-generated by a purely self-contained unit test; or use ConfigurationBuilder to read in external config (eg json settings file) if you want a more integration-style approach which reads in some version of your app's configuration. It will retry up to 3 times. To subscribe to this RSS feed, copy and paste this URL into your RSS reader. But, to allow you to concentrate on delivering your business value rather than reinventing Polly's test wheel, keep in mind that the Polly codebase tests its own operation extensively. It will authenticate first (the authentication service itself will also use Polly) and try to get products. Updated Integration Test method I figured it out. Theres only one instance of Random, and there could be multiple threads making requests concurrently. Writing unit-tests to verify that Polly works can be a very valuable way to explore and understand what Polly does. Setting upIHttpClientFactory is quite easy in ASP.NET Core container setup in Startup.cs. How my code behaves when the policy throws an exception, such as TimeoutRejectionException, BulkheadRejectedException or BrokenCircuitException. Not the answer you're looking for? Polly is a .NET resilience and transient-fault-handling library that allows developers to express policies such as Retry, Circuit Breaker, Timeout, Bulkhead Isolation, Rate-limiting and Fallback in a fluent and thread-safe manner. There is no need for any WebApplicationFactory, IHost, IHostedService or anything from ASP.NET. For more information, see How to: Use CTest in Visual Studio. ErrorProneCode.cs is the unreliable class that I will mock and pass mocked policies into. In this section, Ill only try to handle one: the Too Many Requests error response (429). In your test code, inject an equivalent policy that doesn't do any waiting, eg Retry (3) // etc Extract static SystemClock to interface
Chen Family Murders Virginia,
How To Deal With Conflict Avoidant Personality,
Stober Farms Out Of Business,
Educational Sustainability Mobilization Inc,
Articles U
unit test polly retry c#