]> granicus.if.org Git - icinga2/blob - test/base-timer.cpp
Disallow side-effect-free r-value expressions in expression lists
[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.hpp"
21 #include "base/utility.hpp"
22 #include "base/application.hpp"
23 #include <boost/test/unit_test.hpp>
24 #include <boost/foreach.hpp>
25
26 using namespace icinga;
27
28 BOOST_AUTO_TEST_SUITE(base_timer)
29
30 BOOST_AUTO_TEST_CASE(construct)
31 {
32         Timer::Ptr timer = new Timer();
33         BOOST_CHECK(timer);
34 }
35
36 BOOST_AUTO_TEST_CASE(interval)
37 {
38         Timer::Ptr timer = new Timer();
39         timer->SetInterval(1.5);
40         BOOST_CHECK(timer->GetInterval() == 1.5);
41 }
42
43 static void Callback(int *counter)
44 {
45         (*counter)++;
46 }
47
48 BOOST_AUTO_TEST_CASE(invoke)
49 {
50         int counter;
51         Timer::Ptr timer = new Timer();
52         timer->OnTimerExpired.connect(boost::bind(&Callback, &counter));
53         timer->SetInterval(1);
54
55         counter = 0;
56         timer->Start();
57         Utility::Sleep(5.5);
58         timer->Stop();
59
60         BOOST_CHECK(counter >= 4 && counter <= 6);
61 }
62
63 BOOST_AUTO_TEST_CASE(scope)
64 {
65         int counter;
66         Timer::Ptr timer = new Timer();
67         timer->OnTimerExpired.connect(boost::bind(&Callback, &counter));
68         timer->SetInterval(1);
69
70         counter = 0;
71         timer->Start();
72         Utility::Sleep(5.5);
73         timer.reset();
74         Utility::Sleep(5.5);
75
76         BOOST_CHECK(counter >= 4 && counter <= 6);
77 }
78
79 BOOST_AUTO_TEST_SUITE_END()