blob: 2779b2464179463f0b0993c2756f0288ef67b03b [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 Pesavento19779d82019-02-14 13:40:04 -05003 * Copyright (c) 2014-2019, 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
26#include "limited-io.hpp"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060027#include "core/logger.hpp"
Davide Pesavento3dade002019-03-19 11:29:56 -060028#include "daemon/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
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070032namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070033namespace tests {
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070034
Davide Pesaventoa3148082018-04-12 18:21:54 -040035NFD_LOG_INIT(tests.LimitedIo);
Junxiao Shi7e2413b2014-03-02 11:15:09 -070036
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070037const int LimitedIo::UNLIMITED_OPS = std::numeric_limits<int>::max();
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070038const time::nanoseconds LimitedIo::UNLIMITED_TIME = time::nanoseconds::min();
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070039
Junxiao Shi3cb4fc62014-12-25 22:17:39 -070040LimitedIo::LimitedIo(UnitTestTimeFixture* uttf)
41 : m_uttf(uttf)
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070042{
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070043}
44
45LimitedIo::StopReason
Junxiao Shi3cb4fc62014-12-25 22:17:39 -070046LimitedIo::run(int nOpsLimit, const time::nanoseconds& timeLimit, const time::nanoseconds& tick)
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070047{
48 BOOST_ASSERT(!m_isRunning);
Junxiao Shi82e7f582014-09-07 15:15:40 -070049
50 if (nOpsLimit <= 0) {
51 return EXCEED_OPS;
52 }
53
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070054 m_isRunning = true;
Junxiao Shi98e29f42014-03-31 10:27:26 -070055
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070056 m_reason = NO_WORK;
57 m_nOpsRemaining = nOpsLimit;
Davide Pesaventoe4b22382018-06-10 14:37:24 -040058 if (timeLimit >= 0_ns) {
Davide Pesavento3dade002019-03-19 11:29:56 -060059 m_timeout = getScheduler().schedule(timeLimit, [this] { afterTimeout(); });
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070060 }
Junxiao Shi98e29f42014-03-31 10:27:26 -070061
Junxiao Shi7e2413b2014-03-02 11:15:09 -070062 try {
Junxiao Shi3cb4fc62014-12-25 22:17:39 -070063 if (m_uttf == nullptr) {
64 getGlobalIoService().run();
65 }
66 else {
67 // timeLimit is enforced by afterTimeout
68 m_uttf->advanceClocks(tick, time::nanoseconds::max());
69 }
70 }
Davide Pesaventof35a5a92015-12-18 18:57:45 +010071 catch (const StopException&) {
Junxiao Shi7e2413b2014-03-02 11:15:09 -070072 }
Davide Pesaventof35a5a92015-12-18 18:57:45 +010073 catch (const std::exception& ex) {
Davide Pesavento97e33022019-02-14 16:00:50 -050074 NFD_LOG_ERROR("LimitedIo::run: " << boost::diagnostic_information(ex));
Junxiao Shi7e2413b2014-03-02 11:15:09 -070075 m_reason = EXCEPTION;
Davide Pesaventof35a5a92015-12-18 18:57:45 +010076 m_lastException = std::current_exception();
Junxiao Shi7e2413b2014-03-02 11:15:09 -070077 }
Junxiao Shi98e29f42014-03-31 10:27:26 -070078
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070079 getGlobalIoService().reset();
Davide Pesavento3dade002019-03-19 11:29:56 -060080 m_timeout.cancel();
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070081 m_isRunning = false;
Davide Pesaventof35a5a92015-12-18 18:57:45 +010082
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070083 return m_reason;
84}
85
86void
87LimitedIo::afterOp()
88{
Junxiao Shi82e7f582014-09-07 15:15:40 -070089 if (!m_isRunning) {
90 // Do not proceed further if .afterOp() is invoked out of .run(),
Junxiao Shi82e7f582014-09-07 15:15:40 -070091 return;
92 }
93
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070094 --m_nOpsRemaining;
Davide Pesaventof35a5a92015-12-18 18:57:45 +010095
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070096 if (m_nOpsRemaining <= 0) {
97 m_reason = EXCEED_OPS;
98 getGlobalIoService().stop();
Junxiao Shi3cb4fc62014-12-25 22:17:39 -070099 if (m_uttf != nullptr) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500100 NDN_THROW(StopException());
Junxiao Shi3cb4fc62014-12-25 22:17:39 -0700101 }
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700102 }
103}
104
105void
106LimitedIo::afterTimeout()
107{
108 m_reason = EXCEED_TIME;
109 getGlobalIoService().stop();
Junxiao Shi3cb4fc62014-12-25 22:17:39 -0700110 if (m_uttf != nullptr) {
Davide Pesavento19779d82019-02-14 13:40:04 -0500111 NDN_THROW(StopException());
Junxiao Shi3cb4fc62014-12-25 22:17:39 -0700112 }
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700113}
114
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700115} // namespace tests
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700116} // namespace nfd