-
Notifications
You must be signed in to change notification settings - Fork 470
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
Mock/Spy interaction verification withing specific time frame (with timeout) #1661
Comments
Spock assertions are not tested sequentially, unless you use separate
In the meantime I'd change the alternative to move the given:
...
OrderFacade orderFacadeMock = Mock()
def latch = new CountDownLatch(EXPECTED_NUMBER_OF_ORDER_CONFIRMATIONS)
when:
kafkaTemplate(ORDER_CONFIRMED_TOPIC_NAME, ORDER_NUMBER)
latch.await(1000, SECONDS)
then:
//orderConfirmed() should be called when Kafka consumer received a message from a designed topic
EXPECTED_NUMBER_OF_ORDER_CONFIRMATIONS * orderFacadeMock.orderConfirmed(ORDER_NUMBER) >> { latch.countDown() } A downside of the timeout would be that it would require polling, the |
That's a good question. I would probably go for
I don't see a sensible case for that construction. It should finish with success on the first check as "matched".
Probably. We could write tests for know cases and describe it in docs. Having the feature marked as Of course the implementation (and testing) effort might be not worth doing that (there is a workaround ↑↑↑). I've just reported an idea to discuss it and also to see (by 👍 ) if anyone else is interested in that feature (like that guy on SO years ago).
Thanks, it is slightly better.
Sure, but at the cost of decreased readability. |
Is your feature request related to a problem?
Testing asynchronous code (schedules, Kafka/RabbitMQ), an expected interaction might be triggered with some delay (e.g. 60ms). The regular verification construction fails immediately and an artificial wait before it is required (or some other
hacksworkarounds), what reduces the readability.Describe the solution you'd like
An ability to define optional timeout to wait for an interaction before failing the assertion. E.g.
Describe alternatives you've considered
As a workaround, I've been usually using some (thread safe) boolean or counter in the stubbing, to verify the number of interactions. However, it makes code less readable and suppress the "Unmatched invocations" reporting.
Recently @Vampire proposed a nicer variant of that approach with
CountDownLatch
, which eliminates the second limitation, but still requires extra elements and obscures the code.Additional context
withTimeout()
to a number of range) can be applied only within the scope of the mock assertion (not to every number "everywhere").when
block.verify(orderFacadeMock, timeout(200)).orderConfirmed(ORDER_NUMBER)
.The text was updated successfully, but these errors were encountered: