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