memgraph/tests/concurrent/timer.cpp

61 lines
1.3 KiB
C++
Raw Normal View History

#include <iostream>
#include <chrono>
2016-12-22 22:51:16 +08:00
#include "gtest/gtest.h"
#include "logging/default.cpp"
#include "utils/timer/timer.hpp"
2016-12-22 22:51:16 +08:00
#include "utils/assert.hpp"
using namespace std::chrono_literals;
2016-12-22 22:51:16 +08:00
/**
* Creates a test timer which will log timeout message at the timeout event.
*
* @param counter how many time units the timer has to wait
*
* @return shared pointer to a timer
*/
Timer::sptr create_test_timer(int64_t counter)
{
return std::make_shared<Timer>(
counter, [](){ logging::info("Timer timeout"); }
);
}
2016-12-22 22:51:16 +08:00
TEST(TimerSchedulerTest, TimerSchedulerExecution)
{
2016-12-22 22:51:16 +08:00
// initialize the timer
TimerScheduler<TimerSet, std::chrono::seconds> timer_scheduler;
2016-12-22 22:51:16 +08:00
// run the timer
timer_scheduler.run();
2016-12-22 22:51:16 +08:00
// add a couple of test timers
for (int64_t i = 1; i <= 3; ++i) {
timer_scheduler.add(create_test_timer(i));
}
2016-12-22 22:51:16 +08:00
// wait for that timers
2016-06-05 20:30:40 +08:00
std::this_thread::sleep_for(4s);
2016-12-22 22:51:16 +08:00
ASSERT_EQ(timer_scheduler.size(), 0);
// add another test timer
timer_scheduler.add(create_test_timer(1));
2016-12-22 22:51:16 +08:00
// wait for another timer
2016-06-05 20:30:40 +08:00
std::this_thread::sleep_for(2s);
2016-12-22 22:51:16 +08:00
// the test is done
2016-06-05 20:30:40 +08:00
timer_scheduler.stop();
2016-12-22 22:51:16 +08:00
ASSERT_EQ(timer_scheduler.size(), 0);
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}