2022-02-22 20:33:45 +08:00
|
|
|
// Copyright 2022 Memgraph Ltd.
|
2021-10-26 14:53:56 +08:00
|
|
|
//
|
|
|
|
// Use of this software is governed by the Business Source License
|
|
|
|
// included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source
|
|
|
|
// License, and you may not use this file except in compliance with the Business Source License.
|
|
|
|
//
|
|
|
|
// As of the Change Date specified in that file, in accordance with
|
|
|
|
// the Business Source License, use of this software will be governed
|
|
|
|
// by the Apache License, Version 2.0, included in the file
|
|
|
|
// licenses/APL.txt.
|
|
|
|
|
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-10-09 20:44:11 +08:00
|
|
|
std::atomic<int> x{0};
|
|
|
|
std::function<void()> func{[&x]() { ++x; }};
|
2022-02-22 20:33:45 +08:00
|
|
|
memgraph::utils::Scheduler scheduler;
|
2018-02-23 21:35:16 +08:00
|
|
|
scheduler.Run("Test", std::chrono::seconds(1), func);
|
2017-04-05 16:27:13 +08:00
|
|
|
|
2017-10-09 20:44:11 +08:00
|
|
|
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);
|
2017-04-05 16:27:13 +08:00
|
|
|
|
2017-04-07 17:33:24 +08:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
|
|
scheduler.Stop();
|
2017-10-09 20:44:11 +08:00
|
|
|
EXPECT_EQ(x, 3);
|
2017-04-05 16:27:13 +08:00
|
|
|
}
|