1 // Copyright (c) 2012-2016 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
8 #include "test/test_bitcoin.h"
10 #include <boost/bind.hpp>
11 #include <boost/random/mersenne_twister.hpp>
12 #include <boost/random/uniform_int_distribution.hpp>
13 #include <boost/thread.hpp>
14 #include <boost/test/unit_test.hpp>
16 BOOST_AUTO_TEST_SUITE(scheduler_tests
)
18 static void microTask(CScheduler
& s
, boost::mutex
& mutex
, int& counter
, int delta
, boost::chrono::system_clock::time_point rescheduleTime
)
21 boost::unique_lock
<boost::mutex
> lock(mutex
);
24 boost::chrono::system_clock::time_point noTime
= boost::chrono::system_clock::time_point::min();
25 if (rescheduleTime
!= noTime
) {
26 CScheduler::Function f
= boost::bind(µTask
, boost::ref(s
), boost::ref(mutex
), boost::ref(counter
), -delta
+ 1, noTime
);
27 s
.schedule(f
, rescheduleTime
);
31 static void MicroSleep(uint64_t n
)
33 #if defined(HAVE_WORKING_BOOST_SLEEP_FOR)
34 boost::this_thread::sleep_for(boost::chrono::microseconds(n
));
35 #elif defined(HAVE_WORKING_BOOST_SLEEP)
36 boost::this_thread::sleep(boost::posix_time::microseconds(n
));
38 //should never get here
39 #error missing boost sleep implementation
43 BOOST_AUTO_TEST_CASE(manythreads
)
45 // Stress test: hundreds of microsecond-scheduled tasks,
46 // serviced by 10 threads.
48 // So... ten shared counters, which if all the tasks execute
49 // properly will sum to the number of tasks done.
50 // Each task adds or subtracts a random amount from one of the
51 // counters, and then schedules another task 0-1000
52 // microseconds in the future to subtract or add from
53 // the counter -random_amount+1, so in the end the shared
54 // counters should sum to the number of initial tasks performed.
55 CScheduler microTasks
;
57 boost::mutex counterMutex
[10];
58 int counter
[10] = { 0 };
59 boost::random::mt19937
rng(42);
60 boost::random::uniform_int_distribution
<> zeroToNine(0, 9);
61 boost::random::uniform_int_distribution
<> randomMsec(-11, 1000);
62 boost::random::uniform_int_distribution
<> randomDelta(-1000, 1000);
64 boost::chrono::system_clock::time_point start
= boost::chrono::system_clock::now();
65 boost::chrono::system_clock::time_point now
= start
;
66 boost::chrono::system_clock::time_point first
, last
;
67 size_t nTasks
= microTasks
.getQueueInfo(first
, last
);
68 BOOST_CHECK(nTasks
== 0);
70 for (int i
= 0; i
< 100; i
++) {
71 boost::chrono::system_clock::time_point t
= now
+ boost::chrono::microseconds(randomMsec(rng
));
72 boost::chrono::system_clock::time_point tReschedule
= now
+ boost::chrono::microseconds(500 + randomMsec(rng
));
73 int whichCounter
= zeroToNine(rng
);
74 CScheduler::Function f
= boost::bind(µTask
, boost::ref(microTasks
),
75 boost::ref(counterMutex
[whichCounter
]), boost::ref(counter
[whichCounter
]),
76 randomDelta(rng
), tReschedule
);
77 microTasks
.schedule(f
, t
);
79 nTasks
= microTasks
.getQueueInfo(first
, last
);
80 BOOST_CHECK(nTasks
== 100);
81 BOOST_CHECK(first
< last
);
82 BOOST_CHECK(last
> now
);
84 // As soon as these are created they will start running and servicing the queue
85 boost::thread_group microThreads
;
86 for (int i
= 0; i
< 5; i
++)
87 microThreads
.create_thread(boost::bind(&CScheduler::serviceQueue
, µTasks
));
90 now
= boost::chrono::system_clock::now();
92 // More threads and more tasks:
93 for (int i
= 0; i
< 5; i
++)
94 microThreads
.create_thread(boost::bind(&CScheduler::serviceQueue
, µTasks
));
95 for (int i
= 0; i
< 100; i
++) {
96 boost::chrono::system_clock::time_point t
= now
+ boost::chrono::microseconds(randomMsec(rng
));
97 boost::chrono::system_clock::time_point tReschedule
= now
+ boost::chrono::microseconds(500 + randomMsec(rng
));
98 int whichCounter
= zeroToNine(rng
);
99 CScheduler::Function f
= boost::bind(µTask
, boost::ref(microTasks
),
100 boost::ref(counterMutex
[whichCounter
]), boost::ref(counter
[whichCounter
]),
101 randomDelta(rng
), tReschedule
);
102 microTasks
.schedule(f
, t
);
105 // Drain the task queue then exit threads
106 microTasks
.stop(true);
107 microThreads
.join_all(); // ... wait until all the threads are done
110 for (int i
= 0; i
< 10; i
++) {
111 BOOST_CHECK(counter
[i
] != 0);
112 counterSum
+= counter
[i
];
114 BOOST_CHECK_EQUAL(counterSum
, 200);
117 BOOST_AUTO_TEST_SUITE_END()