blob: 0aae2900673f4d998ca91663c252510efb2bbff0 [file] [log] [blame]
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#ifndef NFD_TEST_CORE_LIMITED_IO_HPP
8#define NFD_TEST_CORE_LIMITED_IO_HPP
9
10#include "core/scheduler.hpp"
11
12namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070013namespace tests {
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070014
Junxiao Shi7e2413b2014-03-02 11:15:09 -070015/** \brief provides IO operations limit and/or time limit for unit testing
16 */
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070017class LimitedIo
18{
19public:
20 LimitedIo();
21
Junxiao Shi7e2413b2014-03-02 11:15:09 -070022 /// indicates why .run returns
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070023 enum StopReason
24 {
Junxiao Shi7e2413b2014-03-02 11:15:09 -070025 /// g_io.run() runs normally because there's no work to do
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070026 NO_WORK,
Junxiao Shi7e2413b2014-03-02 11:15:09 -070027 /// .afterOp() has been invoked nOpsLimit times
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070028 EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -070029 /// nTimeLimit has elapsed
30 EXCEED_TIME,
31 /// an exception is thrown
32 EXCEPTION
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070033 };
34
Junxiao Shi7e2413b2014-03-02 11:15:09 -070035 /** \brief g_io.run() with operation count and/or time limit
36 *
37 * \param nOpsLimit operation count limit, pass UNLIMITED_OPS for no limit
38 * \param nTimeLimit time limit, pass UNLIMITED_TIME for no limit
39 */
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070040 StopReason
41 run(int nOpsLimit, time::Duration nTimeLimit);
42
Junxiao Shi7e2413b2014-03-02 11:15:09 -070043 /// count an operation
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070044 void
45 afterOp();
46
Junxiao Shi7e2413b2014-03-02 11:15:09 -070047 const std::exception&
48 getLastException() const;
49
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070050private:
51 void
52 afterTimeout();
53
54public:
55 static const int UNLIMITED_OPS;
56 static const time::Duration UNLIMITED_TIME;
57
58private:
59 bool m_isRunning;
60 int m_nOpsRemaining;
61 EventId m_timeout;
62 StopReason m_reason;
Junxiao Shi7e2413b2014-03-02 11:15:09 -070063 std::exception m_lastException;
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070064};
65
Junxiao Shid9ee45c2014-02-27 15:38:11 -070066} // namespace tests
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070067} // namespace nfd
68
69#endif // NFD_TEST_CORE_LIMITED_IO_HPP