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 <util/time.h> | #include <util/time.h> | ||||
#include <random.h> | #include <random.h> | ||||
#include <sync.h> | #include <sync.h> | ||||
#include <boost/test/unit_test.hpp> | #include <boost/test/unit_test.hpp> | ||||
#include <boost/thread.hpp> | #include <boost/thread.hpp> | ||||
#include <atomic> | #include <atomic> | ||||
#include <condition_variable> | #include <condition_variable> | ||||
#include <mutex> | |||||
#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, std::mutex &mutex, int &counter, int delta, | ||||
int delta, | |||||
std::chrono::system_clock::time_point rescheduleTime) { | std::chrono::system_clock::time_point rescheduleTime) { | ||||
{ | { | ||||
boost::unique_lock<boost::mutex> lock(mutex); | std::lock_guard<std::mutex> lock(mutex); | ||||
counter += delta; | counter += delta; | ||||
} | } | ||||
std::chrono::system_clock::time_point noTime = | std::chrono::system_clock::time_point noTime = | ||||
std::chrono::system_clock::time_point::min(); | std::chrono::system_clock::time_point::min(); | ||||
if (rescheduleTime != noTime) { | if (rescheduleTime != noTime) { | ||||
CScheduler::Function f = | CScheduler::Function f = | ||||
std::bind(µTask, std::ref(s), std::ref(mutex), | std::bind(µTask, std::ref(s), std::ref(mutex), | ||||
std::ref(counter), -delta + 1, noTime); | std::ref(counter), -delta + 1, noTime); | ||||
Show All 9 Lines | BOOST_AUTO_TEST_CASE(manythreads) { | ||||
// properly will sum to the number of tasks done. | // properly will sum to the number of tasks done. | ||||
// Each task adds or subtracts a random amount from one of the | // Each task adds or subtracts a random amount from one of the | ||||
// counters, and then schedules another task 0-1000 | // counters, and then schedules another task 0-1000 | ||||
// microseconds in the future to subtract or add from | // microseconds in the future to subtract or add from | ||||
// the counter -random_amount+1, so in the end the shared | // the counter -random_amount+1, so in the end the shared | ||||
// counters should sum to the number of initial tasks performed. | // counters should sum to the number of initial tasks performed. | ||||
CScheduler microTasks; | CScheduler microTasks; | ||||
boost::mutex counterMutex[10]; | std::mutex counterMutex[10]; | ||||
int counter[10] = {0}; | int counter[10] = {0}; | ||||
FastRandomContext rng{/* fDeterministic */ true}; | FastRandomContext rng{/* fDeterministic */ true}; | ||||
// [0, 9] | // [0, 9] | ||||
auto zeroToNine = [](FastRandomContext &rc) -> int { | auto zeroToNine = [](FastRandomContext &rc) -> int { | ||||
return rc.randrange(10); | return rc.randrange(10); | ||||
}; | }; | ||||
// [-11, 1000] | // [-11, 1000] | ||||
auto randomMsec = [](FastRandomContext &rc) -> int { | auto randomMsec = [](FastRandomContext &rc) -> int { | ||||
▲ Show 20 Lines • Show All 230 Lines • Show Last 20 Lines |