blob: a42a79770238ce3c4ceef8877a1e0661c8b4283b [file] [log] [blame]
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Teng Liang952d6fd2018-05-29 21:09:52 -07002/*
3 * Copyright (c) 2014-2018, Regents of the University of California,
Junxiao Shi1e46be32015-01-08 20:18:05 -07004 * 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 Shi0e42c572014-10-05 20:33:48 -070024 */
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070025
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070026#ifndef NFD_TESTS_LIMITED_IO_HPP
27#define NFD_TESTS_LIMITED_IO_HPP
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070028
Junxiao Shi3cb4fc62014-12-25 22:17:39 -070029#include "test-common.hpp"
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070030#include "core/scheduler.hpp"
31
Davide Pesaventof35a5a92015-12-18 18:57:45 +010032#include <exception>
33
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070034namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070035namespace tests {
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070036
Junxiao Shi7e2413b2014-03-02 11:15:09 -070037/** \brief provides IO operations limit and/or time limit for unit testing
Teng Liang952d6fd2018-05-29 21:09:52 -070038 *
39 * \warning LimitedIo is incompatible with RibIoFixture
Junxiao Shi7e2413b2014-03-02 11:15:09 -070040 */
Junxiao Shi0e42c572014-10-05 20:33:48 -070041class LimitedIo : noncopyable
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070042{
43public:
Junxiao Shi3cb4fc62014-12-25 22:17:39 -070044 /** \brief construct with UnitTestTimeFixture
45 */
Davide Pesaventof35a5a92015-12-18 18:57:45 +010046 explicit
47 LimitedIo(UnitTestTimeFixture* uttf = nullptr);
Junxiao Shi3cb4fc62014-12-25 22:17:39 -070048
Junxiao Shi7e2413b2014-03-02 11:15:09 -070049 /// indicates why .run returns
Davide Pesaventof35a5a92015-12-18 18:57:45 +010050 enum StopReason {
Junxiao Shi0e42c572014-10-05 20:33:48 -070051 /// g_io.run() returns normally because there's no work to do
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070052 NO_WORK,
Junxiao Shi7e2413b2014-03-02 11:15:09 -070053 /// .afterOp() has been invoked nOpsLimit times
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070054 EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -070055 /// nTimeLimit has elapsed
56 EXCEED_TIME,
57 /// an exception is thrown
58 EXCEPTION
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070059 };
Junxiao Shi98e29f42014-03-31 10:27:26 -070060
Junxiao Shi7e2413b2014-03-02 11:15:09 -070061 /** \brief g_io.run() with operation count and/or time limit
Junxiao Shi7e2413b2014-03-02 11:15:09 -070062 * \param nOpsLimit operation count limit, pass UNLIMITED_OPS for no limit
Junxiao Shi455581d2014-11-17 18:38:40 -070063 * \param timeLimit time limit, pass UNLIMITED_TIME for no limit
Junxiao Shi3cb4fc62014-12-25 22:17:39 -070064 * \param tick if this LimitedIo is constructed with UnitTestTimeFixture,
65 * this is passed to .advanceClocks(), otherwise ignored
Junxiao Shi7e2413b2014-03-02 11:15:09 -070066 */
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070067 StopReason
Junxiao Shi3cb4fc62014-12-25 22:17:39 -070068 run(int nOpsLimit, const time::nanoseconds& timeLimit,
69 const time::nanoseconds& tick = time::milliseconds(1));
Junxiao Shi98e29f42014-03-31 10:27:26 -070070
Junxiao Shi7e2413b2014-03-02 11:15:09 -070071 /// count an operation
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070072 void
73 afterOp();
Junxiao Shi98e29f42014-03-31 10:27:26 -070074
Junxiao Shi0e42c572014-10-05 20:33:48 -070075 /** \brief defer for specified duration
76 *
77 * equivalent to .run(UNLIMITED_OPS, d)
78 */
79 void
80 defer(const time::nanoseconds& d)
81 {
82 this->run(UNLIMITED_OPS, d);
83 }
84
Davide Pesaventof35a5a92015-12-18 18:57:45 +010085 std::exception_ptr
86 getLastException() const
87 {
88 return m_lastException;
89 }
90
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070091private:
Junxiao Shi3cb4fc62014-12-25 22:17:39 -070092 /** \brief an exception to stop IO operation
93 */
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -070094 class StopException : public std::exception
Junxiao Shi3cb4fc62014-12-25 22:17:39 -070095 {
96 };
97
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070098 void
99 afterTimeout();
100
101public:
102 static const int UNLIMITED_OPS;
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -0700103 static const time::nanoseconds UNLIMITED_TIME;
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700104
105private:
Junxiao Shi3cb4fc62014-12-25 22:17:39 -0700106 UnitTestTimeFixture* m_uttf;
Davide Pesaventof35a5a92015-12-18 18:57:45 +0100107 StopReason m_reason;
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700108 int m_nOpsRemaining;
Junxiao Shi1e46be32015-01-08 20:18:05 -0700109 scheduler::EventId m_timeout;
Davide Pesaventof35a5a92015-12-18 18:57:45 +0100110 std::exception_ptr m_lastException;
111 bool m_isRunning;
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700112};
113
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700114} // namespace tests
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700115} // namespace nfd
116
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700117#endif // NFD_TESTS_LIMITED_IO_HPP