blob: 1d98ab2bdf21a65c04ffb4378b1d82265e2c76fc [file] [log] [blame]
Ashlesh Gawande08784d42017-09-06 23:40:21 -05001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Davide Pesavento1af79492023-09-22 15:45:21 -04003 * Copyright (c) 2012-2023 University of California, Los Angeles
Qiuhan Dingd71a08a2014-12-30 18:56:15 -08004 *
Ashlesh Gawande08784d42017-09-06 23:40:21 -05005 * This file is part of ChronoSync, synchronization library for distributed realtime
6 * applications for NDN.
Qiuhan Dingd71a08a2014-12-30 18:56:15 -08007 *
Ashlesh Gawande08784d42017-09-06 23:40:21 -05008 * ChronoSync is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation, either
10 * version 3 of the License, or (at your option) any later version.
Qiuhan Dingd71a08a2014-12-30 18:56:15 -080011 *
Ashlesh Gawande08784d42017-09-06 23:40:21 -050012 * ChronoSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
Qiuhan Dingd71a08a2014-12-30 18:56:15 -080015 *
Ashlesh Gawande08784d42017-09-06 23:40:21 -050016 * You should have received a copy of the GNU General Public License along with
17 * ChronoSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Qiuhan Dingd71a08a2014-12-30 18:56:15 -080018 */
19
Davide Pesavento8663ed12022-07-23 03:04:27 -040020#ifndef CHRONOSYNC_TESTS_UNIT_TEST_TIME_FIXTURE_HPP
21#define CHRONOSYNC_TESTS_UNIT_TEST_TIME_FIXTURE_HPP
Qiuhan Dingd71a08a2014-12-30 18:56:15 -080022
23#include <ndn-cxx/util/time-unit-test-clock.hpp>
Ashlesh Gawande08784d42017-09-06 23:40:21 -050024
Davide Pesavento1af79492023-09-22 15:45:21 -040025#include <boost/asio/io_context.hpp>
Qiuhan Dingd71a08a2014-12-30 18:56:15 -080026
Davide Pesavento8663ed12022-07-23 03:04:27 -040027namespace ndn::tests {
Qiuhan Dingd71a08a2014-12-30 18:56:15 -080028
29class UnitTestTimeFixture
30{
31public:
32 UnitTestTimeFixture()
33 : steadyClock(make_shared<time::UnitTestSteadyClock>())
34 , systemClock(make_shared<time::UnitTestSystemClock>())
35 {
36 time::setCustomClocks(steadyClock, systemClock);
37 }
38
39 ~UnitTestTimeFixture()
40 {
41 time::setCustomClocks(nullptr, nullptr);
42 }
43
44 void
Davide Pesavento1af79492023-09-22 15:45:21 -040045 advanceClocks(time::nanoseconds tick, size_t nTicks = 1)
Qiuhan Dingd71a08a2014-12-30 18:56:15 -080046 {
47 for (size_t i = 0; i < nTicks; ++i) {
48 steadyClock->advance(tick);
49 systemClock->advance(tick);
50
Davide Pesavento1af79492023-09-22 15:45:21 -040051 if (io.stopped()) {
52 io.restart();
53 }
Qiuhan Dingd71a08a2014-12-30 18:56:15 -080054 io.poll();
55 }
56 }
57
58public:
59 shared_ptr<time::UnitTestSteadyClock> steadyClock;
60 shared_ptr<time::UnitTestSystemClock> systemClock;
Davide Pesavento1af79492023-09-22 15:45:21 -040061 boost::asio::io_context io;
Qiuhan Dingd71a08a2014-12-30 18:56:15 -080062};
63
Davide Pesavento8663ed12022-07-23 03:04:27 -040064} // namespace ndn::tests
Qiuhan Dingd71a08a2014-12-30 18:56:15 -080065
Davide Pesavento8663ed12022-07-23 03:04:27 -040066#endif // CHRONOSYNC_TESTS_UNIT_TEST_TIME_FIXTURE_HPP