Changeset View
Changeset View
Standalone View
Standalone View
src/test/scheduler_tests.cpp
Show First 20 Lines • Show All 146 Lines • ▼ Show 20 Lines | scheduler.scheduleEvery( | ||||
// We reached the end of our test, make sure nothing run again for | // We reached the end of our test, make sure nothing run again for | ||||
// 100ms. | // 100ms. | ||||
scheduler.scheduleFromNow( | scheduler.scheduleFromNow( | ||||
[&keepRunning, &cvar]() { | [&keepRunning, &cvar]() { | ||||
keepRunning = false; | keepRunning = false; | ||||
cvar.notify_all(); | cvar.notify_all(); | ||||
}, | }, | ||||
100); | std::chrono::milliseconds{100}); | ||||
// We set the counter to some magic value to check the scheduler | // We set the counter to some magic value to check the scheduler | ||||
// empty its queue properly after 120ms. | // empty its queue properly after 120ms. | ||||
scheduler.scheduleFromNow([&counter]() { counter = 42; }, 120); | scheduler.scheduleFromNow([&counter]() { counter = 42; }, | ||||
std::chrono::milliseconds{120}); | |||||
return false; | return false; | ||||
}, | }, | ||||
5); | std::chrono::milliseconds{5}); | ||||
// Start the scheduler thread. | // Start the scheduler thread. | ||||
std::thread schedulerThread( | std::thread schedulerThread( | ||||
std::bind(&CScheduler::serviceQueue, &scheduler)); | std::bind(&CScheduler::serviceQueue, &scheduler)); | ||||
Mutex mutex; | Mutex mutex; | ||||
WAIT_LOCK(mutex, lock); | WAIT_LOCK(mutex, lock); | ||||
while (keepRunning) { | while (keepRunning) { | ||||
▲ Show 20 Lines • Show All 55 Lines • ▼ Show 20 Lines | |||||
BOOST_AUTO_TEST_CASE(mockforward) { | BOOST_AUTO_TEST_CASE(mockforward) { | ||||
CScheduler scheduler; | CScheduler scheduler; | ||||
int counter{0}; | int counter{0}; | ||||
CScheduler::Function dummy = [&counter] { counter++; }; | CScheduler::Function dummy = [&counter] { counter++; }; | ||||
// schedule jobs for 2, 5 & 8 minutes into the future | // schedule jobs for 2, 5 & 8 minutes into the future | ||||
int64_t min_in_milli = 60 * 1000; | scheduler.scheduleFromNow(dummy, std::chrono::minutes{2}); | ||||
scheduler.scheduleFromNow(dummy, 2 * min_in_milli); | scheduler.scheduleFromNow(dummy, std::chrono::minutes{5}); | ||||
scheduler.scheduleFromNow(dummy, 5 * min_in_milli); | scheduler.scheduleFromNow(dummy, std::chrono::minutes{8}); | ||||
scheduler.scheduleFromNow(dummy, 8 * min_in_milli); | |||||
// check taskQueue | // check taskQueue | ||||
std::chrono::system_clock::time_point first, last; | std::chrono::system_clock::time_point first, last; | ||||
size_t num_tasks = scheduler.getQueueInfo(first, last); | size_t num_tasks = scheduler.getQueueInfo(first, last); | ||||
BOOST_CHECK_EQUAL(num_tasks, 3ul); | BOOST_CHECK_EQUAL(num_tasks, 3ul); | ||||
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::seconds(5 * 60)); | 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); }, 1); | scheduler.scheduleFromNow([&scheduler] { scheduler.stop(false); }, | ||||
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 | ||||
BOOST_CHECK_EQUAL(counter, 2); | BOOST_CHECK_EQUAL(counter, 2); | ||||
Show All 11 Lines |