blob: 431032a0741790596d14c9a03348426c33686ecd [file] [log] [blame]
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi0e42c572014-10-05 20:33:48 -07003 * 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 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 Shi98e29f42014-03-31 10:27:26 -070029#include "core/global-io.hpp"
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070030#include "core/scheduler.hpp"
31
32namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070033namespace tests {
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070034
Junxiao Shi7e2413b2014-03-02 11:15:09 -070035/** \brief provides IO operations limit and/or time limit for unit testing
36 */
Junxiao Shi0e42c572014-10-05 20:33:48 -070037class LimitedIo : noncopyable
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070038{
39public:
40 LimitedIo();
Junxiao Shi98e29f42014-03-31 10:27:26 -070041
Junxiao Shi7e2413b2014-03-02 11:15:09 -070042 /// indicates why .run returns
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070043 enum StopReason
44 {
Junxiao Shi0e42c572014-10-05 20:33:48 -070045 /// g_io.run() returns normally because there's no work to do
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070046 NO_WORK,
Junxiao Shi7e2413b2014-03-02 11:15:09 -070047 /// .afterOp() has been invoked nOpsLimit times
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070048 EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -070049 /// nTimeLimit has elapsed
50 EXCEED_TIME,
51 /// an exception is thrown
52 EXCEPTION
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070053 };
Junxiao Shi98e29f42014-03-31 10:27:26 -070054
Junxiao Shi7e2413b2014-03-02 11:15:09 -070055 /** \brief g_io.run() with operation count and/or time limit
56 *
57 * \param nOpsLimit operation count limit, pass UNLIMITED_OPS for no limit
Junxiao Shi455581d2014-11-17 18:38:40 -070058 * \param timeLimit time limit, pass UNLIMITED_TIME for no limit
59 *
60 * \warning if timeLimit is used with UnitTestTimeFixture,
61 * some other code must advance steady clock in order to exceed time limit
Junxiao Shi7e2413b2014-03-02 11:15:09 -070062 */
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070063 StopReason
Junxiao Shi455581d2014-11-17 18:38:40 -070064 run(int nOpsLimit, const time::nanoseconds& timeLimit);
Junxiao Shi98e29f42014-03-31 10:27:26 -070065
Junxiao Shi7e2413b2014-03-02 11:15:09 -070066 /// count an operation
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070067 void
68 afterOp();
Junxiao Shi98e29f42014-03-31 10:27:26 -070069
Junxiao Shi7e2413b2014-03-02 11:15:09 -070070 const std::exception&
71 getLastException() const;
Junxiao Shi98e29f42014-03-31 10:27:26 -070072
Junxiao Shi0e42c572014-10-05 20:33:48 -070073 /** \brief defer for specified duration
74 *
75 * equivalent to .run(UNLIMITED_OPS, d)
76 */
77 void
78 defer(const time::nanoseconds& d)
79 {
80 this->run(UNLIMITED_OPS, d);
81 }
82
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070083private:
84 void
85 afterTimeout();
86
87public:
88 static const int UNLIMITED_OPS;
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070089 static const time::nanoseconds UNLIMITED_TIME;
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070090
91private:
92 bool m_isRunning;
93 int m_nOpsRemaining;
94 EventId m_timeout;
95 StopReason m_reason;
Junxiao Shi7e2413b2014-03-02 11:15:09 -070096 std::exception m_lastException;
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070097};
98
Junxiao Shid9ee45c2014-02-27 15:38:11 -070099} // namespace tests
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700100} // namespace nfd
101
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700102#endif // NFD_TESTS_LIMITED_IO_HPP