blob: 94fe47668ca1d4907b31ffef5adacf77d078efe8 [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
Junxiao Shi98e29f42014-03-31 10:27:26 -070010#include "core/global-io.hpp"
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070011#include "core/scheduler.hpp"
12
13namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070014namespace tests {
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070015
Junxiao Shi7e2413b2014-03-02 11:15:09 -070016/** \brief provides IO operations limit and/or time limit for unit testing
17 */
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070018class LimitedIo
19{
20public:
21 LimitedIo();
Junxiao Shi98e29f42014-03-31 10:27:26 -070022
Junxiao Shi7e2413b2014-03-02 11:15:09 -070023 /// indicates why .run returns
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070024 enum StopReason
25 {
Junxiao Shi7e2413b2014-03-02 11:15:09 -070026 /// g_io.run() runs normally because there's no work to do
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070027 NO_WORK,
Junxiao Shi7e2413b2014-03-02 11:15:09 -070028 /// .afterOp() has been invoked nOpsLimit times
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070029 EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -070030 /// nTimeLimit has elapsed
31 EXCEED_TIME,
32 /// an exception is thrown
33 EXCEPTION
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070034 };
Junxiao Shi98e29f42014-03-31 10:27:26 -070035
Junxiao Shi7e2413b2014-03-02 11:15:09 -070036 /** \brief g_io.run() with operation count and/or time limit
37 *
38 * \param nOpsLimit operation count limit, pass UNLIMITED_OPS for no limit
39 * \param nTimeLimit time limit, pass UNLIMITED_TIME for no limit
40 */
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070041 StopReason
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070042 run(int nOpsLimit, const time::nanoseconds& nTimeLimit);
Junxiao Shi98e29f42014-03-31 10:27:26 -070043
Junxiao Shi7e2413b2014-03-02 11:15:09 -070044 /// count an operation
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070045 void
46 afterOp();
Junxiao Shi98e29f42014-03-31 10:27:26 -070047
Junxiao Shi7e2413b2014-03-02 11:15:09 -070048 const std::exception&
49 getLastException() const;
Junxiao Shi98e29f42014-03-31 10:27:26 -070050
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070051private:
52 void
53 afterTimeout();
54
55public:
56 static const int UNLIMITED_OPS;
Alexander Afanasyeveb3197f2014-03-17 19:28:18 -070057 static const time::nanoseconds UNLIMITED_TIME;
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070058
59private:
60 bool m_isRunning;
61 int m_nOpsRemaining;
62 EventId m_timeout;
63 StopReason m_reason;
Junxiao Shi7e2413b2014-03-02 11:15:09 -070064 std::exception m_lastException;
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070065};
66
Junxiao Shid9ee45c2014-02-27 15:38:11 -070067} // namespace tests
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070068} // namespace nfd
69
70#endif // NFD_TEST_CORE_LIMITED_IO_HPP