Changeset View
Changeset View
Standalone View
Standalone View
src/test/scheduler_tests.cpp
Show First 20 Lines • Show All 112 Lines • ▼ Show 20 Lines | for (int i = 0; i < 100; i++) { | ||||
CScheduler::Function f = std::bind(µTask, std::ref(microTasks), | CScheduler::Function f = std::bind(µTask, std::ref(microTasks), | ||||
std::ref(counterMutex[whichCounter]), | std::ref(counterMutex[whichCounter]), | ||||
std::ref(counter[whichCounter]), | std::ref(counter[whichCounter]), | ||||
randomDelta(rng), tReschedule); | 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.StopWhenDrained(); | ||||
// ... wait until all the threads are done | // ... wait until all the threads are done | ||||
microThreads.join_all(); | microThreads.join_all(); | ||||
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]; | ||||
} | } | ||||
Show All 39 Lines | BOOST_AUTO_TEST_CASE(schedule_every) { | ||||
Mutex mutex; | Mutex mutex; | ||||
WAIT_LOCK(mutex, lock); | WAIT_LOCK(mutex, lock); | ||||
while (keepRunning) { | while (keepRunning) { | ||||
cvar.wait(lock); | cvar.wait(lock); | ||||
BOOST_CHECK(counter >= 0); | BOOST_CHECK(counter >= 0); | ||||
} | } | ||||
BOOST_CHECK_EQUAL(counter, 0); | BOOST_CHECK_EQUAL(counter, 0); | ||||
scheduler.stop(true); | scheduler.StopWhenDrained(); | ||||
schedulerThread.join(); | schedulerThread.join(); | ||||
BOOST_CHECK_EQUAL(counter, 42); | BOOST_CHECK_EQUAL(counter, 42); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(wait_until_past) { | BOOST_AUTO_TEST_CASE(wait_until_past) { | ||||
std::condition_variable condvar; | std::condition_variable condvar; | ||||
Mutex mtx; | Mutex mtx; | ||||
WAIT_LOCK(mtx, lock); | WAIT_LOCK(mtx, lock); | ||||
▲ Show 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | for (int i = 0; i < 100; ++i) { | ||||
queue2.AddToProcessQueue([i, &counter2]() { | queue2.AddToProcessQueue([i, &counter2]() { | ||||
bool expectation = i == counter2++; | bool expectation = i == counter2++; | ||||
assert(expectation); | assert(expectation); | ||||
}); | }); | ||||
} | } | ||||
// finish up | // finish up | ||||
scheduler.stop(true); | scheduler.StopWhenDrained(); | ||||
threads.join_all(); | threads.join_all(); | ||||
BOOST_CHECK_EQUAL(counter1, 100); | BOOST_CHECK_EQUAL(counter1, 100); | ||||
BOOST_CHECK_EQUAL(counter2, 100); | BOOST_CHECK_EQUAL(counter2, 100); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(mockforward) { | BOOST_AUTO_TEST_CASE(mockforward) { | ||||
CScheduler scheduler; | CScheduler scheduler; | ||||
Show All 13 Lines | BOOST_AUTO_TEST_CASE(mockforward) { | ||||
std::thread scheduler_thread([&]() { scheduler.serviceQueue(); }); | std::thread scheduler_thread([&]() { scheduler.serviceQueue(); }); | ||||
// bump the scheduler forward 5 minutes | // bump the scheduler forward 5 minutes | ||||
scheduler.MockForward(std::chrono::minutes{5}); | scheduler.MockForward(std::chrono::minutes{5}); | ||||
// ensure scheduler has chance to process all tasks queued for before 1 ms | // ensure scheduler has chance to process all tasks queued for before 1 ms | ||||
// from now. | // from now. | ||||
scheduler.scheduleFromNow([&scheduler] { scheduler.stop(false); }, | scheduler.scheduleFromNow([&scheduler] { scheduler.stop(); }, | ||||
std::chrono::milliseconds{1}); | std::chrono::milliseconds{1}); | ||||
scheduler_thread.join(); | scheduler_thread.join(); | ||||
// check that the queue only has one job remaining | // check that the queue only has one job remaining | ||||
num_tasks = scheduler.getQueueInfo(first, last); | num_tasks = scheduler.getQueueInfo(first, last); | ||||
BOOST_CHECK_EQUAL(num_tasks, 1ul); | BOOST_CHECK_EQUAL(num_tasks, 1ul); | ||||
// check that the dummy function actually ran | // check that the dummy function actually ran | ||||
Show All 12 Lines |