connor4312 / cockatiel

🐦 A resilience and fault-handling library. Supports Backoffs, Retries, Circuit Breakers, Timeouts, Bulkhead Isolation, and Fallbacks.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Stubbing a policy when applied via decorator

stevenveerbeek opened this issue · comments

Hi,

I recently started using cockatiel and it is working great. However, I'm struggling with how to stub retry policies for my unit tests. When running my unit tests, I want to test that my retry policy is executed, but I don't want to actually await the full delays because this will terribly slow down my unit tests.

I'm applying my policies via a decorator in TypeScript, but when trying to stub (using ts-mockito) any values that are consumed by the decorator (to reduce/eliminate the delays), I find that the decorator doesn't use the stubbed values, but the original values instead. On the other hand, when referencing the same variables in the body of the decorated method, the stubbed values are used.

I've also tried using the rewire library to replace my retry policies by zero-delay policies, but without any luck. Again, the decorators seem to use the original files instead of the rewired ones. I'm assuming this has to do with how decorators are implemented in TypeScript, but it does prevent me from properly testing my code that uses cockatiel. Is there any suggested way of dealing with this? Your help will be greatly appreciated. Let me know if you need any further details.

Thanks!

I'm not sure honestly. Decorators in TS are very magical, I think if you subclass your class and override the method with your own, that'll do it. However you can also use fake timers (such as what Sinon or Jest has) to fake the clock which will work with policies.

Personally I don't use the decorator form of the policy because of the magicness.

Thanks for your answer. I did try Sinon's fake timers but was not able to get it working properly. Looked like it was just ignored and my tests kept getting timed out. Might be that I just didn't use it correctly so sorry if I'm asking the obvious, but could you give me some pointers as to how you would use Sinon's fake timers to eliminate delays in tests?