Tutorial A - Testing OpTimer asynchronously, Introducing async
Asynchronous tests are just like normal tests, but you have to
manually advance the testsuite by calling either ST_passed or
ST_failed.
If you don't call either, the testsuite will halt, there is no timeout
involved.
group "tutorial_a";
include "modules/hardcore/timer/optimer.h";
global {
// Variable used in the test below.
OpTimer *m_timer;
// Create a timer listener class, which, on a timeout, simply calls ST_passed.
//
// There is really no way to get this test to fail if a timer could
// be allocated, since it's sort of hard to create a platform
// independent timeout without using optimer
class myTimerListener : public OpTimerListener
{
void OnTimeOut( OpTimer *timer ) {
// All OK.
ST_passed();
}
};
}
setup
{
m_timer = 0;
}
exit
{
delete m_timer;
}
test( "OpTimer" )
async;
{
m_timer = new OpTimer();
if (!m_timer)
{
// Note that we have to manually call ST_failed and ST_ok in asynchronous tests.
ST_failed( "Failed to create timer" );
}
else
{
myTimerListener *notifier = new myTimerListener;
if( notifier )
{
// The listener in the global section will call ST_passed().
m_timer->SetTimerListener( notifier );
m_timer->Start( 3 );
}
else
ST_failed( "Failed to create timer listener" );
}
}
Output from this test:
=========================================================================
/home/ph/src/opera-oom2/modules/selftest/documentation/tutorial/tutoriala.ot(5): tutorial_a
=========================================================================
OpTimer ....................................................... Passed