diff --git a/src/scheduler.h b/src/scheduler.h --- a/src/scheduler.h +++ b/src/scheduler.h @@ -57,6 +57,13 @@ // this method. void scheduleEvery(Predicate p, int64_t deltaMilliSeconds); + /** + * Mock the scheduler to fast forward in time. + * Iterates through items on taskQueue and reschedules them + * to be delta_seconds sooner. + */ + void MockForward(boost::chrono::seconds delta_seconds); + // To keep things as simple as possible, there is no unschedule. // Services the queue 'forever'. Should be run in a thread, and interrupted diff --git a/src/scheduler.cpp b/src/scheduler.cpp --- a/src/scheduler.cpp +++ b/src/scheduler.cpp @@ -100,6 +100,31 @@ boost::chrono::milliseconds(deltaMilliSeconds)); } +void CScheduler::MockForward(boost::chrono::seconds delta_seconds) { + assert(delta_seconds.count() > 0 && + delta_seconds < boost::chrono::hours{1}); + + { + boost::unique_lock lock(newTaskMutex); + + // use temp_queue to maintain updated schedule + std::multimap + temp_queue; + + for (const auto &element : taskQueue) { + temp_queue.emplace_hint(temp_queue.cend(), + element.first - delta_seconds, + element.second); + } + + // point taskQueue to temp_queue + taskQueue = std::move(temp_queue); + } + + // notify that the taskQueue needs to be processed + newTaskScheduled.notify_one(); +} + static void Repeat(CScheduler *s, CScheduler::Predicate p, int64_t deltaMilliSeconds) { if (p()) {