aboutsummaryrefslogtreecommitdiff
path: root/src/test/scheduler_tests.cpp
diff options
context:
space:
mode:
authorAmiti Uttarwar <amiti@uttarwar.org>2020-01-28 15:36:47 -0800
committerAmiti Uttarwar <amiti@uttarwar.org>2020-02-13 08:59:51 -0800
commit1cd43e83c6e8d81e950aaaede7a8a51505d0a2bc (patch)
tree7103e639d85d7c7c8ca0d78c81f95924c3edb1e7 /src/test/scheduler_tests.cpp
parenta6f63598adb880a75e1571aac58338c17fa7ad53 (diff)
[test] unit test for new MockForward scheduler method
Diffstat (limited to 'src/test/scheduler_tests.cpp')
-rw-r--r--src/test/scheduler_tests.cpp43
1 files changed, 41 insertions, 2 deletions
diff --git a/src/test/scheduler_tests.cpp b/src/test/scheduler_tests.cpp
index b292d5b0d0..a6cb34cf28 100644
--- a/src/test/scheduler_tests.cpp
+++ b/src/test/scheduler_tests.cpp
@@ -5,8 +5,6 @@
#include <random.h>
#include <scheduler.h>
-#include <test/util/setup_common.h>
-
#include <boost/thread.hpp>
#include <boost/test/unit_test.hpp>
@@ -155,4 +153,45 @@ BOOST_AUTO_TEST_CASE(singlethreadedscheduler_ordered)
BOOST_CHECK_EQUAL(counter2, 100);
}
+BOOST_AUTO_TEST_CASE(mockforward)
+{
+ CScheduler scheduler;
+
+ int counter{0};
+ CScheduler::Function dummy = [&counter]{counter++;};
+
+ // schedule jobs for 2, 5 & 8 minutes into the future
+ int64_t min_in_milli = 60*1000;
+ scheduler.scheduleFromNow(dummy, 2*min_in_milli);
+ scheduler.scheduleFromNow(dummy, 5*min_in_milli);
+ scheduler.scheduleFromNow(dummy, 8*min_in_milli);
+
+ // check taskQueue
+ boost::chrono::system_clock::time_point first, last;
+ size_t num_tasks = scheduler.getQueueInfo(first, last);
+ BOOST_CHECK_EQUAL(num_tasks, 3ul);
+
+ std::thread scheduler_thread([&]() { scheduler.serviceQueue(); });
+
+ // bump the scheduler forward 5 minutes
+ scheduler.MockForward(boost::chrono::seconds(5*60));
+
+ // ensure scheduler has chance to process all tasks queued for before 1 ms from now.
+ scheduler.scheduleFromNow([&scheduler]{ scheduler.stop(false); }, 1);
+ scheduler_thread.join();
+
+ // check that the queue only has one job remaining
+ num_tasks = scheduler.getQueueInfo(first, last);
+ BOOST_CHECK_EQUAL(num_tasks, 1ul);
+
+ // check that the dummy function actually ran
+ BOOST_CHECK_EQUAL(counter, 2);
+
+ // check that the time of the remaining job has been updated
+ boost::chrono::system_clock::time_point now = boost::chrono::system_clock::now();
+ int delta = boost::chrono::duration_cast<boost::chrono::seconds>(first - now).count();
+ // should be between 2 & 3 minutes from now
+ BOOST_CHECK(delta > 2*60 && delta < 3*60);
+}
+
BOOST_AUTO_TEST_SUITE_END()