]> granicus.if.org Git - icinga2/blob - test/base-timer.cpp
Merge pull request #5861 from Icinga/fix/invalid-memory-access
[icinga2] / test / base-timer.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/)  *
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 <BoostTestTargetConfig.h>
24
25 using namespace icinga;
26
27 BOOST_AUTO_TEST_SUITE(base_timer)
28
29 BOOST_AUTO_TEST_CASE(construct)
30 {
31         Timer::Ptr timer = new Timer();
32         BOOST_CHECK(timer);
33 }
34
35 BOOST_AUTO_TEST_CASE(interval)
36 {
37         Timer::Ptr timer = new Timer();
38         timer->SetInterval(1.5);
39         BOOST_CHECK(timer->GetInterval() == 1.5);
40 }
41
42 static void Callback(int *counter)
43 {
44         (*counter)++;
45 }
46
47 BOOST_AUTO_TEST_CASE(invoke)
48 {
49         int counter;
50         Timer::Ptr timer = new Timer();
51         timer->OnTimerExpired.connect(std::bind(&Callback, &counter));
52         timer->SetInterval(1);
53
54         counter = 0;
55         timer->Start();
56         Utility::Sleep(5.5);
57         timer->Stop();
58
59         BOOST_CHECK(counter >= 4 && counter <= 6);
60 }
61
62 BOOST_AUTO_TEST_CASE(scope)
63 {
64         int counter;
65         Timer::Ptr timer = new Timer();
66         timer->OnTimerExpired.connect(std::bind(&Callback, &counter));
67         timer->SetInterval(1);
68
69         counter = 0;
70         timer->Start();
71         Utility::Sleep(5.5);
72         timer.reset();
73         Utility::Sleep(5.5);
74
75         BOOST_CHECK(counter >= 4 && counter <= 6);
76 }
77
78 BOOST_AUTO_TEST_SUITE_END()