2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2013-04-19 12:58:16 +02:00
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/timer.hpp"
|
|
|
|
#include "base/utility.hpp"
|
|
|
|
#include "base/application.hpp"
|
2016-09-07 08:20:51 +02:00
|
|
|
#include <BoostTestTargetConfig.h>
|
2013-04-19 12:58:16 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2014-11-12 09:33:13 +01:00
|
|
|
BOOST_AUTO_TEST_SUITE(base_timer)
|
2013-04-19 12:58:16 +02:00
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(construct)
|
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
Timer::Ptr timer = new Timer();
|
2013-04-19 12:58:16 +02:00
|
|
|
BOOST_CHECK(timer);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(interval)
|
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
Timer::Ptr timer = new Timer();
|
2013-04-19 12:58:16 +02:00
|
|
|
timer->SetInterval(1.5);
|
|
|
|
BOOST_CHECK(timer->GetInterval() == 1.5);
|
|
|
|
}
|
|
|
|
|
2018-11-08 15:03:41 +01:00
|
|
|
int counter = 0;
|
|
|
|
|
2019-04-17 22:31:42 +02:00
|
|
|
static void Callback(const Timer * const&)
|
2013-04-19 12:58:16 +02:00
|
|
|
{
|
2018-11-08 15:03:41 +01:00
|
|
|
counter++;
|
2013-04-19 12:58:16 +02:00
|
|
|
}
|
|
|
|
|
2014-05-09 19:26:31 +02:00
|
|
|
BOOST_AUTO_TEST_CASE(invoke)
|
2013-04-19 12:58:16 +02:00
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
Timer::Ptr timer = new Timer();
|
2018-11-08 15:03:41 +01:00
|
|
|
timer->OnTimerExpired.connect(&Callback);
|
2013-04-19 12:58:16 +02:00
|
|
|
timer->SetInterval(1);
|
|
|
|
|
|
|
|
counter = 0;
|
|
|
|
timer->Start();
|
|
|
|
Utility::Sleep(5.5);
|
|
|
|
timer->Stop();
|
|
|
|
|
|
|
|
BOOST_CHECK(counter >= 4 && counter <= 6);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(scope)
|
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
Timer::Ptr timer = new Timer();
|
2018-11-08 15:03:41 +01:00
|
|
|
timer->OnTimerExpired.connect(&Callback);
|
2013-04-19 12:58:16 +02:00
|
|
|
timer->SetInterval(1);
|
|
|
|
|
|
|
|
counter = 0;
|
|
|
|
timer->Start();
|
|
|
|
Utility::Sleep(5.5);
|
|
|
|
timer.reset();
|
|
|
|
Utility::Sleep(5.5);
|
|
|
|
|
|
|
|
BOOST_CHECK(counter >= 4 && counter <= 6);
|
2014-05-09 19:26:31 +02:00
|
|
|
}
|
2013-04-19 12:58:16 +02:00
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|