blob: cb8124721a01d116d9d78feb10f4f9328e931f55 [file] [log] [blame]
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -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 *
10 * This file is part of NFD (Named Data Networking Forwarding Daemon).
11 * See AUTHORS.md for complete list of NFD authors and contributors.
12 *
13 * NFD is free software: you can redistribute it and/or modify it under the terms
14 * of the GNU General Public License as published by the Free Software Foundation,
15 * either version 3 of the License, or (at your option) any later version.
16 *
17 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 * PURPOSE. See the GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
23 **/
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070024
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070025#ifndef NFD_TESTS_LIMITED_IO_HPP
26#define NFD_TESTS_LIMITED_IO_HPP
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070027
Junxiao Shi98e29f42014-03-31 10:27:26 -070028#include "core/global-io.hpp"
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070029#include "core/scheduler.hpp"
30
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 -070034/** \brief provides IO operations limit and/or time limit for unit testing
35 */
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070036class LimitedIo
37{
38public:
39 LimitedIo();
Junxiao Shi98e29f42014-03-31 10:27:26 -070040
Junxiao Shi7e2413b2014-03-02 11:15:09 -070041 /// indicates why .run returns
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070042 enum StopReason
43 {
Junxiao Shi7e2413b2014-03-02 11:15:09 -070044 /// g_io.run() runs normally because there's no work to do
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070045 NO_WORK,
Junxiao Shi7e2413b2014-03-02 11:15:09 -070046 /// .afterOp() has been invoked nOpsLimit times
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070047 EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -070048 /// nTimeLimit has elapsed
49 EXCEED_TIME,
50 /// an exception is thrown
51 EXCEPTION
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070052 };
Junxiao Shi98e29f42014-03-31 10:27:26 -070053
Junxiao Shi7e2413b2014-03-02 11:15:09 -070054 /** \brief g_io.run() with operation count and/or time limit
55 *
56 * \param nOpsLimit operation count limit, pass UNLIMITED_OPS for no limit
57 * \param nTimeLimit time limit, pass UNLIMITED_TIME for no limit
58 */
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070059 StopReason
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070060 run(int nOpsLimit, const time::nanoseconds& nTimeLimit);
Junxiao Shi98e29f42014-03-31 10:27:26 -070061
Junxiao Shi7e2413b2014-03-02 11:15:09 -070062 /// count an operation
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070063 void
64 afterOp();
Junxiao Shi98e29f42014-03-31 10:27:26 -070065
Junxiao Shi7e2413b2014-03-02 11:15:09 -070066 const std::exception&
67 getLastException() const;
Junxiao Shi98e29f42014-03-31 10:27:26 -070068
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070069private:
70 void
71 afterTimeout();
72
73public:
74 static const int UNLIMITED_OPS;
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070075 static const time::nanoseconds UNLIMITED_TIME;
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070076
77private:
78 bool m_isRunning;
79 int m_nOpsRemaining;
80 EventId m_timeout;
81 StopReason m_reason;
Junxiao Shi7e2413b2014-03-02 11:15:09 -070082 std::exception m_lastException;
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070083};
84
Junxiao Shid9ee45c2014-02-27 15:38:11 -070085} // namespace tests
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070086} // namespace nfd
87
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070088#endif // NFD_TESTS_LIMITED_IO_HPP