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 BOOST_REQUIRE_LE( t.now() - t.expires(), 3 );
54 //convenience function to execute a function later
55 zyppng::EventDispatcher::invokeOnIdle( [ &executedIdle](){ executedIdle++; return ( executedIdle < 2 );} );
57 zyppng::EventDispatcher::invokeOnIdle( [ &executedIdleOnce ](){ executedIdleOnce++; return false;} );
66 BOOST_REQUIRE_EQUAL( executedIdle, 2 );
67 BOOST_REQUIRE_EQUAL( executedIdleOnce, 1 );
68 BOOST_REQUIRE_GE ( hitT1, 3 );
69 BOOST_REQUIRE_EQUAL( hitT2, 3 );
70 BOOST_REQUIRE_EQUAL( hitT4, 1 );
75 BOOST_REQUIRE_EQUAL( loop->runningTimers(), 0 );
78 BOOST_AUTO_TEST_CASE(createTimerWithoutEV)
80 BOOST_CHECK_THROW( zyppng::Timer::create(), zypp::Exception);
83 BOOST_AUTO_TEST_CASE(checkcleanup)
85 zyppng::EventDispatcher::Ptr loop = zyppng::EventDispatcher::createMain();
86 BOOST_REQUIRE_EQUAL( loop.get(), zyppng::EventDispatcher::instance().get() );
90 BOOST_REQUIRE_EQUAL( static_cast<zyppng::EventDispatcher *>(nullptr), zyppng::EventDispatcher::instance().get() );
94 zyppng::EventDispatcher::createMain();
95 BOOST_REQUIRE_EQUAL( static_cast<zyppng::EventDispatcher *>(nullptr), zyppng::EventDispatcher::instance().get() );
98 //only one instance per thread
99 loop = zyppng::EventDispatcher::createMain();
100 BOOST_REQUIRE_THROW( zyppng::EventDispatcher::createMain(), zypp::Exception );
102 BOOST_REQUIRE_EQUAL( loop.get(), zyppng::EventDispatcher::instance().get() );