]> granicus.if.org Git - icinga2/blob - test/base-timer.cpp
Update INSTALL file.
[icinga2] / test / base-timer.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org)    *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #include "base/timer.h"
21 #include "base/utility.h"
22 #include "base/application.h"
23 #include <boost/test/unit_test.hpp>
24 #include <boost/foreach.hpp>
25
26 using namespace icinga;
27
28 struct TimerFixture
29 {
30         TimerFixture(void)
31         {
32                 Timer::Initialize();
33         }
34
35         ~TimerFixture(void)
36         {
37                 Timer::Uninitialize();
38         }
39 };
40
41 BOOST_FIXTURE_TEST_SUITE(base_timer, TimerFixture)
42
43 BOOST_AUTO_TEST_CASE(construct)
44 {
45         Timer::Ptr timer = make_shared<Timer>();
46         BOOST_CHECK(timer);
47 }
48
49 BOOST_AUTO_TEST_CASE(interval)
50 {
51         Timer::Ptr timer = make_shared<Timer>();
52         timer->SetInterval(1.5);
53         BOOST_CHECK(timer->GetInterval() == 1.5);
54 }
55
56 static void Callback(int *counter)
57 {
58         (*counter)++;
59 }
60
61 BOOST_AUTO_TEST_CASE(invoke)
62 {
63         int counter;
64         Timer::Ptr timer = make_shared<Timer>();
65         timer->OnTimerExpired.connect(boost::bind(&Callback, &counter));
66         timer->SetInterval(1);
67
68         counter = 0;
69         timer->Start();
70         Utility::Sleep(5.5);
71         timer->Stop();
72
73         BOOST_CHECK(counter >= 4 && counter <= 6);
74 }
75
76 BOOST_AUTO_TEST_CASE(scope)
77 {
78         int counter;
79         Timer::Ptr timer = make_shared<Timer>();
80         timer->OnTimerExpired.connect(boost::bind(&Callback, &counter));
81         timer->SetInterval(1);
82
83         counter = 0;
84         timer->Start();
85         Utility::Sleep(5.5);
86         timer.reset();
87         Utility::Sleep(5.5);
88
89         BOOST_CHECK(counter >= 4 && counter <= 6);
90 }
91
92 BOOST_AUTO_TEST_SUITE_END()