blob: 7b541907b8e4eca10684ff131e9fd749ec004bca [file] [log] [blame]
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoa3148082018-04-12 18:21:54 -04002/*
Davide Pesaventod91fe6d2023-10-04 21:40:02 -04003 * Copyright (c) 2014-2023, Regents of the University of California,
Davide Pesaventof35a5a92015-12-18 18:57:45 +01004 * 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.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Junxiao Shi82e7f582014-09-07 15:15:40 -070024 */
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070025
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040026#include "tests/daemon/limited-io.hpp"
27#include "tests/test-common.hpp"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040028#include "common/global.hpp"
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070029
Davide Pesavento97e33022019-02-14 16:00:50 -050030#include <boost/exception/diagnostic_information.hpp>
31
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040032namespace nfd::tests {
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070033
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040034LimitedIo::LimitedIo(GlobalIoTimeFixture* fixture)
35 : m_fixture(fixture)
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070036{
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070037}
38
39LimitedIo::StopReason
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040040LimitedIo::run(int nOpsLimit, time::nanoseconds timeLimit, time::nanoseconds tick)
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070041{
42 BOOST_ASSERT(!m_isRunning);
Junxiao Shi82e7f582014-09-07 15:15:40 -070043
44 if (nOpsLimit <= 0) {
45 return EXCEED_OPS;
46 }
47
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070048 m_isRunning = true;
Junxiao Shi98e29f42014-03-31 10:27:26 -070049
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070050 m_reason = NO_WORK;
51 m_nOpsRemaining = nOpsLimit;
Davide Pesaventoe4b22382018-06-10 14:37:24 -040052 if (timeLimit >= 0_ns) {
Davide Pesavento3dade002019-03-19 11:29:56 -060053 m_timeout = getScheduler().schedule(timeLimit, [this] { afterTimeout(); });
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070054 }
Junxiao Shi98e29f42014-03-31 10:27:26 -070055
Junxiao Shi7e2413b2014-03-02 11:15:09 -070056 try {
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040057 if (m_fixture == nullptr) {
Junxiao Shi3cb4fc62014-12-25 22:17:39 -070058 getGlobalIoService().run();
59 }
60 else {
61 // timeLimit is enforced by afterTimeout
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040062 m_fixture->advanceClocks(tick, time::nanoseconds::max());
Junxiao Shi3cb4fc62014-12-25 22:17:39 -070063 }
64 }
Davide Pesaventof35a5a92015-12-18 18:57:45 +010065 catch (const StopException&) {
Junxiao Shi7e2413b2014-03-02 11:15:09 -070066 }
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040067 catch (...) {
68 BOOST_WARN_MESSAGE(false, boost::current_exception_diagnostic_information());
Junxiao Shi7e2413b2014-03-02 11:15:09 -070069 m_reason = EXCEPTION;
Davide Pesaventof35a5a92015-12-18 18:57:45 +010070 m_lastException = std::current_exception();
Junxiao Shi7e2413b2014-03-02 11:15:09 -070071 }
Junxiao Shi98e29f42014-03-31 10:27:26 -070072
Davide Pesaventod91fe6d2023-10-04 21:40:02 -040073 getGlobalIoService().restart();
Davide Pesavento3dade002019-03-19 11:29:56 -060074 m_timeout.cancel();
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070075 m_isRunning = false;
Davide Pesaventof35a5a92015-12-18 18:57:45 +010076
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070077 return m_reason;
78}
79
80void
81LimitedIo::afterOp()
82{
Junxiao Shi82e7f582014-09-07 15:15:40 -070083 if (!m_isRunning) {
84 // Do not proceed further if .afterOp() is invoked out of .run(),
Junxiao Shi82e7f582014-09-07 15:15:40 -070085 return;
86 }
87
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070088 --m_nOpsRemaining;
Davide Pesaventof35a5a92015-12-18 18:57:45 +010089
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070090 if (m_nOpsRemaining <= 0) {
91 m_reason = EXCEED_OPS;
92 getGlobalIoService().stop();
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040093 if (m_fixture != nullptr) {
Davide Pesavento19779d82019-02-14 13:40:04 -050094 NDN_THROW(StopException());
Junxiao Shi3cb4fc62014-12-25 22:17:39 -070095 }
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070096 }
97}
98
99void
100LimitedIo::afterTimeout()
101{
102 m_reason = EXCEED_TIME;
103 getGlobalIoService().stop();
Davide Pesaventocf7db2f2019-03-24 23:17:28 -0400104 if (m_fixture != nullptr) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500105 NDN_THROW(StopException());
Junxiao Shi3cb4fc62014-12-25 22:17:39 -0700106 }
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700107}
108
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400109} // namespace nfd::tests