Changeset View
Changeset View
Standalone View
Standalone View
src/test/scheduler_tests.cpp
// Copyright (c) 2012-2019 The Bitcoin Core developers | // Copyright (c) 2012-2019 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <scheduler.h> | #include <scheduler.h> | ||||
#include <random.h> | #include <random.h> | ||||
#include <test/util/setup_common.h> | |||||
#include <boost/test/unit_test.hpp> | #include <boost/test/unit_test.hpp> | ||||
#include <atomic> | #include <atomic> | ||||
#include <thread> | #include <thread> | ||||
BOOST_AUTO_TEST_SUITE(scheduler_tests) | BOOST_AUTO_TEST_SUITE(scheduler_tests) | ||||
static void microTask(CScheduler &s, boost::mutex &mutex, int &counter, | static void microTask(CScheduler &s, boost::mutex &mutex, int &counter, | ||||
▲ Show 20 Lines • Show All 202 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(singlethreadedscheduler_ordered) { | ||||
// finish up | // finish up | ||||
scheduler.stop(true); | scheduler.stop(true); | ||||
threads.join_all(); | threads.join_all(); | ||||
BOOST_CHECK_EQUAL(counter1, 100); | BOOST_CHECK_EQUAL(counter1, 100); | ||||
BOOST_CHECK_EQUAL(counter2, 100); | BOOST_CHECK_EQUAL(counter2, 100); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(mockforward) { | |||||
CScheduler scheduler; | |||||
int counter{0}; | |||||
CScheduler::Function dummy = [&counter] { counter++; }; | |||||
// schedule jobs for 2, 5 & 8 minutes into the future | |||||
int64_t min_in_milli = 60 * 1000; | |||||
scheduler.scheduleFromNow(dummy, 2 * min_in_milli); | |||||
scheduler.scheduleFromNow(dummy, 5 * min_in_milli); | |||||
scheduler.scheduleFromNow(dummy, 8 * min_in_milli); | |||||
// check taskQueue | |||||
boost::chrono::system_clock::time_point first, last; | |||||
size_t num_tasks = scheduler.getQueueInfo(first, last); | |||||
BOOST_CHECK_EQUAL(num_tasks, 3ul); | |||||
std::thread scheduler_thread([&]() { scheduler.serviceQueue(); }); | |||||
// bump the scheduler forward 5 minutes | |||||
scheduler.MockForward(boost::chrono::seconds(5 * 60)); | |||||
// ensure scheduler has chance to process all tasks queued for before 1 ms | |||||
// from now. | |||||
scheduler.scheduleFromNow([&scheduler] { scheduler.stop(false); }, 1); | |||||
scheduler_thread.join(); | |||||
// check that the queue only has one job remaining | |||||
num_tasks = scheduler.getQueueInfo(first, last); | |||||
BOOST_CHECK_EQUAL(num_tasks, 1ul); | |||||
// check that the dummy function actually ran | |||||
BOOST_CHECK_EQUAL(counter, 2); | |||||
// check that the time of the remaining job has been updated | |||||
boost::chrono::system_clock::time_point now = | |||||
boost::chrono::system_clock::now(); | |||||
int delta = | |||||
boost::chrono::duration_cast<boost::chrono::seconds>(first - now) | |||||
.count(); | |||||
// should be between 2 & 3 minutes from now | |||||
BOOST_CHECK(delta > 2 * 60 && delta < 3 * 60); | |||||
} | |||||
BOOST_AUTO_TEST_SUITE_END() | BOOST_AUTO_TEST_SUITE_END() |