How to use assertEvent() methods?

22 views
Skip to first unread message

Peter Kirschner

unread,
Aug 13, 2014, 12:53:37 AM8/13/14
to knowhowlab-...@googlegroups.com
What is the correct approach to monitor an events inside a testcase?

variant A:
fireEvent(topicX)
assertEvent(topicX, 500, TimeUnit.MILLISECONDS)

fireEvent could then be already happened before assertEvent is ready and the assertion would run into the timeout

variant B:
assertEvent(topicX, 500, TimeUnit.MILLISECONDS)
fireEvent(topicX)

with this order the testcase would assure that the OSGi EventAdmin is delivering the fireEvent method within the specified timeout of 500 ms.

What would be a meaningful timeout for this variant B?

An other idea:

How about registering an EventListener for the eventAssertion upfront and having a method assertEvent(topic, timeoutPast, timeoutFuture).
This could be the using the registered EventListener to identify if such an event has happened in the timeframe between timeoutPast->now->timeoutFuture ?

Dmytro Pishchukhin

unread,
Aug 13, 2014, 3:54:30 AM8/13/14
to knowhowlab-...@googlegroups.com
Hi Peter,

the variant B is correct. About timeout: the latest version of the tool supports only one timeout. Could you add your requirement as an issue to the github project with more details of your test case, please? 

Dave Humeniuk

unread,
Nov 10, 2014, 2:30:42 PM11/10/14
to knowhowlab-...@googlegroups.com
I don't see how either variant would work, won't variant B wait 500 MS for the event and fail before the event is ever called on to fire?

Dave Humeniuk

unread,
Nov 10, 2014, 2:38:31 PM11/10/14
to knowhowlab-...@googlegroups.com
In our own code we have done something like:

EventListener listener = new EventListener(topicX, filter);

fireEvent
(topicX);

listener
.assertEvent(500);

This will starting listening for the event immediately when EventListener is constructed and then will wait at most 500 ms once the assertion method is called for the event to be recieved.

Dave Humeniuk

unread,
Nov 10, 2014, 2:56:54 PM11/10/14
to knowhowlab-...@googlegroups.com

Dmytro Pishchukhin

unread,
Nov 11, 2014, 1:27:31 AM11/11/14
to knowhowlab-...@googlegroups.com
Thanks, Dave.

There are several points regarding testing of the events:

1. Which type of event firing is used POST (async) or SEND (sync). 
2. Usually, fireEvent and assertEvent are in the separate Threads.
3. Class EventAdminUtils has several methods that can help you to delay event firing and run it in the separate thread.
e.g. 
@Test
public void test_Post_Event() {
    // post event in 200ms
    postEvent(getBundleContext(), KNOWHOWLAB_TOPICS_TEST, 200);

    // wait for event in defined topic
    assertEvent(KNOWHOWLAB_TOPICS_TEST, 500, TimeUnit.MILLISECONDS);
}
postEvent() fires an event in the separate Thread and after 200ms delay. 

Dmytro Pishchukhin

unread,
Nov 11, 2014, 1:31:12 AM11/11/14
to knowhowlab-...@googlegroups.com
Peter,

sorry for the wrong answer (summer and vacation time). The best approach I use for event testing is variant A with modification:
postEvent(topicX, 200, TimeUnit.MILLISECONDS) // check API for more details
assertEvent(topicX, 500, TimeUnit.MILLISECONDS)

it fires an event after a small delay in the separate Thread and assetEvent tries to catch it in the test main thread.


On Wednesday, August 13, 2014 6:53:37 AM UTC+2, Peter Kirschner wrote:
Reply all
Reply to author
Forward
0 new messages