-
Notifications
You must be signed in to change notification settings - Fork 606
Expand file tree
/
Copy pathbase-timer.cpp
More file actions
71 lines (56 loc) · 1.87 KB
/
base-timer.cpp
File metadata and controls
71 lines (56 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// SPDX-FileCopyrightText: 2012 Icinga GmbH <https://icinga.com>
// SPDX-License-Identifier: GPL-2.0-or-later
#include "base/timer.hpp"
#include "base/utility.hpp"
#include "base/application.hpp"
#include <BoostTestTargetConfig.h>
/**
* Windows needs a special handicap to keep up with the other OSs.
*/
#ifdef _WIN32
static constexpr double timeMultiplier = 5;
#else //_WIN32
static constexpr double timeMultiplier = 1;
#endif //_WIN32
using namespace icinga;
BOOST_AUTO_TEST_SUITE(base_timer)
BOOST_AUTO_TEST_CASE(construct)
{
Timer::Ptr timer = Timer::Create();
BOOST_CHECK(timer);
}
BOOST_AUTO_TEST_CASE(interval)
{
Timer::Ptr timer = Timer::Create();
timer->SetInterval(1.5);
BOOST_CHECK(timer->GetInterval() == 1.5);
}
BOOST_AUTO_TEST_CASE(invoke)
{
int counter = 0;
Timer::Ptr timer = Timer::Create();
timer->OnTimerExpired.connect([&counter](const Timer* const&) { counter++; });
timer->SetInterval(.2 * timeMultiplier);
timer->Start();
Utility::Sleep(1.1 * timeMultiplier);
timer->Stop();
// At this point, the timer should have fired exactly 5 times (0.5 / 0.1) and the sixth time
// should not have fired yet as we stopped the timer after 0.55 seconds (0.6 would be needed).
BOOST_CHECK_EQUAL(5, counter);
}
BOOST_AUTO_TEST_CASE(scope)
{
int counter = 0;
Timer::Ptr timer = Timer::Create();
timer->OnTimerExpired.connect([&counter](const Timer* const&) { counter++; });
timer->SetInterval(.2 * timeMultiplier);
timer->Start();
Utility::Sleep(1.1 * timeMultiplier);
timer.reset();
Utility::Sleep(.2 * timeMultiplier);
// At this point, the timer should have fired exactly 5 times (0.5 / 0.1) and the sixth time
// should not have fired yet as we destroyed the timer after 0.55 seconds (0.6 would be needed),
// and even if we wait another 0.1 seconds after its destruction, it should not fire again.
BOOST_CHECK_EQUAL(5, counter);
}
BOOST_AUTO_TEST_SUITE_END()