|
5 | 5 | #include <random.h>
|
6 | 6 | #include <scheduler.h>
|
7 | 7 |
|
8 |
| -#include <test/util/setup_common.h> |
9 |
| - |
10 | 8 | #include <boost/thread.hpp>
|
11 | 9 | #include <boost/test/unit_test.hpp>
|
12 | 10 |
|
@@ -155,4 +153,45 @@ BOOST_AUTO_TEST_CASE(singlethreadedscheduler_ordered)
|
155 | 153 | BOOST_CHECK_EQUAL(counter2, 100);
|
156 | 154 | }
|
157 | 155 |
|
| 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 | + |
158 | 197 | BOOST_AUTO_TEST_SUITE_END()
|
0 commit comments