2017-06-07 16:15:08 +08:00
|
|
|
#include <atomic>
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
2017-09-07 20:43:27 +08:00
|
|
|
#include "utils/executor.hpp"
|
2017-06-07 16:15:08 +08:00
|
|
|
|
2017-09-07 20:43:27 +08:00
|
|
|
TEST(Executor, Run) {
|
2017-06-07 16:15:08 +08:00
|
|
|
std::atomic<int> count{0};
|
|
|
|
{
|
2018-04-22 14:31:09 +08:00
|
|
|
utils::Executor exec(std::chrono::milliseconds(500));
|
2017-09-07 20:43:27 +08:00
|
|
|
// Be sure executor is sleeping.
|
2017-06-07 16:15:08 +08:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
|
|
|
|
|
|
exec.RegisterJob([&count]() { ++count; });
|
|
|
|
exec.RegisterJob([&count]() { ++count; });
|
|
|
|
exec.RegisterJob([&count]() { ++count; });
|
|
|
|
|
2017-09-07 20:43:27 +08:00
|
|
|
// Be sure executor execute thread is triggered
|
2017-06-07 16:15:08 +08:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
|
|
|
}
|
|
|
|
EXPECT_EQ(count, 3);
|
|
|
|
}
|
|
|
|
|
2017-09-07 20:43:27 +08:00
|
|
|
TEST(Executor, RunUnregister) {
|
2017-06-07 16:15:08 +08:00
|
|
|
std::atomic<int> count1{0};
|
|
|
|
std::atomic<int> count2{0};
|
|
|
|
{
|
2018-04-22 14:31:09 +08:00
|
|
|
utils::Executor exec(std::chrono::milliseconds(500));
|
2017-09-07 20:43:27 +08:00
|
|
|
// Be sure executor is sleeping.
|
2017-06-07 16:15:08 +08:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
|
|
auto job = exec.RegisterJob([&count1]() { ++count1; });
|
|
|
|
exec.RegisterJob([&count2]() { ++count2; });
|
|
|
|
|
2017-09-07 20:43:27 +08:00
|
|
|
// Be sure executor execute thread is triggered
|
2017-06-07 16:15:08 +08:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
|
|
|
|
|
|
|
exec.UnRegisterJob(job);
|
|
|
|
|
2017-09-07 20:43:27 +08:00
|
|
|
// Be sure executor execute thread is triggered
|
2017-06-07 16:15:08 +08:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
|
|
|
}
|
|
|
|
EXPECT_EQ(count1, 1);
|
|
|
|
EXPECT_EQ(count2, 2);
|
|
|
|
}
|