blob: 3e4be61aa46b24f8efd4e122262a6d7e70f4b399 [file] [log] [blame]
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi1e46be32015-01-08 20:18:05 -07003 * 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 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 Shi98e29f42014-03-31 10:27:26 -070030#include "core/global-io.hpp"
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070031#include "core/scheduler.hpp"
32
33namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070034namespace tests {
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070035
Junxiao Shi7e2413b2014-03-02 11:15:09 -070036/** \brief provides IO operations limit and/or time limit for unit testing
37 */
Junxiao Shi0e42c572014-10-05 20:33:48 -070038class LimitedIo : noncopyable
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070039{
40public:
41 LimitedIo();
Junxiao Shi98e29f42014-03-31 10:27:26 -070042
Junxiao Shi3cb4fc62014-12-25 22:17:39 -070043 /** \brief construct with UnitTestTimeFixture
44 */
45 LimitedIo(UnitTestTimeFixture* uttf);
46
Junxiao Shi7e2413b2014-03-02 11:15:09 -070047 /// indicates why .run returns
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070048 enum StopReason
49 {
Junxiao Shi0e42c572014-10-05 20:33:48 -070050 /// g_io.run() returns normally because there's no work to do
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070051 NO_WORK,
Junxiao Shi7e2413b2014-03-02 11:15:09 -070052 /// .afterOp() has been invoked nOpsLimit times
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070053 EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -070054 /// nTimeLimit has elapsed
55 EXCEED_TIME,
56 /// an exception is thrown
57 EXCEPTION
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070058 };
Junxiao Shi98e29f42014-03-31 10:27:26 -070059
Junxiao Shi7e2413b2014-03-02 11:15:09 -070060 /** \brief g_io.run() with operation count and/or time limit
Junxiao Shi7e2413b2014-03-02 11:15:09 -070061 * \param nOpsLimit operation count limit, pass UNLIMITED_OPS for no limit
Junxiao Shi455581d2014-11-17 18:38:40 -070062 * \param timeLimit time limit, pass UNLIMITED_TIME for no limit
Junxiao Shi3cb4fc62014-12-25 22:17:39 -070063 * \param tick if this LimitedIo is constructed with UnitTestTimeFixture,
64 * this is passed to .advanceClocks(), otherwise ignored
Junxiao Shi7e2413b2014-03-02 11:15:09 -070065 */
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070066 StopReason
Junxiao Shi3cb4fc62014-12-25 22:17:39 -070067 run(int nOpsLimit, const time::nanoseconds& timeLimit,
68 const time::nanoseconds& tick = time::milliseconds(1));
Junxiao Shi98e29f42014-03-31 10:27:26 -070069
Junxiao Shi7e2413b2014-03-02 11:15:09 -070070 /// count an operation
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070071 void
72 afterOp();
Junxiao Shi98e29f42014-03-31 10:27:26 -070073
Junxiao Shi7e2413b2014-03-02 11:15:09 -070074 const std::exception&
75 getLastException() const;
Junxiao Shi98e29f42014-03-31 10:27:26 -070076
Junxiao Shi0e42c572014-10-05 20:33:48 -070077 /** \brief defer for specified duration
78 *
79 * equivalent to .run(UNLIMITED_OPS, d)
80 */
81 void
82 defer(const time::nanoseconds& d)
83 {
84 this->run(UNLIMITED_OPS, d);
85 }
86
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070087private:
Junxiao Shi3cb4fc62014-12-25 22:17:39 -070088 /** \brief an exception to stop IO operation
89 */
90 class StopException
91 {
92 };
93
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070094 void
95 afterTimeout();
96
97public:
98 static const int UNLIMITED_OPS;
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070099 static const time::nanoseconds UNLIMITED_TIME;
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700100
101private:
Junxiao Shi3cb4fc62014-12-25 22:17:39 -0700102 UnitTestTimeFixture* m_uttf;
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700103 bool m_isRunning;
104 int m_nOpsRemaining;
Junxiao Shi1e46be32015-01-08 20:18:05 -0700105 scheduler::EventId m_timeout;
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700106 StopReason m_reason;
Junxiao Shi7e2413b2014-03-02 11:15:09 -0700107 std::exception m_lastException;
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700108};
109
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700110} // namespace tests
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700111} // namespace nfd
112
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700113#endif // NFD_TESTS_LIMITED_IO_HPP