[test] unit test for new MockForward scheduler method

This commit is contained in:
Amiti Uttarwar 2020-01-28 15:36:47 -08:00
parent a6f63598ad
commit 1cd43e83c6

View file

@ -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()