blob: 9f9eeb90fc3450926412189423bb62c110675c84 [file] [log] [blame]
Eric Newberrya93680e2015-07-15 19:25:29 -07001/* -*- 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/client/ping.hpp"
21#include <ndn-cxx/util/dummy-client-face.hpp>
22
23#include "tests/test-common.hpp"
24
25namespace ndn {
26namespace ping {
27namespace client {
28namespace tests {
29
30using namespace ndn::tests;
31
32BOOST_AUTO_TEST_SUITE(PingClientPing)
33
34class SequenceNumberIncrementFixture : public UnitTestTimeFixture
35{
36protected:
37 SequenceNumberIncrementFixture()
38 : face(util::makeDummyClientFace(io, {false, true}))
39 , pingOptions(makeOptions())
40 , ping(*face, pingOptions)
41 , numPings(0)
42 , lastPingSeq(pingOptions.startSeq)
43 {
44 ping.afterResponse.connect(bind(&SequenceNumberIncrementFixture::onResponse, this, _1));
45 ping.afterTimeout.connect(bind(&SequenceNumberIncrementFixture::onTimeout, this, _1));
46 }
47
48public:
49 void
50 onResponse(uint64_t seq)
51 {
52 numPings++;
53 lastPingSeq = seq;
54 if (numPings == maxPings) {
55 face->shutdown();
56 io.stop();
57 }
58 }
59
60 void
61 onTimeout(uint64_t seq)
62 {
63 numPings++;
64 lastPingSeq = seq;
65 if (numPings == maxPings) {
66 face->shutdown();
67 io.stop();
68 }
69 }
70
71private:
72 static Options
73 makeOptions()
74 {
75 Options opt;
76 opt.prefix = "ndn:/test-prefix";
77 opt.shouldAllowStaleData = false;
78 opt.shouldGenerateRandomSeq = false;
79 opt.shouldPrintTimestamp = false;
80 opt.nPings = 4;
81 opt.interval = time::milliseconds(100);
82 opt.timeout = time::milliseconds(1000);
83 opt.startSeq = 1000;
84 return opt;
85 }
86
87protected:
88 boost::asio::io_service io;
89 shared_ptr<util::DummyClientFace> face;
90 Options pingOptions;
91 Ping ping;
92 uint32_t numPings;
93 uint32_t maxPings;
94 uint64_t lastPingSeq;
95 KeyChain keyChain;
96};
97
98BOOST_FIXTURE_TEST_CASE(SequenceNumberIncrement, SequenceNumberIncrementFixture)
99{
100 maxPings = 4;
101 ping.start();
102
103 this->advanceClocks(io, time::milliseconds(1), 400);
104
105 face->receive(*makeData("ndn:/test-prefix/ping/1000"));
106 face->receive(*makeData("ndn:/test-prefix/ping/1001"));
107 face->receive(*makeData("ndn:/test-prefix/ping/1002"));
108 face->receive(*makeData("ndn:/test-prefix/ping/1003"));
109
110 io.run();
111
112 BOOST_REQUIRE_EQUAL(1003, lastPingSeq);
113 BOOST_REQUIRE_EQUAL(4, numPings);
114}
115
116BOOST_AUTO_TEST_SUITE_END()
117
118} // namespace tests
119} // namespace client
120} // namespace ping
121} // namespace ndn