Skip to content

Commit 1cd43e8

Browse files
committed
[test] unit test for new MockForward scheduler method
1 parent a6f6359 commit 1cd43e8

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

src/test/scheduler_tests.cpp

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
#include <random.h>
66
#include <scheduler.h>
77

8-
#include <test/util/setup_common.h>
9-
108
#include <boost/thread.hpp>
119
#include <boost/test/unit_test.hpp>
1210

@@ -155,4 +153,45 @@ BOOST_AUTO_TEST_CASE(singlethreadedscheduler_ordered)
155153
BOOST_CHECK_EQUAL(counter2, 100);
156154
}
157155

156+
BOOST_AUTO_TEST_CASE(mockforward)
157+
{
158+
CScheduler scheduler;
159+
160+
int counter{0};
161+
CScheduler::Function dummy = [&counter]{counter++;};
162+
163+
// schedule jobs for 2, 5 & 8 minutes into the future
164+
int64_t min_in_milli = 60*1000;
165+
scheduler.scheduleFromNow(dummy, 2*min_in_milli);
166+
scheduler.scheduleFromNow(dummy, 5*min_in_milli);
167+
scheduler.scheduleFromNow(dummy, 8*min_in_milli);
168+
169+
// check taskQueue
170+
boost::chrono::system_clock::time_point first, last;
171+
size_t num_tasks = scheduler.getQueueInfo(first, last);
172+
BOOST_CHECK_EQUAL(num_tasks, 3ul);
173+
174+
std::thread scheduler_thread([&]() { scheduler.serviceQueue(); });
175+
176+
// bump the scheduler forward 5 minutes
177+
scheduler.MockForward(boost::chrono::seconds(5*60));
178+
179+
// ensure scheduler has chance to process all tasks queued for before 1 ms from now.
180+
scheduler.scheduleFromNow([&scheduler]{ scheduler.stop(false); }, 1);
181+
scheduler_thread.join();
182+
183+
// check that the queue only has one job remaining
184+
num_tasks = scheduler.getQueueInfo(first, last);
185+
BOOST_CHECK_EQUAL(num_tasks, 1ul);
186+
187+
// check that the dummy function actually ran
188+
BOOST_CHECK_EQUAL(counter, 2);
189+
190+
// check that the time of the remaining job has been updated
191+
boost::chrono::system_clock::time_point now = boost::chrono::system_clock::now();
192+
int delta = boost::chrono::duration_cast<boost::chrono::seconds>(first - now).count();
193+
// should be between 2 & 3 minutes from now
194+
BOOST_CHECK(delta > 2*60 && delta < 3*60);
195+
}
196+
158197
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)