1 #include <boost/test/unit_test.hpp>
2 #include <zypp/zyppng/base/EventDispatcher>
3 #include <zypp/zyppng/base/Timer>
4 #include <zypp/base/Exception.h>
8 BOOST_AUTO_TEST_CASE(eventloop)
10 zyppng::EventDispatcher::Ptr loop = zyppng::EventDispatcher::createMain();
12 //we should hit that timer first
13 zyppng::Timer::Ptr t1 = zyppng::Timer::create();
16 zyppng::Timer::Ptr t2 = zyppng::Timer::create();
18 //this in theory would be the 2nd hit but is cleaned up by t1 before that
19 zyppng::Timer::Ptr t3 = zyppng::Timer::create();
21 //this is a singleshot timer, it should just hit once
22 zyppng::Timer::Ptr t4 = zyppng::Timer::create();
28 int executedIdleOnce = 0;
30 t1->sigExpired().connect( [ &hitT1, &t3 ]( zyppng::Timer &t) {
35 t2->sigExpired().connect( [ &hitT1, &hitT2, &loop ]( zyppng::Timer & ){
36 BOOST_REQUIRE_GT( hitT1, 0 );
42 t3->sigExpired().connect( [ ]( zyppng::Timer & ){
46 t4->setSingleShot( true );
47 t4->sigExpired().connect( [ &hitT4 ]( zyppng::Timer &t ){
50 //timer deviation should not be too big, can only be tested on a singleShot timer
51 // ma: disabled as OBS builder have quite big deviation >15
52 // BOOST_REQUIRE_LE( t.now() - t.expires(), 3 );
55 //convenience function to execute a function later
56 zyppng::EventDispatcher::invokeOnIdle( [ &executedIdle](){ executedIdle++; return ( executedIdle < 2 );} );
58 zyppng::EventDispatcher::invokeOnIdle( [ &executedIdleOnce ](){ executedIdleOnce++; return false;} );
67 BOOST_REQUIRE_EQUAL( executedIdle, 2 );
68 BOOST_REQUIRE_EQUAL( executedIdleOnce, 1 );
69 BOOST_REQUIRE_GE ( hitT1, 3 );
70 BOOST_REQUIRE_EQUAL( hitT2, 3 );
71 BOOST_REQUIRE_EQUAL( hitT4, 1 );
76 BOOST_REQUIRE_EQUAL( loop->runningTimers(), 0 );
79 BOOST_AUTO_TEST_CASE(createTimerWithoutEV)
81 BOOST_CHECK_THROW( zyppng::Timer::create(), zypp::Exception);
84 BOOST_AUTO_TEST_CASE(checkcleanup)
86 zyppng::EventDispatcher::Ptr loop = zyppng::EventDispatcher::createMain();
87 BOOST_REQUIRE_EQUAL( loop.get(), zyppng::EventDispatcher::instance().get() );
91 BOOST_REQUIRE_EQUAL( static_cast<zyppng::EventDispatcher *>(nullptr), zyppng::EventDispatcher::instance().get() );
95 zyppng::EventDispatcher::createMain();
96 BOOST_REQUIRE_EQUAL( static_cast<zyppng::EventDispatcher *>(nullptr), zyppng::EventDispatcher::instance().get() );
99 //only one instance per thread
100 loop = zyppng::EventDispatcher::createMain();
101 BOOST_REQUIRE_THROW( zyppng::EventDispatcher::createMain(), zypp::Exception );
103 BOOST_REQUIRE_EQUAL( loop.get(), zyppng::EventDispatcher::instance().get() );