-
Notifications
You must be signed in to change notification settings - Fork 296
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
Make CircuitBreaker delay time mockable for testing #357
Comments
Yes, I can imagine mocking |
Something like the Clock abstraction might be useful, but it would mean some refactoring, because I don't think that it works at nanosecond granularity. (Mind you, I don't think one needs or wants true nanosecond granularity in cases where Failsafe is used, but the code currently is written that way.) |
Thank you for the quick response, @jhalterman. If you did want to provide a Clock abstraction for unit testing, that would be even better. But a quicker fix might be to just use |
I'm not sure I'd document mocking |
I wasn't thinking about mocking Here's an example of what I'd like to do in a test:
Ideally, I'd like the
|
We would love to be able to test these sorts of behavior as well. We don't want to have to mock static classes - this gets ugly fast. It'd be great to plug-in a user supplied |
I'm using
CircuitBreaker
in its Standalone Usage. MyCircuitBreaker
is configuredwithFailureRateThreshold(50, 2, Duration.ofSeconds(30))
to open when 50% of requests over a 30 second period fail.I'd like to be able to unit test around the
CircuitBreaker
behavior so that I know that the closed / open / half-open behaviors are correct.The challenge appears to be that
OpenState
usesSystem.nanoTime()
to calculate the delay. This is very difficult to mock. I started going down the road of using AspectJ, but this is a mixed Kotlin/Java Android project, and the best library I found to implement AspectJ is no longer maintained because Android Gradle Plugin 8.0 will no longer support bytecode transformation.If you switched to use something like
java.time.Instant.now()
, that would make test mocking much easier.I see that in your own tests you just configure very short delay times and use
Thread.sleep()
. That's okay, but it's not nearly as flexible as being able to mock time itself.The text was updated successfully, but these errors were encountered: