Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, 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" |
| 21 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 22 | |
| 23 | #include "tests/test-common.hpp" |
| 24 | |
| 25 | namespace ndn { |
| 26 | namespace ping { |
| 27 | namespace client { |
| 28 | namespace tests { |
| 29 | |
| 30 | using namespace ndn::tests; |
| 31 | |
| 32 | BOOST_AUTO_TEST_SUITE(PingClientPing) |
| 33 | |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 34 | BOOST_FIXTURE_TEST_CASE(Basic, UnitTestTimeFixture) |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 35 | { |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 36 | Options pingOptions; |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 37 | pingOptions.prefix = "ndn:/test-prefix"; |
| 38 | pingOptions.shouldAllowStaleData = false; |
| 39 | pingOptions.shouldGenerateRandomSeq = false; |
| 40 | pingOptions.shouldPrintTimestamp = false; |
| 41 | pingOptions.nPings = 4; |
| 42 | pingOptions.interval = time::milliseconds(100); |
| 43 | pingOptions.timeout = time::milliseconds(2000); |
| 44 | pingOptions.startSeq = 1000; |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 45 | |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 46 | boost::asio::io_service io; |
| 47 | util::DummyClientFace face(io, {true, true}); |
| 48 | Ping ping(face, pingOptions); |
| 49 | |
| 50 | int nFinishSignals = 0; |
| 51 | std::vector<uint64_t> dataSeqs; |
| 52 | std::vector<uint64_t> nackSeqs; |
| 53 | std::vector<uint64_t> timeoutSeqs; |
| 54 | |
| 55 | ping.afterData.connect(bind([&] (uint64_t seq) { dataSeqs.push_back(seq); }, _1)); |
| 56 | ping.afterNack.connect(bind([&] (uint64_t seq) { nackSeqs.push_back(seq); }, _1)); |
| 57 | ping.afterTimeout.connect(bind([&] (uint64_t seq) { timeoutSeqs.push_back(seq); }, _1)); |
| 58 | ping.afterFinish.connect(bind([&] { |
| 59 | BOOST_REQUIRE_EQUAL(dataSeqs.size(), 2); |
| 60 | BOOST_REQUIRE_EQUAL(nackSeqs.size(), 1); |
| 61 | BOOST_REQUIRE_EQUAL(timeoutSeqs.size(), 1); |
| 62 | |
| 63 | BOOST_CHECK_EQUAL(dataSeqs[0], 1000); |
| 64 | BOOST_CHECK_EQUAL(nackSeqs[0], 1001); |
| 65 | BOOST_CHECK_EQUAL(dataSeqs[1], 1002); |
| 66 | BOOST_CHECK_EQUAL(timeoutSeqs[0], 1003); |
| 67 | |
| 68 | nFinishSignals++; |
| 69 | })); |
| 70 | |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 71 | ping.start(); |
| 72 | |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 73 | this->advanceClocks(io, time::milliseconds(1), 500); |
| 74 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 4); |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 75 | |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 76 | face.receive(*makeData("ndn:/test-prefix/ping/1000")); |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 77 | |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 78 | lp::Nack nack(face.sentInterests[1]); |
| 79 | nack.setReason(lp::NackReason::DUPLICATE); |
| 80 | face.receive(nack); |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 81 | |
Teng Liang | 6288486 | 2016-03-31 18:15:36 -0700 | [diff] [blame] | 82 | face.receive(*makeData("ndn:/test-prefix/ping/1002")); |
| 83 | |
| 84 | this->advanceClocks(io, time::milliseconds(100), 20); |
| 85 | |
| 86 | // ndn:/test-prefix/ping/1003 is unanswered and will timeout |
| 87 | |
| 88 | BOOST_CHECK_EQUAL(nFinishSignals, 1); |
| 89 | |
| 90 | face.shutdown(); |
| 91 | io.stop(); |
Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | BOOST_AUTO_TEST_SUITE_END() |
| 95 | |
| 96 | } // namespace tests |
| 97 | } // namespace client |
| 98 | } // namespace ping |
| 99 | } // namespace ndn |