Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 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 | * The University of Memphis. |
| 10 | * |
| 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/>. |
| 24 | */ |
| 25 | |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 26 | #include "nfdc/status-report.hpp" |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 27 | |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 28 | #include "status-fixture.hpp" |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 29 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 30 | namespace nfd::tools::nfdc::tests { |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 31 | |
| 32 | const std::string STATUS_XML = stripXmlSpaces(R"XML( |
| 33 | <?xml version="1.0"?> |
| 34 | <nfdStatus xmlns="ndn:/localhost/nfd/status/1"> |
| 35 | <module1/> |
| 36 | <module2/> |
| 37 | </nfdStatus> |
| 38 | )XML"); |
| 39 | |
| 40 | const std::string STATUS_TEXT = std::string(R"TEXT( |
| 41 | module1 |
| 42 | module2 |
| 43 | )TEXT").substr(1); |
| 44 | |
| 45 | class DummyModule : public Module |
| 46 | { |
| 47 | public: |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 48 | DummyModule(const std::string& moduleName, boost::asio::io_service& io) |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 49 | : m_moduleName(moduleName) |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 50 | , m_scheduler(io) |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 51 | , m_res(0) |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 52 | , m_delay(1_ms) |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 53 | { |
| 54 | } |
| 55 | |
| 56 | /** \brief cause fetchStatus to succeed or fail |
| 57 | * \param res zero to succeed, non-zero to fail with specific code |
| 58 | * \param delay duration from fetchStatus invocation to succeed or fail; must be positive |
| 59 | */ |
| 60 | void |
| 61 | setResult(uint32_t res, time::nanoseconds delay) |
| 62 | { |
| 63 | BOOST_ASSERT(delay > time::nanoseconds::zero()); |
| 64 | m_res = res; |
| 65 | m_delay = delay; |
| 66 | } |
| 67 | |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 68 | void |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 69 | fetchStatus(Controller&, |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 70 | const std::function<void()>& onSuccess, |
Junxiao Shi | 29b4128 | 2016-08-22 03:47:02 +0000 | [diff] [blame] | 71 | const Controller::DatasetFailCallback& onFailure, |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 72 | const CommandOptions&) final |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 73 | { |
| 74 | ++nFetchStatusCalls; |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 75 | m_scheduler.schedule(m_delay, [=] { |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 76 | if (m_res == 0) { |
| 77 | onSuccess(); |
| 78 | } |
| 79 | else { |
| 80 | onFailure(m_res, m_moduleName + " fails with code " + to_string(m_res)); |
| 81 | } |
| 82 | }); |
| 83 | } |
| 84 | |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 85 | void |
| 86 | formatStatusXml(std::ostream& os) const final |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 87 | { |
| 88 | os << '<' << m_moduleName << "/>"; |
| 89 | } |
| 90 | |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 91 | void |
| 92 | formatStatusText(std::ostream& os) const final |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 93 | { |
| 94 | os << m_moduleName << '\n'; |
| 95 | } |
| 96 | |
| 97 | public: |
| 98 | int nFetchStatusCalls = 0; |
| 99 | |
| 100 | private: |
| 101 | std::string m_moduleName; |
Davide Pesavento | 3dade00 | 2019-03-19 11:29:56 -0600 | [diff] [blame] | 102 | Scheduler m_scheduler; |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 103 | uint32_t m_res; |
| 104 | time::nanoseconds m_delay; |
| 105 | }; |
| 106 | |
| 107 | class StatusReportTester : public StatusReport |
| 108 | { |
| 109 | private: |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 110 | void |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 111 | processEvents(Face&) override |
| 112 | { |
| 113 | processEventsFunc(); |
| 114 | } |
| 115 | |
| 116 | public: |
| 117 | std::function<void()> processEventsFunc; |
| 118 | }; |
| 119 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 120 | class StatusReportModulesFixture : public nfd::tests::IoFixture, public nfd::tests::KeyChainFixture |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 121 | { |
| 122 | protected: |
| 123 | StatusReportModulesFixture() |
Davide Pesavento | 2135375 | 2020-11-20 00:43:44 -0500 | [diff] [blame] | 124 | : face(m_io, m_keyChain) |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 125 | , controller(face, m_keyChain, validator) |
| 126 | , res(0) |
| 127 | { |
| 128 | } |
| 129 | |
| 130 | DummyModule& |
| 131 | addModule(const std::string& moduleName) |
| 132 | { |
Davide Pesavento | b7703ad | 2019-03-23 21:12:56 -0400 | [diff] [blame] | 133 | report.sections.push_back(make_unique<DummyModule>(moduleName, m_io)); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 134 | return static_cast<DummyModule&>(*report.sections.back()); |
| 135 | } |
| 136 | |
| 137 | void |
| 138 | collect(time::nanoseconds tick, size_t nTicks) |
| 139 | { |
Davide Pesavento | 2135375 | 2020-11-20 00:43:44 -0500 | [diff] [blame] | 140 | report.processEventsFunc = [=] { advanceClocks(tick, nTicks); }; |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 141 | res = report.collect(face, m_keyChain, validator, CommandOptions()); |
| 142 | |
| 143 | if (res == 0) { |
| 144 | statusXml.str(""); |
| 145 | report.formatXml(statusXml); |
| 146 | statusText.str(""); |
| 147 | report.formatText(statusText); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | protected: |
Junxiao Shi | 1f481fa | 2017-01-26 15:14:43 +0000 | [diff] [blame] | 152 | ndn::util::DummyClientFace face; |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 153 | ValidatorNull validator; |
| 154 | Controller controller; |
| 155 | StatusReportTester report; |
| 156 | |
| 157 | uint32_t res; |
| 158 | output_test_stream statusXml; |
| 159 | output_test_stream statusText; |
| 160 | }; |
| 161 | |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 162 | BOOST_AUTO_TEST_SUITE(Nfdc) |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 163 | BOOST_FIXTURE_TEST_SUITE(TestStatusReport, StatusReportModulesFixture) |
| 164 | |
| 165 | BOOST_AUTO_TEST_CASE(Normal) |
| 166 | { |
| 167 | DummyModule& m1 = addModule("module1"); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 168 | m1.setResult(0, 10_ms); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 169 | DummyModule& m2 = addModule("module2"); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 170 | m2.setResult(0, 20_ms); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 171 | |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 172 | this->collect(5_ms, 6); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 173 | |
| 174 | BOOST_CHECK_EQUAL(m1.nFetchStatusCalls, 1); |
| 175 | BOOST_CHECK_EQUAL(m2.nFetchStatusCalls, 1); |
| 176 | |
| 177 | BOOST_CHECK_EQUAL(res, 0); |
| 178 | BOOST_CHECK(statusXml.is_equal(STATUS_XML)); |
| 179 | BOOST_CHECK(statusText.is_equal(STATUS_TEXT)); |
| 180 | } |
| 181 | |
| 182 | BOOST_AUTO_TEST_CASE(Reorder) |
| 183 | { |
| 184 | DummyModule& m1 = addModule("module1"); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 185 | m1.setResult(0, 20_ms); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 186 | DummyModule& m2 = addModule("module2"); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 187 | m2.setResult(0, 10_ms); // module2 completes earlier than module1 |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 188 | |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 189 | this->collect(5_ms, 6); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 190 | |
| 191 | BOOST_CHECK_EQUAL(res, 0); |
| 192 | BOOST_CHECK(statusXml.is_equal(STATUS_XML)); // output is still in order |
| 193 | BOOST_CHECK(statusText.is_equal(STATUS_TEXT)); |
| 194 | } |
| 195 | |
| 196 | BOOST_AUTO_TEST_CASE(Error) |
| 197 | { |
| 198 | DummyModule& m1 = addModule("module1"); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 199 | m1.setResult(0, 20_ms); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 200 | DummyModule& m2 = addModule("module2"); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 201 | m2.setResult(500, 10_ms); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 202 | |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 203 | this->collect(5_ms, 6); |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 204 | |
| 205 | BOOST_CHECK_EQUAL(res, 1000500); |
| 206 | } |
| 207 | |
Junxiao Shi | 8c39bf0 | 2016-08-28 23:54:13 +0000 | [diff] [blame] | 208 | BOOST_AUTO_TEST_SUITE_END() // TestStatusReport |
Junxiao Shi | 331ade7 | 2016-08-19 14:07:19 +0000 | [diff] [blame] | 209 | BOOST_AUTO_TEST_SUITE_END() // Nfdc |
Junxiao Shi | 38f4ce9 | 2016-08-04 10:01:52 +0000 | [diff] [blame] | 210 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 211 | } // namespace nfd::tools::nfdc::tests |