blob: 63d56821606c32ac33f8bcacfd6b7ad57c49dcef [file] [log] [blame]
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Teng Liang952d6fd2018-05-29 21:09:52 -07002/*
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -05003 * Copyright (c) 2014-2024, Regents of the University of California,
Junxiao Shi1e46be32015-01-08 20:18:05 -07004 * 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
Davide Pesavento3dade002019-03-19 11:29:56 -060026#ifndef NFD_TESTS_DAEMON_LIMITED_IO_HPP
27#define NFD_TESTS_DAEMON_LIMITED_IO_HPP
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070028
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040029#include "tests/daemon/global-io-fixture.hpp"
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070030
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -050031#include <ndn-cxx/util/scheduler.hpp>
32
Davide Pesaventof35a5a92015-12-18 18:57:45 +010033#include <exception>
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -050034#include <limits>
Davide Pesaventof35a5a92015-12-18 18:57:45 +010035
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040036namespace nfd::tests {
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070037
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040038/** \brief Provides IO operations limit and/or time limit for unit testing.
Teng Liang952d6fd2018-05-29 21:09:52 -070039 *
40 * \warning LimitedIo is incompatible with RibIoFixture
Junxiao Shi7e2413b2014-03-02 11:15:09 -070041 */
Junxiao Shi0e42c572014-10-05 20:33:48 -070042class LimitedIo : noncopyable
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070043{
44public:
Davide Pesaventof35a5a92015-12-18 18:57:45 +010045 explicit
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040046 LimitedIo(GlobalIoTimeFixture* fixture = nullptr);
Junxiao Shi3cb4fc62014-12-25 22:17:39 -070047
Davide Pesavento3dade002019-03-19 11:29:56 -060048 /// indicates why run() returns
Davide Pesaventof35a5a92015-12-18 18:57:45 +010049 enum StopReason {
Junxiao Shi0e42c572014-10-05 20:33:48 -070050 /// g_io.run() returns normally because there's no work to do
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070051 NO_WORK,
Junxiao Shi7e2413b2014-03-02 11:15:09 -070052 /// .afterOp() has been invoked nOpsLimit times
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070053 EXCEED_OPS,
Junxiao Shi7e2413b2014-03-02 11:15:09 -070054 /// nTimeLimit has elapsed
55 EXCEED_TIME,
56 /// an exception is thrown
57 EXCEPTION
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070058 };
Junxiao Shi98e29f42014-03-31 10:27:26 -070059
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040060 /** \brief `g_io.run()` with operation count and/or time limit.
Junxiao Shi7e2413b2014-03-02 11:15:09 -070061 * \param nOpsLimit operation count limit, pass UNLIMITED_OPS for no limit
Junxiao Shi455581d2014-11-17 18:38:40 -070062 * \param timeLimit time limit, pass UNLIMITED_TIME for no limit
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040063 * \param tick if this LimitedIo is constructed with GlobalIoTimeFixture,
Junxiao Shi3cb4fc62014-12-25 22:17:39 -070064 * this is passed to .advanceClocks(), otherwise ignored
Junxiao Shi7e2413b2014-03-02 11:15:09 -070065 */
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040066 [[nodiscard]] StopReason
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040067 run(int nOpsLimit, time::nanoseconds timeLimit, time::nanoseconds tick = 1_ms);
Junxiao Shi98e29f42014-03-31 10:27:26 -070068
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040069 /// Count an operation
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070070 void
71 afterOp();
Junxiao Shi98e29f42014-03-31 10:27:26 -070072
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040073 /**
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040074 * \brief Defer for the specified duration.
Junxiao Shi0e42c572014-10-05 20:33:48 -070075 *
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040076 * Equivalent to run(UNLIMITED_OPS, d)
Junxiao Shi0e42c572014-10-05 20:33:48 -070077 */
78 void
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040079 defer(time::nanoseconds d)
Junxiao Shi0e42c572014-10-05 20:33:48 -070080 {
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040081 std::ignore = run(UNLIMITED_OPS, d);
Junxiao Shi0e42c572014-10-05 20:33:48 -070082 }
83
Davide Pesaventof35a5a92015-12-18 18:57:45 +010084 std::exception_ptr
85 getLastException() const
86 {
87 return m_lastException;
88 }
89
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070090private:
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -040091 /** \brief An exception to stop IO operation.
Junxiao Shi3cb4fc62014-12-25 22:17:39 -070092 */
Spyridon Mastorakis149e02c2015-07-27 13:22:22 -070093 class StopException : public std::exception
Junxiao Shi3cb4fc62014-12-25 22:17:39 -070094 {
95 };
96
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -070097 void
98 afterTimeout();
99
100public:
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400101 static constexpr int UNLIMITED_OPS = std::numeric_limits<int>::max();
102 static constexpr time::nanoseconds UNLIMITED_TIME = time::nanoseconds::min();
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700103
104private:
Davide Pesaventocf7db2f2019-03-24 23:17:28 -0400105 GlobalIoTimeFixture* m_fixture;
Davide Pesaventof35a5a92015-12-18 18:57:45 +0100106 StopReason m_reason;
Davide Pesavento3dade002019-03-19 11:29:56 -0600107 int m_nOpsRemaining = 0;
Davide Pesavento2c9d2ca2024-01-27 16:36:51 -0500108 ndn::scheduler::EventId m_timeout;
Davide Pesaventof35a5a92015-12-18 18:57:45 +0100109 std::exception_ptr m_lastException;
Davide Pesavento3dade002019-03-19 11:29:56 -0600110 bool m_isRunning = false;
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700111};
112
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400113} // namespace nfd::tests
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -0700114
Davide Pesavento3dade002019-03-19 11:29:56 -0600115#endif // NFD_TESTS_DAEMON_LIMITED_IO_HPP