| 39 | } |
| 40 | |
| 41 | BOOST_AUTO_TEST_CASE(manythreads) |
| 42 | { |
| 43 | // Stress test: hundreds of microsecond-scheduled tasks, |
| 44 | // serviced by 10 threads. |
| 45 | // |
| 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; |
| 54 | |
| 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 + (int)rc.randrange(1012); }; // [-11, 1000] |
| 60 | auto randomDelta = [](FastRandomContext& rc) -> int { return -1000 + (int)rc.randrange(2001); }; // [-1000, 1000] |
| 61 | |
| 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); |
| 67 | |
| 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); |
| 76 | } |
| 77 | nTasks = microTasks.getQueueInfo(first, last); |
| 78 | BOOST_CHECK(nTasks == 100); |
| 79 | BOOST_CHECK(first < last); |
| 80 | BOOST_CHECK(last > now); |
| 81 | |
| 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)); |
| 86 | |
| 87 | MicroSleep(600); |
| 88 | now = boost::chrono::system_clock::now(); |
| 89 | |
| 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]), |
nothing calls this directly
no test coverage detected