memgraph/tests/unit/scheduler.cpp
Matej Ferencevic d99724647a Add thread names
Summary:
To see the thread names in htop press <F2> to enter setup and under
'Display options' choose 'Show custom thread names'.

Reviewers: buda, teon.banek, florijan

Reviewed By: teon.banek

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D1234
2018-02-23 16:04:49 +01:00

32 lines
832 B
C++

#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <atomic>
#include "utils/scheduler.hpp"
/**
* Scheduler runs every 2 seconds and increases one variable. Test thread
* increases other variable. Scheduler checks if variables have the same
* value.
*/
TEST(Scheduler, TestFunctionExecuting) {
std::atomic<int> x{0};
std::function<void()> func{[&x]() { ++x; }};
Scheduler scheduler;
scheduler.Run("Test", std::chrono::seconds(1), func);
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);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
scheduler.Stop();
EXPECT_EQ(x, 3);
}