Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 1 | /* -*- 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 | |
| 12 | namespace nfd { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 13 | namespace tests { |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 14 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 15 | /** \brief provides IO operations limit and/or time limit for unit testing |
| 16 | */ |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 17 | class LimitedIo |
| 18 | { |
| 19 | public: |
| 20 | LimitedIo(); |
| 21 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 22 | /// indicates why .run returns |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 23 | enum StopReason |
| 24 | { |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 25 | /// g_io.run() runs normally because there's no work to do |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 26 | NO_WORK, |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 27 | /// .afterOp() has been invoked nOpsLimit times |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 28 | EXCEED_OPS, |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 29 | /// nTimeLimit has elapsed |
| 30 | EXCEED_TIME, |
| 31 | /// an exception is thrown |
| 32 | EXCEPTION |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 33 | }; |
| 34 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 35 | /** \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 Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 40 | StopReason |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 41 | run(int nOpsLimit, const time::nanoseconds& nTimeLimit); |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 42 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 43 | /// count an operation |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 44 | void |
| 45 | afterOp(); |
| 46 | |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 47 | const std::exception& |
| 48 | getLastException() const; |
| 49 | |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 50 | private: |
| 51 | void |
| 52 | afterTimeout(); |
| 53 | |
| 54 | public: |
| 55 | static const int UNLIMITED_OPS; |
Alexander Afanasyev | eb3197f | 2014-03-17 19:28:18 -0700 | [diff] [blame] | 56 | static const time::nanoseconds UNLIMITED_TIME; |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 57 | |
| 58 | private: |
| 59 | bool m_isRunning; |
| 60 | int m_nOpsRemaining; |
| 61 | EventId m_timeout; |
| 62 | StopReason m_reason; |
Junxiao Shi | 7e2413b | 2014-03-02 11:15:09 -0700 | [diff] [blame] | 63 | std::exception m_lastException; |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 66 | } // namespace tests |
Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame] | 67 | } // namespace nfd |
| 68 | |
| 69 | #endif // NFD_TEST_CORE_LIMITED_IO_HPP |