blob: 765511c62afe48ddcb0f055fbc977161568b1e40 [file] [log] [blame]
Alexander Afanasyevfde570c2016-12-19 16:02:55 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventobdd88c12020-11-26 00:35:08 -05002/*
Davide Pesavento9a0d2132024-02-10 16:55:04 -05003 * Copyright (c) 2014-2024, Regents of the University of California,
Alexander Afanasyevfde570c2016-12-19 16:02:55 -08004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
10 *
11 * This file is part of NDNS (Named Data Networking Domain Name Service) and is
12 * based on the code written as part of NFD (Named Data Networking Daemon).
13 * See AUTHORS.md for complete list of NDNS authors and contributors.
14 *
15 * NDNS is free software: you can redistribute it and/or modify it under the terms
16 * of the GNU General Public License as published by the Free Software Foundation,
17 * either version 3 of the License, or (at your option) any later version.
18 *
19 * NDNS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
20 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
21 * PURPOSE. See the GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along with
24 * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
25 */
26
Davide Pesaventobdd88c12020-11-26 00:35:08 -050027#include "clock-fixture.hpp"
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080028
Davide Pesavento9a0d2132024-02-10 16:55:04 -050029namespace ndn::ndns::tests {
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080030
Davide Pesaventobdd88c12020-11-26 00:35:08 -050031ClockFixture::ClockFixture()
Davide Pesavento9a0d2132024-02-10 16:55:04 -050032 : m_steadyClock(std::make_shared<time::UnitTestSteadyClock>())
33 , m_systemClock(std::make_shared<time::UnitTestSystemClock>())
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080034{
Davide Pesaventobdd88c12020-11-26 00:35:08 -050035 time::setCustomClocks(m_steadyClock, m_systemClock);
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080036}
37
Davide Pesaventobdd88c12020-11-26 00:35:08 -050038ClockFixture::~ClockFixture()
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080039{
40 time::setCustomClocks(nullptr, nullptr);
41}
42
43void
Davide Pesaventobdd88c12020-11-26 00:35:08 -050044ClockFixture::advanceClocks(time::nanoseconds tick, time::nanoseconds total)
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080045{
46 BOOST_ASSERT(tick > time::nanoseconds::zero());
47 BOOST_ASSERT(total >= time::nanoseconds::zero());
48
Davide Pesaventobdd88c12020-11-26 00:35:08 -050049 while (total > time::nanoseconds::zero()) {
50 auto t = std::min(tick, total);
51 m_steadyClock->advance(t);
52 m_systemClock->advance(t);
53 total -= t;
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080054
Davide Pesaventobdd88c12020-11-26 00:35:08 -050055 afterTick();
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080056 }
57}
58
Davide Pesavento9a0d2132024-02-10 16:55:04 -050059} // namespace ndn::ndns::tests