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/thread.hpp>
12 #include <boost/test/unit_test.hpp>
14 BOOST_AUTO_TEST_SUITE(scheduler_tests
)
16 static void microTask(CScheduler
& s
, boost::mutex
& mutex
, int& counter
, int delta
, boost::chrono::system_clock::time_point rescheduleTime
)
19 boost::unique_lock
<boost::mutex
> lock(mutex
);
22 boost::chrono::system_clock::time_point noTime
= boost::chrono::system_clock::time_point::min();
23 if (rescheduleTime
!= noTime
) {
24 CScheduler::Function f
= boost::bind(µTask
, boost::ref(s
), boost::ref(mutex
), boost::ref(counter
), -delta
+ 1, noTime
);
25 s
.schedule(f
, rescheduleTime
);
29 static void MicroSleep(uint64_t n
)
31 #if defined(HAVE_WORKING_BOOST_SLEEP_FOR)
32 boost::this_thread::sleep_for(boost::chrono::microseconds(n
));
33 #elif defined(HAVE_WORKING_BOOST_SLEEP)
34 boost::this_thread::sleep(boost::posix_time::microseconds(n
));
36 //should never get here
37 #error missing boost sleep implementation
41 BOOST_AUTO_TEST_CASE(manythreads
)
43 // Stress test: hundreds of microsecond-scheduled tasks,
44 // serviced by 10 threads.
46 // So... ten shared counters, which if all the tasks execute
47 // properly will sum to the number of tasks done.
48 // Each task adds or subtracts a random amount from one of the
49 // counters, and then schedules another task 0-1000
50 // microseconds in the future to subtract or add from
51 // the counter -random_amount+1, so in the end the shared
52 // counters should sum to the number of initial tasks performed.
53 CScheduler microTasks
;
55 boost::mutex counterMutex
[10];
56 int counter
[10] = { 0 };
57 FastRandomContext
rng(42);
58 auto zeroToNine
= [](FastRandomContext
& rc
) -> int { return rc
.randrange(10); }; // [0, 9]
59 auto randomMsec
= [](FastRandomContext
& rc
) -> int { return -11 + rc
.randrange(1012); }; // [-11, 1000]
60 auto randomDelta
= [](FastRandomContext
& rc
) -> int { return -1000 + rc
.randrange(2001); }; // [-1000, 1000]
62 boost::chrono::system_clock::time_point start
= boost::chrono::system_clock::now();
63 boost::chrono::system_clock::time_point now
= start
;
64 boost::chrono::system_clock::time_point first
, last
;
65 size_t nTasks
= microTasks
.getQueueInfo(first
, last
);
66 BOOST_CHECK(nTasks
== 0);
68 for (int i
= 0; i
< 100; i
++) {
69 boost::chrono::system_clock::time_point t
= now
+ boost::chrono::microseconds(randomMsec(rng
));
70 boost::chrono::system_clock::time_point tReschedule
= now
+ boost::chrono::microseconds(500 + randomMsec(rng
));
71 int whichCounter
= zeroToNine(rng
);
72 CScheduler::Function f
= boost::bind(µTask
, boost::ref(microTasks
),
73 boost::ref(counterMutex
[whichCounter
]), boost::ref(counter
[whichCounter
]),
74 randomDelta(rng
), tReschedule
);
75 microTasks
.schedule(f
, t
);
77 nTasks
= microTasks
.getQueueInfo(first
, last
);
78 BOOST_CHECK(nTasks
== 100);
79 BOOST_CHECK(first
< last
);
80 BOOST_CHECK(last
> now
);
82 // As soon as these are created they will start running and servicing the queue
83 boost::thread_group microThreads
;
84 for (int i
= 0; i
< 5; i
++)
85 microThreads
.create_thread(boost::bind(&CScheduler::serviceQueue
, µTasks
));
88 now
= boost::chrono::system_clock::now();
90 // More threads and more tasks:
91 for (int i
= 0; i
< 5; i
++)
92 microThreads
.create_thread(boost::bind(&CScheduler::serviceQueue
, µTasks
));
93 for (int i
= 0; i
< 100; i
++) {
94 boost::chrono::system_clock::time_point t
= now
+ boost::chrono::microseconds(randomMsec(rng
));
95 boost::chrono::system_clock::time_point tReschedule
= now
+ boost::chrono::microseconds(500 + randomMsec(rng
));
96 int whichCounter
= zeroToNine(rng
);
97 CScheduler::Function f
= boost::bind(µTask
, boost::ref(microTasks
),
98 boost::ref(counterMutex
[whichCounter
]), boost::ref(counter
[whichCounter
]),
99 randomDelta(rng
), tReschedule
);
100 microTasks
.schedule(f
, t
);
103 // Drain the task queue then exit threads
104 microTasks
.stop(true);
105 microThreads
.join_all(); // ... wait until all the threads are done
108 for (int i
= 0; i
< 10; i
++) {
109 BOOST_CHECK(counter
[i
] != 0);
110 counterSum
+= counter
[i
];
112 BOOST_CHECK_EQUAL(counterSum
, 200);
115 BOOST_AUTO_TEST_SUITE_END()