Did you try mocking the listeners? With mockito you can verify if/how many 
times a method has been called with a timeout and also
use argument captors to check the value was as expected. I recently did 
something similar in [1]

[1]: 
https://github.com/opendaylight/mdsal/blob/28e0ef3b11e12112c0acc89ba07f59cc742f7417/dom/mdsal-dom-broker/src/test/java/org/opendaylight/mdsal/dom/broker/ShardedDOMDataTreeProducerMultiShardTest.java#L192-L214

Tomas

From: controller-dev-boun...@lists.opendaylight.org 
[mailto:controller-dev-boun...@lists.opendaylight.org] On Behalf Of Michael 
Vorburger
Sent: Thursday, August 11, 2016 17:47
To: controller-dev; Kitt, Stephen; Alexis de Talhouƫt; Tom Pantelis; Sam Hague; 
Robert Varga
Subject: [controller-dev] How to correctly wait for events to have been 
processed in an AbstractDataBrokerTest?

Ahoi controller-dev and others who may have an idea (please + anyone else 
applicable),
How do you write an AbstractDataBrokerTest and in it wait for a put on the 
DataBroker to have processed by all of its registered DataTreeChangeListener ?
Background with concrete example: The (Abstract)AclServiceTest  in 
https://git.opendaylight.org/gerrit/#/c/42109/ (see [1]) puts two entries into 
the DataBroker, the 2nd of which triggers a DataTreeChangeListener which 
ultimately calls some service. The test asserts that this service got called 
(slightly simplified, but that's the gist of that test).
I've inserted a Thread.sleep(500) after the DataBroker put and before the 
assert, to give the DataTreeChangeListener a chance - I understand runs 
asynchronously, right?
This sleep makes that test work just fine (contrary to without any sleep, when 
it fails, but just sometimes; that's always fun) - but I'm wondering if there 
would be better way to write these kind of tests, without sleep ... but not 
quite clear in my mind yet how.

I've written tests like this before, using e.g. 
https://github.com/awaitility/awaitility, which is just a nice utility to poll 
say every 100ms for max. 2s for a certain condition to be true - but what is 
that condition in this scenario - what API / helper would one use to ask a 
DataBroker "have all of your listeners already processed all of your pending 
puts" ? If this is impossible as is today, would this seem like a fair 
enhancement request - would you consider adding something like this, 
specifically to aid in writing tests?

[1] (Abstract)AclServiceTest is an AbstractDataBrokerTest, even if it doesn't 
extend AbstractDataBrokerTest; ignore that, it's just because of 
https://git.opendaylight.org/gerrit/#/c/43645/
Thanks a lot,
M.
--
Michael Vorburger <vorbur...@redhat.com<mailto:vorbur...@redhat.com>> | IRC: 
vorburger @freenode | ~ = http://vorburger.ch<http://vorburger.ch/>
_______________________________________________
controller-dev mailing list
controller-dev@lists.opendaylight.org
https://lists.opendaylight.org/mailman/listinfo/controller-dev

Reply via email to