ping: Tests for Ping and PingServer
This commit includes minor refactoring in Ping and PingServer to support unit testing
refs #2796
Change-Id: Id0172486aa07e129a90091d72594fbccbb5dbc06
diff --git a/tests/ping/client/ping.t.cpp b/tests/ping/client/ping.t.cpp
new file mode 100644
index 0000000..9f9eeb9
--- /dev/null
+++ b/tests/ping/client/ping.t.cpp
@@ -0,0 +1,121 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2015, Arizona Board of Regents.
+ *
+ * This file is part of ndn-tools (Named Data Networking Essential Tools).
+ * See AUTHORS.md for complete list of ndn-tools authors and contributors.
+ *
+ * ndn-tools is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "tools/ping/client/ping.hpp"
+#include <ndn-cxx/util/dummy-client-face.hpp>
+
+#include "tests/test-common.hpp"
+
+namespace ndn {
+namespace ping {
+namespace client {
+namespace tests {
+
+using namespace ndn::tests;
+
+BOOST_AUTO_TEST_SUITE(PingClientPing)
+
+class SequenceNumberIncrementFixture : public UnitTestTimeFixture
+{
+protected:
+ SequenceNumberIncrementFixture()
+ : face(util::makeDummyClientFace(io, {false, true}))
+ , pingOptions(makeOptions())
+ , ping(*face, pingOptions)
+ , numPings(0)
+ , lastPingSeq(pingOptions.startSeq)
+ {
+ ping.afterResponse.connect(bind(&SequenceNumberIncrementFixture::onResponse, this, _1));
+ ping.afterTimeout.connect(bind(&SequenceNumberIncrementFixture::onTimeout, this, _1));
+ }
+
+public:
+ void
+ onResponse(uint64_t seq)
+ {
+ numPings++;
+ lastPingSeq = seq;
+ if (numPings == maxPings) {
+ face->shutdown();
+ io.stop();
+ }
+ }
+
+ void
+ onTimeout(uint64_t seq)
+ {
+ numPings++;
+ lastPingSeq = seq;
+ if (numPings == maxPings) {
+ face->shutdown();
+ io.stop();
+ }
+ }
+
+private:
+ static Options
+ makeOptions()
+ {
+ Options opt;
+ opt.prefix = "ndn:/test-prefix";
+ opt.shouldAllowStaleData = false;
+ opt.shouldGenerateRandomSeq = false;
+ opt.shouldPrintTimestamp = false;
+ opt.nPings = 4;
+ opt.interval = time::milliseconds(100);
+ opt.timeout = time::milliseconds(1000);
+ opt.startSeq = 1000;
+ return opt;
+ }
+
+protected:
+ boost::asio::io_service io;
+ shared_ptr<util::DummyClientFace> face;
+ Options pingOptions;
+ Ping ping;
+ uint32_t numPings;
+ uint32_t maxPings;
+ uint64_t lastPingSeq;
+ KeyChain keyChain;
+};
+
+BOOST_FIXTURE_TEST_CASE(SequenceNumberIncrement, SequenceNumberIncrementFixture)
+{
+ maxPings = 4;
+ ping.start();
+
+ this->advanceClocks(io, time::milliseconds(1), 400);
+
+ face->receive(*makeData("ndn:/test-prefix/ping/1000"));
+ face->receive(*makeData("ndn:/test-prefix/ping/1001"));
+ face->receive(*makeData("ndn:/test-prefix/ping/1002"));
+ face->receive(*makeData("ndn:/test-prefix/ping/1003"));
+
+ io.run();
+
+ BOOST_REQUIRE_EQUAL(1003, lastPingSeq);
+ BOOST_REQUIRE_EQUAL(4, numPings);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace client
+} // namespace ping
+} // namespace ndn
diff --git a/tests/ping/integrated.t.cpp b/tests/ping/integrated.t.cpp
new file mode 100644
index 0000000..f8cb30d
--- /dev/null
+++ b/tests/ping/integrated.t.cpp
@@ -0,0 +1,161 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2015, Arizona Board of Regents.
+ *
+ * This file is part of ndn-tools (Named Data Networking Essential Tools).
+ * See AUTHORS.md for complete list of ndn-tools authors and contributors.
+ *
+ * ndn-tools is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "tools/ping/server/ping-server.hpp"
+#include "tools/ping/client/ping.hpp"
+#include <ndn-cxx/util/dummy-client-face.hpp>
+
+#include "tests/test-common.hpp"
+
+namespace ndn {
+namespace ping {
+namespace tests {
+
+using namespace ndn::tests;
+
+class PingIntegratedFixture : public UnitTestTimeFixture
+{
+public:
+ PingIntegratedFixture()
+ : serverFace(util::makeDummyClientFace(io, {false, true}))
+ , clientFace(util::makeDummyClientFace(io, {false, true}))
+ , numResponses(0)
+ , wantLoss(false)
+ {
+ serverFace->onSendInterest.connect([this] (const Interest& interest) {
+ io.post([=] { if (!wantLoss) { clientFace->receive(interest); } });
+ });
+ clientFace->onSendInterest.connect([this] (const Interest& interest) {
+ io.post([=] { if (!wantLoss) { serverFace->receive(interest); } });
+ });
+ serverFace->onSendData.connect([this] (const Data& data) {
+ io.post([=] { if (!wantLoss) { clientFace->receive(data); } });
+ });
+ clientFace->onSendData.connect([this] (const Data& data) {
+ io.post([=] { if (!wantLoss) { serverFace->receive(data); } });
+ });
+ }
+
+ void onTimeout(uint64_t seq)
+ {
+ numResponses++;
+ if (numResponses == maxResponses) {
+ serverFace->shutdown();
+ clientFace->shutdown();
+ io.stop();
+ }
+ }
+
+ void onData(uint64_t seq)
+ {
+ numResponses++;
+ if (numResponses == maxResponses) {
+ serverFace->shutdown();
+ clientFace->shutdown();
+ io.stop();
+ }
+ }
+
+public:
+ boost::asio::io_service io;
+ shared_ptr<util::DummyClientFace> serverFace;
+ shared_ptr<util::DummyClientFace> clientFace;
+ std::unique_ptr<server::PingServer> server;
+ std::unique_ptr<client::Ping> client;
+ int maxResponses;
+ int numResponses;
+ bool wantLoss;
+};
+
+BOOST_AUTO_TEST_SUITE(PingIntegrated)
+
+BOOST_FIXTURE_TEST_CASE(Normal, PingIntegratedFixture)
+{
+ server::Options serverOpts;
+ serverOpts.prefix = "ndn:/test-prefix";
+ serverOpts.freshnessPeriod = time::milliseconds(5000);
+ serverOpts.nMaxPings = 4;
+ serverOpts.shouldPrintTimestamp = false;
+ serverOpts.payloadSize = 0;
+ server.reset(new server::PingServer(*serverFace, serverOpts));
+ BOOST_REQUIRE_EQUAL(0, server->getNPings());
+ server->start();
+
+ client::Options clientOpts;
+ clientOpts.prefix = "ndn:/test-prefix";
+ clientOpts.shouldAllowStaleData = false;
+ clientOpts.shouldGenerateRandomSeq = false;
+ clientOpts.shouldPrintTimestamp = false;
+ clientOpts.nPings = 4;
+ clientOpts.interval = time::milliseconds(100);
+ clientOpts.timeout = time::milliseconds(2000);
+ clientOpts.startSeq = 1000;
+ client.reset(new client::Ping(*clientFace, clientOpts));
+ client->afterResponse.connect(bind(&PingIntegratedFixture::onData, this, _1));
+ client->afterTimeout.connect(bind(&PingIntegratedFixture::onTimeout, this, _1));
+ maxResponses = 4;
+ client->start();
+
+ this->advanceClocks(io, time::milliseconds(1), 400);
+ io.run();
+
+ BOOST_REQUIRE_EQUAL(4, server->getNPings());
+}
+
+BOOST_FIXTURE_TEST_CASE(Timeout, PingIntegratedFixture)
+{
+ wantLoss = true;
+
+ server::Options serverOpts;
+ serverOpts.prefix = "ndn:/test-prefix";
+ serverOpts.freshnessPeriod = time::milliseconds(5000);
+ serverOpts.nMaxPings = 4;
+ serverOpts.shouldPrintTimestamp = false;
+ serverOpts.payloadSize = 0;
+ server.reset(new server::PingServer(*serverFace, serverOpts));
+ BOOST_REQUIRE_EQUAL(0, server->getNPings());
+ server->start();
+
+ client::Options clientOpts;
+ clientOpts.prefix = "ndn:/test-prefix";
+ clientOpts.shouldAllowStaleData = false;
+ clientOpts.shouldGenerateRandomSeq = false;
+ clientOpts.shouldPrintTimestamp = false;
+ clientOpts.nPings = 4;
+ clientOpts.interval = time::milliseconds(100);
+ clientOpts.timeout = time::milliseconds(500);
+ clientOpts.startSeq = 1000;
+ client.reset(new client::Ping(*clientFace, clientOpts));
+ numResponses = 0;
+ maxResponses = 4;
+ client->afterResponse.connect(bind(&PingIntegratedFixture::onData, this, _1));
+ client->afterTimeout.connect(bind(&PingIntegratedFixture::onTimeout, this, _1));
+ client->start();
+
+ this->advanceClocks(io, time::milliseconds(1), 1000);
+ io.run();
+
+ BOOST_REQUIRE_EQUAL(0, server->getNPings());
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace ping
+} // namespace ndn
diff --git a/tests/ping/server/ping-server.t.cpp b/tests/ping/server/ping-server.t.cpp
new file mode 100644
index 0000000..874f1b2
--- /dev/null
+++ b/tests/ping/server/ping-server.t.cpp
@@ -0,0 +1,96 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2015, Arizona Board of Regents.
+ *
+ * This file is part of ndn-tools (Named Data Networking Essential Tools).
+ * See AUTHORS.md for complete list of ndn-tools authors and contributors.
+ *
+ * ndn-tools is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "tools/ping/server/ping-server.hpp"
+#include <ndn-cxx/util/dummy-client-face.hpp>
+
+#include "tests/test-common.hpp"
+
+namespace ndn {
+namespace ping {
+namespace server {
+namespace tests {
+
+using namespace ndn::tests;
+
+BOOST_AUTO_TEST_SUITE(PingServerPingServer)
+
+class CreatePingServerFixture : public UnitTestTimeFixture
+{
+protected:
+ CreatePingServerFixture()
+ : face(util::makeDummyClientFace(io, {false, true}))
+ , pingOptions(makeOptions())
+ , pingServer(*face, pingOptions)
+ {
+ }
+
+ Interest
+ makePingInterest(int seq) const
+ {
+ Name name(pingOptions.prefix);
+ name.append("ping");
+ name.append(std::to_string(seq));
+ Interest interest(name);
+ interest.setMustBeFresh(true);
+ interest.setInterestLifetime(time::milliseconds(2000));
+ return interest;
+ }
+
+private:
+ static Options
+ makeOptions()
+ {
+ Options opt;
+ opt.prefix = "ndn:/test-prefix";
+ opt.freshnessPeriod = time::milliseconds(5000);
+ opt.nMaxPings = 2;
+ opt.shouldPrintTimestamp = false;
+ opt.payloadSize = 0;
+ return opt;
+ }
+
+protected:
+ boost::asio::io_service io;
+ shared_ptr<util::DummyClientFace> face;
+ Options pingOptions;
+ PingServer pingServer;
+};
+
+BOOST_FIXTURE_TEST_CASE(CreatePingServer, CreatePingServerFixture)
+{
+ BOOST_REQUIRE_EQUAL(0, pingServer.getNPings());
+ pingServer.start();
+
+ this->advanceClocks(io, time::milliseconds(1), 200);
+
+ face->receive(makePingInterest(1000));
+ face->receive(makePingInterest(1001));
+
+ io.run();
+
+ BOOST_REQUIRE_EQUAL(2, pingServer.getNPings());
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace server
+} // namespace ping
+} // namespace ndn