memgraph/poc/measure_time.cpp

58 lines
1.3 KiB
C++
Raw Normal View History

#include <chrono>
#include <iostream>
#include <glog/logging.h>
#include <gtest/gtest.h>
2016-12-22 22:51:16 +08:00
#include "timer.hpp"
using namespace std::chrono_literals;
using namespace utils;
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,
[]() { DLOG(INFO) << "Timer timeout"; });
}
TEST(TimerSchedulerTest, TimerSchedulerExecution) {
// initialize the timer
TimerScheduler<TimerSet, std::chrono::seconds> timer_scheduler;
// 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
std::this_thread::sleep_for(4s);
2016-12-22 22:51:16 +08:00
ASSERT_EQ(timer_scheduler.size(), 0);
2016-12-22 22:51:16 +08:00
// add another test timer
timer_scheduler.add(create_test_timer(1));
2016-12-22 22:51:16 +08:00
// wait for another timer
std::this_thread::sleep_for(2s);
2016-12-22 22:51:16 +08:00
// the test is done
timer_scheduler.stop();
2016-12-22 22:51:16 +08:00
ASSERT_EQ(timer_scheduler.size(), 0);
2016-12-22 22:51:16 +08:00
}
int main(int argc, char **argv) {
::google::InitGoogleLogging(argv[0]);
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}