util: make SegmentFetcher test suite run faster
UnitTestTimeFixture::advanceClocks is invoked with less ticks of
larger duration when feasible, so that test cases run faster.
UnitTestTimeFixture is synchronized from NFD codebase.
refs #2734
Change-Id: Ifd338f1008af8662bb9ea8f798f6cc2a922fb865
diff --git a/tests/unit-tests/unit-test-time-fixture.hpp b/tests/unit-tests/unit-test-time-fixture.hpp
index 8d7ac17..9f43258 100644
--- a/tests/unit-tests/unit-test-time-fixture.hpp
+++ b/tests/unit-tests/unit-test-time-fixture.hpp
@@ -29,6 +29,8 @@
namespace ndn {
namespace tests {
+/** \brief a test fixture that overrides steady clock and system clock
+ */
class UnitTestTimeFixture
{
public:
@@ -44,12 +46,47 @@
time::setCustomClocks(nullptr, nullptr);
}
+ /** \brief advance steady and system clocks
+ *
+ * Clocks are advanced in increments of \p tick for \p nTicks ticks.
+ * After each tick, io_service is polled to process pending I/O events.
+ *
+ * Exceptions thrown during I/O events are propagated to the caller.
+ * Clock advancing would stop in case of an exception.
+ */
void
advanceClocks(const time::nanoseconds& tick, size_t nTicks = 1)
{
- for (size_t i = 0; i < nTicks; ++i) {
- steadyClock->advance(tick);
- systemClock->advance(tick);
+ this->advanceClocks(tick, tick * nTicks);
+ }
+
+ /** \brief advance steady and system clocks
+ *
+ * Clocks are advanced in increments of \p tick for \p total time.
+ * The last increment might be shorter than \p tick.
+ * After each tick, io_service is polled to process pending I/O events.
+ *
+ * Exceptions thrown during I/O events are propagated to the caller.
+ * Clock advancing would stop in case of an exception.
+ */
+ void
+ advanceClocks(const time::nanoseconds& tick, const time::nanoseconds& total)
+ {
+ BOOST_ASSERT(tick > time::nanoseconds::zero());
+ BOOST_ASSERT(total >= time::nanoseconds::zero());
+
+ time::nanoseconds remaining = total;
+ while (remaining > time::nanoseconds::zero()) {
+ if (remaining >= tick) {
+ steadyClock->advance(tick);
+ systemClock->advance(tick);
+ remaining -= tick;
+ }
+ else {
+ steadyClock->advance(remaining);
+ systemClock->advance(remaining);
+ remaining = time::nanoseconds::zero();
+ }
if (io.stopped())
io.reset();