2018-01-02 18:12:05 +01:00
|
|
|
// Copyright (c) 2012-2017 The Bitcoin Core developers
|
2015-04-10 16:31:02 +02:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <random.h>
|
|
|
|
#include <scheduler.h>
|
2015-04-10 16:31:02 +02:00
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <test/test_bitcoin.h>
|
2015-04-10 16:31:02 +02:00
|
|
|
|
|
|
|
#include <boost/bind.hpp>
|
|
|
|
#include <boost/thread.hpp>
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE(scheduler_tests)
|
|
|
|
|
|
|
|
static void microTask(CScheduler& s, boost::mutex& mutex, int& counter, int delta, boost::chrono::system_clock::time_point rescheduleTime)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
boost::unique_lock<boost::mutex> lock(mutex);
|
|
|
|
counter += delta;
|
|
|
|
}
|
|
|
|
boost::chrono::system_clock::time_point noTime = boost::chrono::system_clock::time_point::min();
|
|
|
|
if (rescheduleTime != noTime) {
|
|
|
|
CScheduler::Function f = boost::bind(µTask, boost::ref(s), boost::ref(mutex), boost::ref(counter), -delta + 1, noTime);
|
|
|
|
s.schedule(f, rescheduleTime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void MicroSleep(uint64_t n)
|
|
|
|
{
|
|
|
|
#if defined(HAVE_WORKING_BOOST_SLEEP_FOR)
|
|
|
|
boost::this_thread::sleep_for(boost::chrono::microseconds(n));
|
|
|
|
#elif defined(HAVE_WORKING_BOOST_SLEEP)
|
|
|
|
boost::this_thread::sleep(boost::posix_time::microseconds(n));
|
|
|
|
#else
|
|
|
|
//should never get here
|
|
|
|
#error missing boost sleep implementation
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(manythreads)
|
|
|
|
{
|
|
|
|
// Stress test: hundreds of microsecond-scheduled tasks,
|
|
|
|
// serviced by 10 threads.
|
|
|
|
//
|
|
|
|
// So... ten shared counters, which if all the tasks execute
|
|
|
|
// properly will sum to the number of tasks done.
|
2017-01-13 16:05:16 +01:00
|
|
|
// Each task adds or subtracts a random amount from one of the
|
|
|
|
// counters, and then schedules another task 0-1000
|
2015-04-10 16:31:02 +02:00
|
|
|
// microseconds in the future to subtract or add from
|
|
|
|
// the counter -random_amount+1, so in the end the shared
|
|
|
|
// counters should sum to the number of initial tasks performed.
|
|
|
|
CScheduler microTasks;
|
|
|
|
|
|
|
|
boost::mutex counterMutex[10];
|
|
|
|
int counter[10] = { 0 };
|
2017-06-07 16:35:39 +02:00
|
|
|
FastRandomContext rng(42);
|
|
|
|
auto zeroToNine = [](FastRandomContext& rc) -> int { return rc.randrange(10); }; // [0, 9]
|
2018-02-23 08:10:42 +01:00
|
|
|
auto randomMsec = [](FastRandomContext& rc) -> int { return -11 + (int)rc.randrange(1012); }; // [-11, 1000]
|
|
|
|
auto randomDelta = [](FastRandomContext& rc) -> int { return -1000 + (int)rc.randrange(2001); }; // [-1000, 1000]
|
2015-04-10 16:31:02 +02:00
|
|
|
|
|
|
|
boost::chrono::system_clock::time_point start = boost::chrono::system_clock::now();
|
|
|
|
boost::chrono::system_clock::time_point now = start;
|
2015-05-15 18:40:36 +02:00
|
|
|
boost::chrono::system_clock::time_point first, last;
|
|
|
|
size_t nTasks = microTasks.getQueueInfo(first, last);
|
|
|
|
BOOST_CHECK(nTasks == 0);
|
2015-04-10 16:31:02 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < 100; i++) {
|
|
|
|
boost::chrono::system_clock::time_point t = now + boost::chrono::microseconds(randomMsec(rng));
|
|
|
|
boost::chrono::system_clock::time_point tReschedule = now + boost::chrono::microseconds(500 + randomMsec(rng));
|
|
|
|
int whichCounter = zeroToNine(rng);
|
|
|
|
CScheduler::Function f = boost::bind(µTask, boost::ref(microTasks),
|
|
|
|
boost::ref(counterMutex[whichCounter]), boost::ref(counter[whichCounter]),
|
|
|
|
randomDelta(rng), tReschedule);
|
|
|
|
microTasks.schedule(f, t);
|
|
|
|
}
|
2015-05-15 18:40:36 +02:00
|
|
|
nTasks = microTasks.getQueueInfo(first, last);
|
|
|
|
BOOST_CHECK(nTasks == 100);
|
|
|
|
BOOST_CHECK(first < last);
|
|
|
|
BOOST_CHECK(last > now);
|
|
|
|
|
|
|
|
// As soon as these are created they will start running and servicing the queue
|
|
|
|
boost::thread_group microThreads;
|
|
|
|
for (int i = 0; i < 5; i++)
|
|
|
|
microThreads.create_thread(boost::bind(&CScheduler::serviceQueue, µTasks));
|
2015-04-10 16:31:02 +02:00
|
|
|
|
|
|
|
MicroSleep(600);
|
|
|
|
now = boost::chrono::system_clock::now();
|
2015-05-15 18:40:36 +02:00
|
|
|
|
2015-04-10 16:31:02 +02:00
|
|
|
// More threads and more tasks:
|
|
|
|
for (int i = 0; i < 5; i++)
|
|
|
|
microThreads.create_thread(boost::bind(&CScheduler::serviceQueue, µTasks));
|
|
|
|
for (int i = 0; i < 100; i++) {
|
|
|
|
boost::chrono::system_clock::time_point t = now + boost::chrono::microseconds(randomMsec(rng));
|
|
|
|
boost::chrono::system_clock::time_point tReschedule = now + boost::chrono::microseconds(500 + randomMsec(rng));
|
|
|
|
int whichCounter = zeroToNine(rng);
|
|
|
|
CScheduler::Function f = boost::bind(µTask, boost::ref(microTasks),
|
|
|
|
boost::ref(counterMutex[whichCounter]), boost::ref(counter[whichCounter]),
|
|
|
|
randomDelta(rng), tReschedule);
|
|
|
|
microTasks.schedule(f, t);
|
|
|
|
}
|
|
|
|
|
2015-05-15 18:40:36 +02:00
|
|
|
// Drain the task queue then exit threads
|
|
|
|
microTasks.stop(true);
|
|
|
|
microThreads.join_all(); // ... wait until all the threads are done
|
2015-04-10 16:31:02 +02:00
|
|
|
|
|
|
|
int counterSum = 0;
|
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
|
BOOST_CHECK(counter[i] != 0);
|
|
|
|
counterSum += counter[i];
|
|
|
|
}
|
|
|
|
BOOST_CHECK_EQUAL(counterSum, 200);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|