2017-04-05 16:27:13 +08:00
|
|
|
#include "gmock/gmock.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
#include <atomic>
|
2017-04-07 17:33:24 +08:00
|
|
|
#include "utils/scheduler.hpp"
|
2017-04-05 16:27:13 +08:00
|
|
|
|
|
|
|
/**
|
2017-04-07 17:33:24 +08:00
|
|
|
* Scheduler runs every 2 seconds and increases one variable. Test thread
|
2017-04-05 16:27:13 +08:00
|
|
|
* increases other variable. Scheduler checks if variables have the same
|
|
|
|
* value.
|
|
|
|
*/
|
|
|
|
TEST(Scheduler, TestFunctionExecuting) {
|
2017-10-09 20:44:11 +08:00
|
|
|
std::atomic<int> x{0};
|
|
|
|
std::function<void()> func{[&x]() { ++x; }};
|
2018-04-22 14:31:09 +08:00
|
|
|
utils::Scheduler scheduler;
|
2018-02-23 21:35:16 +08:00
|
|
|
scheduler.Run("Test", std::chrono::seconds(1), func);
|
2017-04-05 16:27:13 +08:00
|
|
|
|
2017-10-09 20:44:11 +08:00
|
|
|
EXPECT_EQ(x, 0);
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(900));
|
|
|
|
EXPECT_EQ(x, 0);
|
|
|
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
|
|
|
EXPECT_EQ(x, 1);
|
|
|
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
|
|
|
|
EXPECT_EQ(x, 3);
|
2017-04-05 16:27:13 +08:00
|
|
|
|
2017-04-07 17:33:24 +08:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
|
|
scheduler.Stop();
|
2017-10-09 20:44:11 +08:00
|
|
|
EXPECT_EQ(x, 3);
|
2017-04-05 16:27:13 +08:00
|
|
|
}
|