Eric Newberry | a93680e | 2015-07-15 19:25:29 -0700 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2015, Arizona Board of Regents. |
| 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/server/ping-server.hpp" |
| 21 | #include "tools/ping/client/ping.hpp" |
| 22 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 23 | |
| 24 | #include "tests/test-common.hpp" |
| 25 | |
| 26 | namespace ndn { |
| 27 | namespace ping { |
| 28 | namespace tests { |
| 29 | |
| 30 | using namespace ndn::tests; |
| 31 | |
| 32 | class PingIntegratedFixture : public UnitTestTimeFixture |
| 33 | { |
| 34 | public: |
| 35 | PingIntegratedFixture() |
| 36 | : serverFace(util::makeDummyClientFace(io, {false, true})) |
| 37 | , clientFace(util::makeDummyClientFace(io, {false, true})) |
| 38 | , numResponses(0) |
| 39 | , wantLoss(false) |
| 40 | { |
| 41 | serverFace->onSendInterest.connect([this] (const Interest& interest) { |
| 42 | io.post([=] { if (!wantLoss) { clientFace->receive(interest); } }); |
| 43 | }); |
| 44 | clientFace->onSendInterest.connect([this] (const Interest& interest) { |
| 45 | io.post([=] { if (!wantLoss) { serverFace->receive(interest); } }); |
| 46 | }); |
| 47 | serverFace->onSendData.connect([this] (const Data& data) { |
| 48 | io.post([=] { if (!wantLoss) { clientFace->receive(data); } }); |
| 49 | }); |
| 50 | clientFace->onSendData.connect([this] (const Data& data) { |
| 51 | io.post([=] { if (!wantLoss) { serverFace->receive(data); } }); |
| 52 | }); |
| 53 | } |
| 54 | |
| 55 | void onTimeout(uint64_t seq) |
| 56 | { |
| 57 | numResponses++; |
| 58 | if (numResponses == maxResponses) { |
| 59 | serverFace->shutdown(); |
| 60 | clientFace->shutdown(); |
| 61 | io.stop(); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | void onData(uint64_t seq) |
| 66 | { |
| 67 | numResponses++; |
| 68 | if (numResponses == maxResponses) { |
| 69 | serverFace->shutdown(); |
| 70 | clientFace->shutdown(); |
| 71 | io.stop(); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | public: |
| 76 | boost::asio::io_service io; |
| 77 | shared_ptr<util::DummyClientFace> serverFace; |
| 78 | shared_ptr<util::DummyClientFace> clientFace; |
| 79 | std::unique_ptr<server::PingServer> server; |
| 80 | std::unique_ptr<client::Ping> client; |
| 81 | int maxResponses; |
| 82 | int numResponses; |
| 83 | bool wantLoss; |
| 84 | }; |
| 85 | |
| 86 | BOOST_AUTO_TEST_SUITE(PingIntegrated) |
| 87 | |
| 88 | BOOST_FIXTURE_TEST_CASE(Normal, PingIntegratedFixture) |
| 89 | { |
| 90 | server::Options serverOpts; |
| 91 | serverOpts.prefix = "ndn:/test-prefix"; |
| 92 | serverOpts.freshnessPeriod = time::milliseconds(5000); |
| 93 | serverOpts.nMaxPings = 4; |
| 94 | serverOpts.shouldPrintTimestamp = false; |
| 95 | serverOpts.payloadSize = 0; |
| 96 | server.reset(new server::PingServer(*serverFace, serverOpts)); |
| 97 | BOOST_REQUIRE_EQUAL(0, server->getNPings()); |
| 98 | server->start(); |
| 99 | |
| 100 | client::Options clientOpts; |
| 101 | clientOpts.prefix = "ndn:/test-prefix"; |
| 102 | clientOpts.shouldAllowStaleData = false; |
| 103 | clientOpts.shouldGenerateRandomSeq = false; |
| 104 | clientOpts.shouldPrintTimestamp = false; |
| 105 | clientOpts.nPings = 4; |
| 106 | clientOpts.interval = time::milliseconds(100); |
| 107 | clientOpts.timeout = time::milliseconds(2000); |
| 108 | clientOpts.startSeq = 1000; |
| 109 | client.reset(new client::Ping(*clientFace, clientOpts)); |
| 110 | client->afterResponse.connect(bind(&PingIntegratedFixture::onData, this, _1)); |
| 111 | client->afterTimeout.connect(bind(&PingIntegratedFixture::onTimeout, this, _1)); |
| 112 | maxResponses = 4; |
| 113 | client->start(); |
| 114 | |
| 115 | this->advanceClocks(io, time::milliseconds(1), 400); |
| 116 | io.run(); |
| 117 | |
| 118 | BOOST_REQUIRE_EQUAL(4, server->getNPings()); |
| 119 | } |
| 120 | |
| 121 | BOOST_FIXTURE_TEST_CASE(Timeout, PingIntegratedFixture) |
| 122 | { |
| 123 | wantLoss = true; |
| 124 | |
| 125 | server::Options serverOpts; |
| 126 | serverOpts.prefix = "ndn:/test-prefix"; |
| 127 | serverOpts.freshnessPeriod = time::milliseconds(5000); |
| 128 | serverOpts.nMaxPings = 4; |
| 129 | serverOpts.shouldPrintTimestamp = false; |
| 130 | serverOpts.payloadSize = 0; |
| 131 | server.reset(new server::PingServer(*serverFace, serverOpts)); |
| 132 | BOOST_REQUIRE_EQUAL(0, server->getNPings()); |
| 133 | server->start(); |
| 134 | |
| 135 | client::Options clientOpts; |
| 136 | clientOpts.prefix = "ndn:/test-prefix"; |
| 137 | clientOpts.shouldAllowStaleData = false; |
| 138 | clientOpts.shouldGenerateRandomSeq = false; |
| 139 | clientOpts.shouldPrintTimestamp = false; |
| 140 | clientOpts.nPings = 4; |
| 141 | clientOpts.interval = time::milliseconds(100); |
| 142 | clientOpts.timeout = time::milliseconds(500); |
| 143 | clientOpts.startSeq = 1000; |
| 144 | client.reset(new client::Ping(*clientFace, clientOpts)); |
| 145 | numResponses = 0; |
| 146 | maxResponses = 4; |
| 147 | client->afterResponse.connect(bind(&PingIntegratedFixture::onData, this, _1)); |
| 148 | client->afterTimeout.connect(bind(&PingIntegratedFixture::onTimeout, this, _1)); |
| 149 | client->start(); |
| 150 | |
| 151 | this->advanceClocks(io, time::milliseconds(1), 1000); |
| 152 | io.run(); |
| 153 | |
| 154 | BOOST_REQUIRE_EQUAL(0, server->getNPings()); |
| 155 | } |
| 156 | |
| 157 | BOOST_AUTO_TEST_SUITE_END() |
| 158 | |
| 159 | } // namespace tests |
| 160 | } // namespace ping |
| 161 | } // namespace ndn |