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-04-07 17:33:24 +08:00
|
|
|
std::atomic<int> x{0}, y{0};
|
|
|
|
std::function<void()> func{[&x, &y]() {
|
|
|
|
EXPECT_EQ(y.load(), x.load());
|
|
|
|
x++;
|
2017-04-05 16:27:13 +08:00
|
|
|
}};
|
2017-08-17 15:19:58 +08:00
|
|
|
Scheduler scheduler;
|
2017-04-07 17:33:24 +08:00
|
|
|
scheduler.Run(std::chrono::seconds(1), func);
|
2017-04-05 16:27:13 +08:00
|
|
|
|
2017-04-07 17:33:24 +08:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(980));
|
2017-04-05 16:27:13 +08:00
|
|
|
y++;
|
|
|
|
EXPECT_EQ(x.load(), y.load());
|
|
|
|
|
2017-04-07 17:33:24 +08:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
|
|
scheduler.Stop();
|
2017-04-05 16:27:13 +08:00
|
|
|
y++;
|
|
|
|
EXPECT_EQ(x.load(), y.load());
|
|
|
|
}
|