fd19b60f508116ddb2ad01d7f0cf7bafd0bbe1c4
[platform/upstream/libzypp.git] / tests / zyppng / EventLoop_test.cc
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>
5
6 #include <iostream>
7
8 BOOST_AUTO_TEST_CASE(eventloop)
9 {
10   zyppng::EventDispatcher::Ptr loop = zyppng::EventDispatcher::createMain();
11
12   //we should hit that timer first
13   zyppng::Timer::Ptr t1 = zyppng::Timer::create();
14
15   //second hit
16   zyppng::Timer::Ptr t2 = zyppng::Timer::create();
17
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();
20
21   //this is a singleshot timer, it should just hit once
22   zyppng::Timer::Ptr t4 = zyppng::Timer::create();
23
24   int hitT1 = 0;
25   int hitT2 = 0;
26   int hitT4 = 0;
27   int executedIdle = 0;
28   int executedIdleOnce = 0;
29
30   t1->sigExpired().connect( [ &hitT1, &t3 ]( zyppng::Timer &t) {
31     hitT1++;
32     t3.reset();
33   });
34
35   t2->sigExpired().connect( [ &hitT1, &hitT2, &loop ]( zyppng::Timer & ){
36     BOOST_REQUIRE_GT( hitT1, 0 );
37     hitT2++;
38     if ( hitT2 >= 3 )
39       loop->quit();
40   });
41
42   t3->sigExpired().connect( [ ]( zyppng::Timer & ){
43     BOOST_TEST( false );
44   });
45
46   t4->setSingleShot( true );
47   t4->sigExpired().connect( [ &hitT4 ]( zyppng::Timer &t ){
48     hitT4++;
49
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 );
52   });
53
54   //convenience function to execute a function later
55   zyppng::EventDispatcher::invokeOnIdle( [ &executedIdle](){ executedIdle++; return ( executedIdle < 2 );} );
56
57   zyppng::EventDispatcher::invokeOnIdle( [ &executedIdleOnce ](){ executedIdleOnce++; return false;} );
58
59   t1->start( 10 );
60   t3->start( 13 );
61   t2->start( 15 );
62   t4->start( 2 );
63
64   loop->run();
65
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 );
71
72   t1->stop();
73   t2->stop();
74
75   BOOST_REQUIRE_EQUAL( loop->runningTimers(), 0 );
76 }
77
78 BOOST_AUTO_TEST_CASE(createTimerWithoutEV)
79 {
80   BOOST_CHECK_THROW( zyppng::Timer::create(), zypp::Exception);
81 }
82
83 BOOST_AUTO_TEST_CASE(checkcleanup)
84 {
85   zyppng::EventDispatcher::Ptr loop = zyppng::EventDispatcher::createMain();
86   BOOST_REQUIRE_EQUAL( loop.get(), zyppng::EventDispatcher::instance().get() );
87
88   //explicit cleanup
89   loop.reset();
90   BOOST_REQUIRE_EQUAL( static_cast<zyppng::EventDispatcher *>(nullptr), zyppng::EventDispatcher::instance().get() );
91
92   //implicit cleanup
93   {
94     zyppng::EventDispatcher::createMain();
95     BOOST_REQUIRE_EQUAL( static_cast<zyppng::EventDispatcher *>(nullptr), zyppng::EventDispatcher::instance().get() );
96   }
97
98   //only one instance per thread
99   loop = zyppng::EventDispatcher::createMain();
100   BOOST_REQUIRE_THROW( zyppng::EventDispatcher::createMain(), zypp::Exception );
101
102   BOOST_REQUIRE_EQUAL( loop.get(), zyppng::EventDispatcher::instance().get() );
103 }