Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 9619295 | 2019-05-22 15:45:12 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | 6677762 | 2020-10-09 18:46:03 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2014-2020, Arizona Board of Regents. |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-tools (Named Data Networking Essential Tools). |
| 6 | * See AUTHORS.md for complete list of ndn-tools authors and contributors. |
| 7 | * |
| 8 | * ndn-tools is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "tools/ping/client/ping.hpp" |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 21 | |
| 22 | #include "tests/test-common.hpp" |
Davide Pesavento | 6677762 | 2020-10-09 18:46:03 -0400 | [diff] [blame^] | 23 | #include "tests/io-fixture.hpp" |
| 24 | |
Davide Pesavento | 013de9b | 2016-09-01 12:06:56 +0000 | [diff] [blame] | 25 | #include <ndn-cxx/util/dummy-client-face.hpp> |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 26 | |
| 27 | namespace ndn { |
| 28 | namespace ping { |
| 29 | namespace client { |
| 30 | namespace tests { |
| 31 | |
| 32 | using namespace ndn::tests; |
| 33 | |
Davide Pesavento | 013de9b | 2016-09-01 12:06:56 +0000 | [diff] [blame] | 34 | BOOST_AUTO_TEST_SUITE(Ping) |
| 35 | BOOST_AUTO_TEST_SUITE(TestPing) |
| 36 | |
| 37 | using ping::client::Ping; |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 38 | |
Davide Pesavento | 6677762 | 2020-10-09 18:46:03 -0400 | [diff] [blame^] | 39 | BOOST_FIXTURE_TEST_CASE(Basic, IoFixture) |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 40 | { |
Davide Pesavento | 6677762 | 2020-10-09 18:46:03 -0400 | [diff] [blame^] | 41 | util::DummyClientFace face(m_io, {true, true}); |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 42 | Options pingOptions; |
Davide Pesavento | 6677762 | 2020-10-09 18:46:03 -0400 | [diff] [blame^] | 43 | pingOptions.prefix = "/test-prefix"; |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 44 | pingOptions.shouldAllowStaleData = false; |
| 45 | pingOptions.shouldGenerateRandomSeq = false; |
| 46 | pingOptions.shouldPrintTimestamp = false; |
| 47 | pingOptions.nPings = 4; |
Davide Pesavento | 6677762 | 2020-10-09 18:46:03 -0400 | [diff] [blame^] | 48 | pingOptions.interval = 100_ms; |
| 49 | pingOptions.timeout = 2_s; |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 50 | pingOptions.startSeq = 1000; |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 51 | Ping ping(face, pingOptions); |
| 52 | |
| 53 | int nFinishSignals = 0; |
| 54 | std::vector<uint64_t> dataSeqs; |
| 55 | std::vector<uint64_t> nackSeqs; |
| 56 | std::vector<uint64_t> timeoutSeqs; |
| 57 | |
| 58 | ping.afterData.connect(bind([&] (uint64_t seq) { dataSeqs.push_back(seq); }, _1)); |
| 59 | ping.afterNack.connect(bind([&] (uint64_t seq) { nackSeqs.push_back(seq); }, _1)); |
| 60 | ping.afterTimeout.connect(bind([&] (uint64_t seq) { timeoutSeqs.push_back(seq); }, _1)); |
| 61 | ping.afterFinish.connect(bind([&] { |
| 62 | BOOST_REQUIRE_EQUAL(dataSeqs.size(), 2); |
| 63 | BOOST_REQUIRE_EQUAL(nackSeqs.size(), 1); |
| 64 | BOOST_REQUIRE_EQUAL(timeoutSeqs.size(), 1); |
| 65 | |
| 66 | BOOST_CHECK_EQUAL(dataSeqs[0], 1000); |
| 67 | BOOST_CHECK_EQUAL(nackSeqs[0], 1001); |
| 68 | BOOST_CHECK_EQUAL(dataSeqs[1], 1002); |
| 69 | BOOST_CHECK_EQUAL(timeoutSeqs[0], 1003); |
| 70 | |
| 71 | nFinishSignals++; |
| 72 | })); |
| 73 | |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 74 | ping.start(); |
| 75 | |
Davide Pesavento | 6677762 | 2020-10-09 18:46:03 -0400 | [diff] [blame^] | 76 | this->advanceClocks(1_ms, 500); |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 77 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 4); |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 78 | |
Junxiao Shi | 9619295 | 2019-05-22 15:45:12 +0000 | [diff] [blame] | 79 | auto data = makeData("/test-prefix/ping/1000"); |
| 80 | data->setFreshnessPeriod(1_s); |
| 81 | face.receive(*data); |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 82 | |
Davide Pesavento | 6677762 | 2020-10-09 18:46:03 -0400 | [diff] [blame^] | 83 | face.receive(makeNack(face.sentInterests[1], lp::NackReason::DUPLICATE)); |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 84 | |
Junxiao Shi | 9619295 | 2019-05-22 15:45:12 +0000 | [diff] [blame] | 85 | data = makeData("/test-prefix/ping/1002"); |
| 86 | data->setFreshnessPeriod(1_s); |
| 87 | face.receive(*data); |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 88 | |
Davide Pesavento | 6677762 | 2020-10-09 18:46:03 -0400 | [diff] [blame^] | 89 | this->advanceClocks(100_ms, 20); |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 90 | |
Davide Pesavento | 6677762 | 2020-10-09 18:46:03 -0400 | [diff] [blame^] | 91 | // /test-prefix/ping/1003 is unanswered and will timeout |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 92 | |
| 93 | BOOST_CHECK_EQUAL(nFinishSignals, 1); |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Davide Pesavento | 013de9b | 2016-09-01 12:06:56 +0000 | [diff] [blame] | 96 | BOOST_AUTO_TEST_SUITE_END() // TestPing |
| 97 | BOOST_AUTO_TEST_SUITE_END() // Ping |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 98 | |
| 99 | } // namespace tests |
| 100 | } // namespace client |
| 101 | } // namespace ping |
| 102 | } // namespace ndn |