Changeset View
Changeset View
Standalone View
Standalone View
src/test/scheduler_tests.cpp
// Copyright (c) 2012-2016 The Bitcoin Core developers | // Copyright (c) 2012-2016 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/test_bitcoin.h> | #include <test/test_bitcoin.h> | ||||
#include <boost/bind.hpp> | #include <boost/bind.hpp> | ||||
#include <boost/random/mersenne_twister.hpp> | |||||
#include <boost/random/uniform_int_distribution.hpp> | |||||
#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 All 27 Lines | BOOST_AUTO_TEST_CASE(manythreads) { | ||||
// 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]; | boost::mutex counterMutex[10]; | ||||
int counter[10] = {0}; | int counter[10] = {0}; | ||||
boost::random::mt19937 rng(42); | FastRandomContext rng{/* fDeterministic */ true}; | ||||
boost::random::uniform_int_distribution<> zeroToNine(0, 9); | // [0, 9] | ||||
boost::random::uniform_int_distribution<> randomMsec(-11, 1000); | auto zeroToNine = [](FastRandomContext &rc) -> int { | ||||
boost::random::uniform_int_distribution<> randomDelta(-1000, 1000); | return rc.randrange(10); | ||||
}; | |||||
// [-11, 1000] | |||||
auto randomMsec = [](FastRandomContext &rc) -> int { | |||||
return -11 + rc.randrange(1012); | |||||
}; | |||||
// [-1000, 1000] | |||||
auto randomDelta = [](FastRandomContext &rc) -> int { | |||||
return -1000 + rc.randrange(2001); | |||||
}; | |||||
boost::chrono::system_clock::time_point start = | boost::chrono::system_clock::time_point start = | ||||
boost::chrono::system_clock::now(); | boost::chrono::system_clock::now(); | ||||
boost::chrono::system_clock::time_point now = start; | boost::chrono::system_clock::time_point now = start; | ||||
boost::chrono::system_clock::time_point first, last; | boost::chrono::system_clock::time_point first, last; | ||||
size_t nTasks = microTasks.getQueueInfo(first, last); | size_t nTasks = microTasks.getQueueInfo(first, last); | ||||
BOOST_CHECK(nTasks == 0); | BOOST_CHECK(nTasks == 0); | ||||
▲ Show 20 Lines • Show All 156 Lines • Show Last 20 Lines |