Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014, 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 Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 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 Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 24 | */ |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 25 | |
| 26 | #include "limited-io.hpp" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 27 | #include "core/logger.hpp" |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 28 | |
| 29 | namespace nfd { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 30 | namespace tests { |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 31 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 32 | NFD_LOG_INIT("LimitedIo"); |
| 33 | |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 34 | const int LimitedIo::UNLIMITED_OPS = std::numeric_limits<int>::max(); |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 35 | const time::nanoseconds LimitedIo::UNLIMITED_TIME = time::nanoseconds::min(); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 36 | |
| 37 | LimitedIo::LimitedIo() |
Junxiao Shi | 3cb4fc6 | 2014-12-25 22:17:39 -0700 | [diff] [blame] | 38 | : m_uttf(nullptr) |
| 39 | , m_isRunning(false) |
| 40 | , m_nOpsRemaining(0) |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | LimitedIo::LimitedIo(UnitTestTimeFixture* uttf) |
| 45 | : m_uttf(uttf) |
| 46 | , m_isRunning(false) |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 47 | , m_nOpsRemaining(0) |
| 48 | { |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | LimitedIo::StopReason |
Junxiao Shi | 3cb4fc6 | 2014-12-25 22:17:39 -0700 | [diff] [blame] | 52 | LimitedIo::run(int nOpsLimit, const time::nanoseconds& timeLimit, const time::nanoseconds& tick) |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 53 | { |
| 54 | BOOST_ASSERT(!m_isRunning); |
Junxiao Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 55 | |
| 56 | if (nOpsLimit <= 0) { |
| 57 | return EXCEED_OPS; |
| 58 | } |
| 59 | |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 60 | m_isRunning = true; |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 61 | |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 62 | m_reason = NO_WORK; |
| 63 | m_nOpsRemaining = nOpsLimit; |
Junxiao Shi | 455581d | 2014-11-17 18:38:40 -0700 | [diff] [blame] | 64 | if (timeLimit >= time::nanoseconds::zero()) { |
| 65 | m_timeout = scheduler::schedule(timeLimit, bind(&LimitedIo::afterTimeout, this)); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 66 | } |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 67 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 68 | try { |
Junxiao Shi | 3cb4fc6 | 2014-12-25 22:17:39 -0700 | [diff] [blame] | 69 | if (m_uttf == nullptr) { |
| 70 | getGlobalIoService().run(); |
| 71 | } |
| 72 | else { |
| 73 | // timeLimit is enforced by afterTimeout |
| 74 | m_uttf->advanceClocks(tick, time::nanoseconds::max()); |
| 75 | } |
| 76 | } |
| 77 | catch (StopException&) { |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 78 | } |
| 79 | catch (std::exception& ex) { |
| 80 | m_reason = EXCEPTION; |
| 81 | NFD_LOG_ERROR("g_io.run() exception: " << ex.what()); |
| 82 | m_lastException = ex; |
| 83 | } |
Junxiao Shi | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame] | 84 | |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 85 | getGlobalIoService().reset(); |
| 86 | scheduler::cancel(m_timeout); |
| 87 | m_isRunning = false; |
| 88 | return m_reason; |
| 89 | } |
| 90 | |
| 91 | void |
| 92 | LimitedIo::afterOp() |
| 93 | { |
Junxiao Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 94 | if (!m_isRunning) { |
| 95 | // Do not proceed further if .afterOp() is invoked out of .run(), |
Junxiao Shi | 82e7f58 | 2014-09-07 15:15:40 -0700 | [diff] [blame] | 96 | return; |
| 97 | } |
| 98 | |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 99 | --m_nOpsRemaining; |
| 100 | if (m_nOpsRemaining <= 0) { |
| 101 | m_reason = EXCEED_OPS; |
| 102 | getGlobalIoService().stop(); |
Junxiao Shi | 3cb4fc6 | 2014-12-25 22:17:39 -0700 | [diff] [blame] | 103 | if (m_uttf != nullptr) { |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 104 | BOOST_THROW_EXCEPTION(StopException()); |
Junxiao Shi | 3cb4fc6 | 2014-12-25 22:17:39 -0700 | [diff] [blame] | 105 | } |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
| 109 | void |
| 110 | LimitedIo::afterTimeout() |
| 111 | { |
| 112 | m_reason = EXCEED_TIME; |
| 113 | getGlobalIoService().stop(); |
Junxiao Shi | 3cb4fc6 | 2014-12-25 22:17:39 -0700 | [diff] [blame] | 114 | if (m_uttf != nullptr) { |
Spyridon Mastorakis | 149e02c | 2015-07-27 13:22:22 -0700 | [diff] [blame] | 115 | BOOST_THROW_EXCEPTION(StopException()); |
Junxiao Shi | 3cb4fc6 | 2014-12-25 22:17:39 -0700 | [diff] [blame] | 116 | } |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 119 | const std::exception& |
| 120 | LimitedIo::getLastException() const |
| 121 | { |
| 122 | return m_lastException; |
| 123 | } |
| 124 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 125 | } // namespace tests |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 126 | } // namespace nfd |