aboutsummaryrefslogtreecommitdiff
path: root/src/scheduler.cpp
diff options
context:
space:
mode:
authorAmiti Uttarwar <amiti@uttarwar.org>2020-01-28 15:26:32 -0800
committerAmiti Uttarwar <amiti@uttarwar.org>2020-02-13 08:59:51 -0800
commita6f63598adb880a75e1571aac58338c17fa7ad53 (patch)
tree39e76485823c1cc23f734cf84f931cab7282b796 /src/scheduler.cpp
parentaabec94541e23a67a9f30dc2c80dab3383a01737 (diff)
downloadbitcoin-a6f63598adb880a75e1571aac58338c17fa7ad53.tar.xz
[util] allow scheduler to be mocked
Add MockForward method to the scheduler that mimics going into the future by rescheduling all items on the taskQueue to be sooner.
Diffstat (limited to 'src/scheduler.cpp')
-rw-r--r--src/scheduler.cpp22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/scheduler.cpp b/src/scheduler.cpp
index 927a3f3820..72cca89d99 100644
--- a/src/scheduler.cpp
+++ b/src/scheduler.cpp
@@ -114,6 +114,28 @@ void CScheduler::scheduleFromNow(CScheduler::Function f, int64_t deltaMilliSecon
schedule(f, boost::chrono::system_clock::now() + 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<boost::mutex> lock(newTaskMutex);
+
+ // use temp_queue to maintain updated schedule
+ std::multimap<boost::chrono::system_clock::time_point, Function> 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::Function f, int64_t deltaMilliSeconds)
{
f();