blob: dfc1a098dc6f21f0d1f92ae5fb29e7e2e5f39d8c [file] [log] [blame]
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesaventof35a5a92015-12-18 18:57:45 +01003 * Copyright (c) 2014-2015, Regents of the University of California,
4 * 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"
Davide Pesaventof35a5a92015-12-18 18:57:45 +010027#include "core/extended-error-message.hpp"
28#include "core/global-io.hpp"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060029#include "core/logger.hpp"
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070030
31namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070032namespace tests {
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070033
Junxiao Shi7e2413b2014-03-02 11:15:09 -070034NFD_LOG_INIT("LimitedIo");
35
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070036const int LimitedIo::UNLIMITED_OPS = std::numeric_limits<int>::max();
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070037const time::nanoseconds LimitedIo::UNLIMITED_TIME = time::nanoseconds::min();
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070038
Junxiao Shi3cb4fc62014-12-25 22:17:39 -070039LimitedIo::LimitedIo(UnitTestTimeFixture* uttf)
40 : m_uttf(uttf)
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070041 , m_nOpsRemaining(0)
Davide Pesaventof35a5a92015-12-18 18:57:45 +010042 , m_isRunning(false)
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070043{
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070044}
45
46LimitedIo::StopReason
Junxiao Shi3cb4fc62014-12-25 22:17:39 -070047LimitedIo::run(int nOpsLimit, const time::nanoseconds& timeLimit, const time::nanoseconds& tick)
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070048{
49 BOOST_ASSERT(!m_isRunning);
Junxiao Shi82e7f582014-09-07 15:15:40 -070050
51 if (nOpsLimit <= 0) {
52 return EXCEED_OPS;
53 }
54
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070055 m_isRunning = true;
Junxiao Shi98e29f42014-03-31 10:27:26 -070056
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070057 m_reason = NO_WORK;
58 m_nOpsRemaining = nOpsLimit;
Junxiao Shi455581d2014-11-17 18:38:40 -070059 if (timeLimit >= time::nanoseconds::zero()) {
60 m_timeout = scheduler::schedule(timeLimit, bind(&LimitedIo::afterTimeout, this));
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070061 }
Junxiao Shi98e29f42014-03-31 10:27:26 -070062
Junxiao Shi7e2413b2014-03-02 11:15:09 -070063 try {
Junxiao Shi3cb4fc62014-12-25 22:17:39 -070064 if (m_uttf == nullptr) {
65 getGlobalIoService().run();
66 }
67 else {
68 // timeLimit is enforced by afterTimeout
69 m_uttf->advanceClocks(tick, time::nanoseconds::max());
70 }
71 }
Davide Pesaventof35a5a92015-12-18 18:57:45 +010072 catch (const StopException&) {
Junxiao Shi7e2413b2014-03-02 11:15:09 -070073 }
Davide Pesaventof35a5a92015-12-18 18:57:45 +010074 catch (const std::exception& ex) {
75 NFD_LOG_ERROR("g_io.run() exception: " << getExtendedErrorMessage(ex));
Junxiao Shi7e2413b2014-03-02 11:15:09 -070076 m_reason = EXCEPTION;
Davide Pesaventof35a5a92015-12-18 18:57:45 +010077 m_lastException = std::current_exception();
Junxiao Shi7e2413b2014-03-02 11:15:09 -070078 }
Junxiao Shi98e29f42014-03-31 10:27:26 -070079
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070080 getGlobalIoService().reset();
81 scheduler::cancel(m_timeout);
82 m_isRunning = false;
Davide Pesaventof35a5a92015-12-18 18:57:45 +010083
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070084 return m_reason;
85}
86
87void
88LimitedIo::afterOp()
89{
Junxiao Shi82e7f582014-09-07 15:15:40 -070090 if (!m_isRunning) {
91 // Do not proceed further if .afterOp() is invoked out of .run(),
Junxiao Shi82e7f582014-09-07 15:15:40 -070092 return;
93 }
94
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070095 --m_nOpsRemaining;
Davide Pesaventof35a5a92015-12-18 18:57:45 +010096
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070097 if (m_nOpsRemaining <= 0) {
98 m_reason = EXCEED_OPS;
99 getGlobalIoService().stop();
Junxiao Shi3cb4fc62014-12-25 22:17:39 -0700100 if (m_uttf != nullptr) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700101 BOOST_THROW_EXCEPTION(StopException());
Junxiao Shi3cb4fc62014-12-25 22:17:39 -0700102 }
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700103 }
104}
105
106void
107LimitedIo::afterTimeout()
108{
109 m_reason = EXCEED_TIME;
110 getGlobalIoService().stop();
Junxiao Shi3cb4fc62014-12-25 22:17:39 -0700111 if (m_uttf != nullptr) {
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -0700112 BOOST_THROW_EXCEPTION(StopException());
Junxiao Shi3cb4fc62014-12-25 22:17:39 -0700113 }
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700114}
115
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700116} // namespace tests
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700117} // namespace nfd