]> Git Repo - VerusCoin.git/blob - src/test/scheduler_tests.cpp
Merge branch 'dev' into jl777
[VerusCoin.git] / src / test / scheduler_tests.cpp
1 // Copyright (c) 2012-2013 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.
4
5 #include "random.h"
6 #include "scheduler.h"
7
8 #include "test/test_bitcoin.h"
9
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>
15
16 BOOST_AUTO_TEST_SUITE(scheduler_tests)
17
18 static void microTask(CScheduler& s, boost::mutex& mutex, int& counter, int delta, boost::chrono::system_clock::time_point rescheduleTime)
19 {
20     {
21         boost::unique_lock<boost::mutex> lock(mutex);
22         counter += delta;
23     }
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(&microTask, boost::ref(s), boost::ref(mutex), boost::ref(counter), -delta + 1, noTime);
27         s.schedule(f, rescheduleTime);
28     }
29 }
30
31 static void MicroSleep(uint64_t n)
32 {
33     boost::this_thread::sleep_for(boost::chrono::microseconds(n));
34 }
35
36 BOOST_AUTO_TEST_CASE(manythreads)
37 {
38     seed_insecure_rand(false);
39
40     // Stress test: hundreds of microsecond-scheduled tasks,
41     // serviced by 10 threads.
42     //
43     // So... ten shared counters, which if all the tasks execute
44     // properly will sum to the number of tasks done.
45     // Each task adds or subtracts from one of the counters a
46     // random amount, and then schedules another task 0-1000
47     // microseconds in the future to subtract or add from
48     // the counter -random_amount+1, so in the end the shared
49     // counters should sum to the number of initial tasks performed.
50     CScheduler microTasks;
51
52     boost::mutex counterMutex[10];
53     int counter[10] = { 0 };
54     boost::random::mt19937 rng(insecure_rand());
55     boost::random::uniform_int_distribution<> zeroToNine(0, 9);
56     boost::random::uniform_int_distribution<> randomMsec(-11, 1000);
57     boost::random::uniform_int_distribution<> randomDelta(-1000, 1000);
58
59     boost::chrono::system_clock::time_point start = boost::chrono::system_clock::now();
60     boost::chrono::system_clock::time_point now = start;
61     boost::chrono::system_clock::time_point first, last;
62     size_t nTasks = microTasks.getQueueInfo(first, last);
63     BOOST_CHECK(nTasks == 0);
64
65     for (int i = 0; i < 100; i++) {
66         boost::chrono::system_clock::time_point t = now + boost::chrono::microseconds(randomMsec(rng));
67         boost::chrono::system_clock::time_point tReschedule = now + boost::chrono::microseconds(500 + randomMsec(rng));
68         int whichCounter = zeroToNine(rng);
69         CScheduler::Function f = boost::bind(&microTask, boost::ref(microTasks),
70                                              boost::ref(counterMutex[whichCounter]), boost::ref(counter[whichCounter]),
71                                              randomDelta(rng), tReschedule);
72         microTasks.schedule(f, t);
73     }
74     nTasks = microTasks.getQueueInfo(first, last);
75     BOOST_CHECK(nTasks == 100);
76     BOOST_CHECK(first < last);
77     BOOST_CHECK(last > now);
78
79     // As soon as these are created they will start running and servicing the queue
80     boost::thread_group microThreads;
81     for (int i = 0; i < 5; i++)
82         microThreads.create_thread(boost::bind(&CScheduler::serviceQueue, &microTasks));
83
84     MicroSleep(600);
85     now = boost::chrono::system_clock::now();
86
87     // More threads and more tasks:
88     for (int i = 0; i < 5; i++)
89         microThreads.create_thread(boost::bind(&CScheduler::serviceQueue, &microTasks));
90     for (int i = 0; i < 100; i++) {
91         boost::chrono::system_clock::time_point t = now + boost::chrono::microseconds(randomMsec(rng));
92         boost::chrono::system_clock::time_point tReschedule = now + boost::chrono::microseconds(500 + randomMsec(rng));
93         int whichCounter = zeroToNine(rng);
94         CScheduler::Function f = boost::bind(&microTask, boost::ref(microTasks),
95                                              boost::ref(counterMutex[whichCounter]), boost::ref(counter[whichCounter]),
96                                              randomDelta(rng), tReschedule);
97         microTasks.schedule(f, t);
98     }
99
100     // Drain the task queue then exit threads
101     microTasks.stop(true);
102     microThreads.join_all(); // ... wait until all the threads are done
103
104     int counterSum = 0;
105     for (int i = 0; i < 10; i++) {
106         BOOST_CHECK(counter[i] != 0);
107         counterSum += counter[i];
108     }
109     BOOST_CHECK_EQUAL(counterSum, 200);
110 }
111
112 BOOST_AUTO_TEST_SUITE_END()
This page took 0.034366 seconds and 4 git commands to generate.