memgraph/tests/unit/scheduler.cpp
Mislav Bradac 2fd43019d3 Change scheduler to first wait then execute
Reviewers: dgleich

Reviewed By: dgleich

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D884
2017-10-09 15:40:00 +02:00

32 lines
824 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(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);
}