Changeset View
Changeset View
Standalone View
Standalone View
src/test/scheduler_tests.cpp
| Show All 20 Lines | static void microTask(CScheduler &s, boost::mutex &mutex, int &counter, | ||||
| { | { | ||||
| boost::unique_lock<boost::mutex> lock(mutex); | boost::unique_lock<boost::mutex> lock(mutex); | ||||
| counter += delta; | counter += delta; | ||||
| } | } | ||||
| boost::chrono::system_clock::time_point noTime = | boost::chrono::system_clock::time_point noTime = | ||||
| boost::chrono::system_clock::time_point::min(); | boost::chrono::system_clock::time_point::min(); | ||||
| if (rescheduleTime != noTime) { | if (rescheduleTime != noTime) { | ||||
| CScheduler::Function f = | CScheduler::Function f = | ||||
| boost::bind(µTask, boost::ref(s), boost::ref(mutex), | boost::bind(µTask, std::ref(s), std::ref(mutex), | ||||
| boost::ref(counter), -delta + 1, noTime); | std::ref(counter), -delta + 1, noTime); | ||||
| s.schedule(f, rescheduleTime); | s.schedule(f, rescheduleTime); | ||||
| } | } | ||||
| } | } | ||||
| static void MicroSleep(uint64_t n) { | static void MicroSleep(uint64_t n) { | ||||
| #if defined(HAVE_WORKING_BOOST_SLEEP_FOR) | #if defined(HAVE_WORKING_BOOST_SLEEP_FOR) | ||||
| boost::this_thread::sleep_for(boost::chrono::microseconds(n)); | boost::this_thread::sleep_for(boost::chrono::microseconds(n)); | ||||
| #elif defined(HAVE_WORKING_BOOST_SLEEP) | #elif defined(HAVE_WORKING_BOOST_SLEEP) | ||||
| Show All 33 Lines | BOOST_AUTO_TEST_CASE(manythreads) { | ||||
| for (int i = 0; i < 100; i++) { | for (int i = 0; i < 100; i++) { | ||||
| boost::chrono::system_clock::time_point t = | boost::chrono::system_clock::time_point t = | ||||
| now + boost::chrono::microseconds(randomMsec(rng)); | now + boost::chrono::microseconds(randomMsec(rng)); | ||||
| boost::chrono::system_clock::time_point tReschedule = | boost::chrono::system_clock::time_point tReschedule = | ||||
| now + boost::chrono::microseconds(500 + randomMsec(rng)); | now + boost::chrono::microseconds(500 + randomMsec(rng)); | ||||
| int whichCounter = zeroToNine(rng); | int whichCounter = zeroToNine(rng); | ||||
| CScheduler::Function f = boost::bind( | CScheduler::Function f = boost::bind( | ||||
| µTask, boost::ref(microTasks), | µTask, std::ref(microTasks), | ||||
| boost::ref(counterMutex[whichCounter]), | std::ref(counterMutex[whichCounter]), | ||||
| boost::ref(counter[whichCounter]), randomDelta(rng), tReschedule); | std::ref(counter[whichCounter]), randomDelta(rng), tReschedule); | ||||
| microTasks.schedule(f, t); | microTasks.schedule(f, t); | ||||
| } | } | ||||
| nTasks = microTasks.getQueueInfo(first, last); | nTasks = microTasks.getQueueInfo(first, last); | ||||
| BOOST_CHECK(nTasks == 100); | BOOST_CHECK(nTasks == 100); | ||||
| BOOST_CHECK(first < last); | BOOST_CHECK(first < last); | ||||
| BOOST_CHECK(last > now); | BOOST_CHECK(last > now); | ||||
| // As soon as these are created they will start running and servicing the | // As soon as these are created they will start running and servicing the | ||||
| Show All 12 Lines | for (int i = 0; i < 5; i++) | ||||
| boost::bind(&CScheduler::serviceQueue, µTasks)); | boost::bind(&CScheduler::serviceQueue, µTasks)); | ||||
| for (int i = 0; i < 100; i++) { | for (int i = 0; i < 100; i++) { | ||||
| boost::chrono::system_clock::time_point t = | boost::chrono::system_clock::time_point t = | ||||
| now + boost::chrono::microseconds(randomMsec(rng)); | now + boost::chrono::microseconds(randomMsec(rng)); | ||||
| boost::chrono::system_clock::time_point tReschedule = | boost::chrono::system_clock::time_point tReschedule = | ||||
| now + boost::chrono::microseconds(500 + randomMsec(rng)); | now + boost::chrono::microseconds(500 + randomMsec(rng)); | ||||
| int whichCounter = zeroToNine(rng); | int whichCounter = zeroToNine(rng); | ||||
| CScheduler::Function f = boost::bind( | CScheduler::Function f = boost::bind( | ||||
| µTask, boost::ref(microTasks), | µTask, std::ref(microTasks), | ||||
| boost::ref(counterMutex[whichCounter]), | std::ref(counterMutex[whichCounter]), | ||||
| boost::ref(counter[whichCounter]), randomDelta(rng), tReschedule); | std::ref(counter[whichCounter]), randomDelta(rng), tReschedule); | ||||
| microTasks.schedule(f, t); | microTasks.schedule(f, t); | ||||
| } | } | ||||
| // Drain the task queue then exit threads | // Drain the task queue then exit threads | ||||
| microTasks.stop(true); | microTasks.stop(true); | ||||
| microThreads.join_all(); // ... wait until all the threads are done | microThreads.join_all(); // ... wait until all the threads are done | ||||
| int counterSum = 0; | int counterSum = 0; | ||||
| for (int i = 0; i < 10; i++) { | for (int i = 0; i < 10; i++) { | ||||
| BOOST_CHECK(counter[i] != 0); | BOOST_CHECK(counter[i] != 0); | ||||
| counterSum += counter[i]; | counterSum += counter[i]; | ||||
| } | } | ||||
| BOOST_CHECK_EQUAL(counterSum, 200); | BOOST_CHECK_EQUAL(counterSum, 200); | ||||
| } | } | ||||
| BOOST_AUTO_TEST_SUITE_END() | BOOST_AUTO_TEST_SUITE_END() | ||||