memgraph/tests/unit/utils_executor.cpp
Teon Banek ce29517998 Extract utils into mg-utils static library and explicitly list tests
Summary:
Utils source files are now moved to a standalone mg-utils library.

Unit and manual tests are no longer collected using glob recursion in
cmake, but are explicitly listed. This allows us to set only required
dependencies of those tests.

Both of these changes should improve compilation and link times, as well
as lower the disk usage.

Additional improvement would be to cleanup utils header files to be
split in .hpp and .cpp as well as merging threading into utils. Other
potential library extractions that shouldn't be difficult are:

  * data_structures
  * io/network
  * communication

Reviewers: buda, mferencevic, dgleich, ipaljak, mculinovic, mtomic, msantl

Reviewed By: buda

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D1408
2018-05-30 09:41:56 +02:00

46 lines
1.3 KiB
C++

#include <atomic>
#include <memory>
#include "gtest/gtest.h"
#include "utils/executor.hpp"
TEST(Executor, Run) {
std::atomic<int> count{0};
{
utils::Executor exec(std::chrono::milliseconds(500));
// Be sure executor is sleeping.
std::this_thread::sleep_for(std::chrono::milliseconds(100));
exec.RegisterJob([&count]() { ++count; });
exec.RegisterJob([&count]() { ++count; });
exec.RegisterJob([&count]() { ++count; });
// Be sure executor execute thread is triggered
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
EXPECT_EQ(count, 3);
}
TEST(Executor, RunUnregister) {
std::atomic<int> count1{0};
std::atomic<int> count2{0};
{
utils::Executor exec(std::chrono::milliseconds(500));
// Be sure executor is sleeping.
std::this_thread::sleep_for(std::chrono::milliseconds(100));
auto job = exec.RegisterJob([&count1]() { ++count1; });
exec.RegisterJob([&count2]() { ++count2; });
// Be sure executor execute thread is triggered
std::this_thread::sleep_for(std::chrono::milliseconds(500));
exec.UnRegisterJob(job);
// Be sure executor execute thread is triggered
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
EXPECT_EQ(count1, 1);
EXPECT_EQ(count2, 2);
}