blob: 98abfa45db00dfb8730724fa995df83a1204e6b9 [file] [log] [blame]
Andrea Tosatto672b9a72016-01-05 16:18:20 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2016, Regents of the University of California,
4 * Colorado State University,
5 * University Pierre & Marie Curie, Sorbonne University.
6 *
7 * This file is part of ndn-tools (Named Data Networking Essential Tools).
8 * See AUTHORS.md for complete list of ndn-tools authors and contributors.
9 *
10 * ndn-tools is free software: you can redistribute it and/or modify it under the terms
11 * of the GNU General Public License as published by the Free Software Foundation,
12 * either version 3 of the License, or (at your option) any later version.
13 *
14 * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
20 *
21 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
22 *
23 * @author Andrea Tosatto
24 */
25
26#include "tools/chunks/catchunks/pipeline-interests.hpp"
27#include "tools/chunks/catchunks/data-fetcher.hpp"
28
29#include "tests/test-common.hpp"
30#include <ndn-cxx/util/dummy-client-face.hpp>
31
32namespace ndn {
33namespace chunks {
34namespace tests {
35
36using namespace ndn::tests;
37
38class PipelineInterestsFixture : public UnitTestTimeFixture
39{
40public:
41 typedef PipelineInterestsOptions Options;
42
43public:
44 PipelineInterestsFixture()
45 : face(io)
46 , opt(makeOptions())
47 , name("/ndn/chunks/test")
48 , pipeline(face, opt)
49 , nDataSegments(0)
50 , nReceivedSegments(0)
51 , hasFailed(false)
52 {
53 }
54
55protected:
56 shared_ptr<Data>
57 makeDataWithSegment(uint64_t segmentNo, bool setFinalBlockId = true)
58 {
59 auto data = make_shared<Data>(Name(name).appendVersion(0).appendSegment(segmentNo));
60 if (setFinalBlockId)
61 data->setFinalBlockId(name::Component::fromSegment(nDataSegments - 1));
62 return signData(data);
63 }
64
65 void
66 runWithData(const Data& data)
67 {
68 pipeline.runWithExcludedSegment(data,
69 bind(&PipelineInterestsFixture::onData, this, _1, _2),
70 bind(&PipelineInterestsFixture::onFailure, this, _1));
71 }
72
73private:
74 void
75 onData(const Interest& interest, const Data& data)
76 {
77 nReceivedSegments++;
78 }
79
80 void
81 onFailure(const std::string& reason)
82 {
83 hasFailed = true;
84 }
85
86 static Options
87 makeOptions()
88 {
89 Options options;
90 options.isVerbose = false;
91 options.interestLifetime = time::seconds(1);
92 options.maxRetriesOnTimeoutOrNack = 3;
93 options.maxPipelineSize = 5;
94 return options;
95 }
96
97protected:
98 boost::asio::io_service io;
99 util::DummyClientFace face;
100 Options opt;
101 Name name;
102 PipelineInterests pipeline;
103 uint64_t nDataSegments;
104 uint64_t nReceivedSegments;
105 bool hasFailed;
106};
107
108BOOST_AUTO_TEST_SUITE(Chunks)
109BOOST_AUTO_TEST_SUITE(TestPipelineInterests)
110
111BOOST_FIXTURE_TEST_CASE(FewerSegmentsThanPipelineCapacity, PipelineInterestsFixture)
112{
113 nDataSegments = 3;
114 BOOST_ASSERT(nDataSegments <= opt.maxPipelineSize);
115
116 runWithData(*makeDataWithSegment(0));
117 advanceClocks(io, time::nanoseconds(1), 1);
118 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), nDataSegments - 1);
119
120 for (uint64_t i = 0; i < nDataSegments - 1; ++i) {
121 face.receive(*makeDataWithSegment(i));
122 advanceClocks(io, time::nanoseconds(1), 1);
123
124 BOOST_CHECK_EQUAL(nReceivedSegments, i);
125 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), nDataSegments - 1);
126 // check if the interest for the segment i+1 is well formed
127 auto sentInterest = face.sentInterests[i];
128 BOOST_CHECK_EQUAL(sentInterest.getExclude().size(), 0);
129 BOOST_CHECK_EQUAL(sentInterest.getMaxSuffixComponents(), 1);
130 BOOST_CHECK_EQUAL(sentInterest.getMustBeFresh(), opt.mustBeFresh);
131 BOOST_CHECK_EQUAL(Name(name).isPrefixOf(sentInterest.getName()), true);
132 BOOST_CHECK_EQUAL(sentInterest.getName()[-1].toSegment(), i + 1);
133 }
134
135 BOOST_CHECK_EQUAL(hasFailed, false);
136
137 advanceClocks(io, ndn::DEFAULT_INTEREST_LIFETIME, opt.maxRetriesOnTimeoutOrNack + 1);
138 BOOST_CHECK_EQUAL(hasFailed, true);
139}
140
141BOOST_FIXTURE_TEST_CASE(FullPipeline, PipelineInterestsFixture)
142{
143 nDataSegments = 13;
144 BOOST_ASSERT(nDataSegments > opt.maxPipelineSize);
145
146 runWithData(*makeDataWithSegment(nDataSegments - 1));
147 advanceClocks(io, time::nanoseconds(1), 1);
148 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), opt.maxPipelineSize);
149
150 for (uint64_t i = 0; i < nDataSegments - 1; ++i) {
151 face.receive(*makeDataWithSegment(i));
152 advanceClocks(io, time::nanoseconds(1), 1);
153 BOOST_CHECK_EQUAL(nReceivedSegments, i + 1);
154
155 if (i < nDataSegments - opt.maxPipelineSize - 1) {
156 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), opt.maxPipelineSize + i + 1);
157 // check if the interest for the segment i+1 is well formed
158 auto sentInterest = face.sentInterests[i];
159 BOOST_CHECK_EQUAL(sentInterest.getExclude().size(), 0);
160 BOOST_CHECK_EQUAL(sentInterest.getMaxSuffixComponents(), 1);
161 BOOST_CHECK_EQUAL(sentInterest.getMustBeFresh(), opt.mustBeFresh);
162 BOOST_CHECK_EQUAL(Name(name).isPrefixOf(sentInterest.getName()), true);
163 BOOST_CHECK_EQUAL(sentInterest.getName()[-1].toSegment(), i);
164 }
165 else {
166 // all the interests have been sent for all the segments
167 BOOST_CHECK_EQUAL(face.sentInterests.size(), nDataSegments - 1);
168 }
169 }
170
171 BOOST_CHECK_EQUAL(hasFailed, false);
172}
173
174BOOST_FIXTURE_TEST_CASE(TimeoutAllSegments, PipelineInterestsFixture)
175{
176 nDataSegments = 13;
177 BOOST_ASSERT(nDataSegments > opt.maxPipelineSize);
178
179 runWithData(*makeDataWithSegment(nDataSegments - 1));
180 advanceClocks(io, time::nanoseconds(1), 1);
181 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), opt.maxPipelineSize);
182
183 for (int i = 0; i < opt.maxRetriesOnTimeoutOrNack; ++i) {
184 advanceClocks(io, opt.interestLifetime, 1);
185 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), opt.maxPipelineSize * (i + 2));
186 BOOST_CHECK_EQUAL(nReceivedSegments, 0);
187
188 // A single retry for every pipeline element
189 for (size_t j = 0; j < opt.maxPipelineSize; ++j) {
190 auto interest = face.sentInterests[(opt.maxPipelineSize * (i + 1)) + j];
191 BOOST_CHECK_EQUAL(static_cast<size_t>(interest.getName()[-1].toSegment()), j);
192 }
193 }
194
195 advanceClocks(io, opt.interestLifetime, 1);
196 BOOST_CHECK_EQUAL(hasFailed, true);
197}
198
199BOOST_FIXTURE_TEST_CASE(TimeoutAfterFinalBlockIdReceived, PipelineInterestsFixture)
200{
201 // the FinalBlockId is sent with the first segment, after the first segment failure the pipeline
202 // should fail
203
204 nDataSegments = 18;
205 BOOST_ASSERT(nDataSegments > opt.maxPipelineSize);
206
207 runWithData(*makeDataWithSegment(nDataSegments - 1));
208 advanceClocks(io, time::nanoseconds(1), 1);
209 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), opt.maxPipelineSize);
210
211 // send a single segment for each pipeline element but not the first element
212 advanceClocks(io, opt.interestLifetime, 1);
213 for (uint64_t i = 1; i < opt.maxPipelineSize; ++i) {
214 face.receive(*makeDataWithSegment(i));
215 advanceClocks(io, time::nanoseconds(1), 1);
216 }
217
218 // send a single data packet for each pipeline element
219 advanceClocks(io, opt.interestLifetime, opt.maxRetriesOnTimeoutOrNack - 1);
220 for (uint64_t i = 0; i < opt.maxPipelineSize - 1; ++i) {
221 face.receive(*makeDataWithSegment(opt.maxPipelineSize + i));
222 advanceClocks(io, time::nanoseconds(1), 1);
223 }
224 advanceClocks(io, opt.interestLifetime, 1);
225
226 size_t interestAfterFailure = face.sentInterests.size();
227
228 BOOST_CHECK_EQUAL(face.getNPendingInterests(), 0);
229 BOOST_CHECK_EQUAL(hasFailed, true);
230
231 // these new segments should not generate new interests
232 advanceClocks(io, opt.interestLifetime, 1);
233 for (uint64_t i = 0; i < opt.maxPipelineSize - 1; ++i) {
234 face.receive(*makeDataWithSegment(opt.maxPipelineSize * 2 + i - 1));
235 advanceClocks(io, time::nanoseconds(1), 1);
236 }
237
238 // no more interests after a failure
239 advanceClocks(io, opt.interestLifetime, opt.maxRetriesOnTimeoutOrNack);
240 BOOST_CHECK_EQUAL(interestAfterFailure, face.sentInterests.size());
241 BOOST_CHECK_EQUAL(face.getNPendingInterests(), 0);
242}
243
244BOOST_FIXTURE_TEST_CASE(TimeoutBeforeFinalBlockIdReceived, PipelineInterestsFixture)
245{
246 // the FinalBlockId is sent only with the last segment, all segments are sent except for the
247 // second one (segment #1); all segments are received correctly until the FinalBlockId is received
248
249 nDataSegments = 22;
250 BOOST_ASSERT(nDataSegments > opt.maxPipelineSize);
251
252 runWithData(*makeDataWithSegment(nDataSegments - 1, false));
253 advanceClocks(io, time::nanoseconds(1), 1);
254 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), opt.maxPipelineSize);
255
256 advanceClocks(io, opt.interestLifetime, 1);
257 for (uint64_t i = 2; i < opt.maxPipelineSize; ++i) {
258 face.receive(*makeDataWithSegment(i, false));
259 advanceClocks(io, time::nanoseconds(1), 1);
260
261 auto lastInterest = face.sentInterests.back();
262 BOOST_CHECK_EQUAL(lastInterest.getName()[-1].toSegment(), opt.maxPipelineSize + i - 2);
263 }
264 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), opt.maxPipelineSize * 3 - 2);
265
266 // nack for the first pipeline element (segment #0)
267 auto nack = make_shared<lp::Nack>(face.sentInterests[opt.maxPipelineSize]);
268 nack->setReason(lp::NackReason::DUPLICATE);
269 face.receive(*nack);
270
271 // all the pipeline elements are two retries near the timeout error, but not the
272 // second (segment #1) that is only one retry near the timeout
273 advanceClocks(io, opt.interestLifetime, opt.maxRetriesOnTimeoutOrNack - 1);
274 BOOST_CHECK_EQUAL(hasFailed, false);
275
276 // data for the first pipeline element (segment #0)
277 face.receive(*makeDataWithSegment(0, false));
278 BOOST_CHECK_EQUAL(hasFailed, false);
279
280 // data for all the pipeline element, but not the second (segment #1)
281 for (uint64_t i = opt.maxPipelineSize; i < nDataSegments - 1; ++i) {
282 if (i == nDataSegments - 2) {
283 face.receive(*makeDataWithSegment(i, true));
284 }
285 else {
286 face.receive(*makeDataWithSegment(i, false));
287 }
288 advanceClocks(io, time::nanoseconds(1), 1);
289 }
290 // timeout for the second pipeline element (segment #1), this should trigger an error
291 advanceClocks(io, opt.interestLifetime, 1);
292
293 BOOST_CHECK_EQUAL(nReceivedSegments, nDataSegments - 2);
294 BOOST_CHECK_EQUAL(hasFailed, true);
295}
296
297BOOST_FIXTURE_TEST_CASE(SegmentReceivedAfterTimeout, PipelineInterestsFixture)
298{
299 // the FinalBlockId is never sent, all the pipeline elements with a segment number greater than
300 // segment #0 will fail, after this failure also segment #0 fail and this should trigger an error
301
302 nDataSegments = 22;
303 BOOST_ASSERT(nDataSegments > opt.maxPipelineSize);
304
305 runWithData(*makeDataWithSegment(nDataSegments - 1, false));
306 advanceClocks(io, time::nanoseconds(1), 1);
307 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), opt.maxPipelineSize);
308
309 advanceClocks(io, opt.interestLifetime, 1);
310
311 // nack for the first pipeline element (segment #0)
312 auto nack = make_shared<lp::Nack>(face.sentInterests[opt.maxPipelineSize]);
313 nack->setReason(lp::NackReason::DUPLICATE);
314 face.receive(*nack);
315 BOOST_CHECK_EQUAL(hasFailed, false);
316
317 // timeout for all the pipeline elements, but not the first (segment #0)
318 advanceClocks(io, opt.interestLifetime, opt.maxRetriesOnTimeoutOrNack);
319 BOOST_CHECK_EQUAL(hasFailed, false);
320
321 // data for the first pipeline element (segment #0), this should trigger an error because the
322 // others pipeline elements failed
323 face.receive(*makeDataWithSegment(0, false));
324 advanceClocks(io, time::nanoseconds(1), 1);
325
326 BOOST_CHECK_EQUAL(nReceivedSegments, 1);
327 BOOST_CHECK_EQUAL(hasFailed, true);
328}
329
330BOOST_FIXTURE_TEST_CASE(CongestionAllSegments, PipelineInterestsFixture)
331{
332 nDataSegments = 13;
333 BOOST_ASSERT(nDataSegments > opt.maxPipelineSize);
334
335 runWithData(*makeDataWithSegment(nDataSegments - 1));
336 advanceClocks(io, time::nanoseconds(1), 1);
337 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), opt.maxPipelineSize);
338
339 // send nack for all the pipeline elements first interest
340 for (size_t j = 0; j < opt.maxPipelineSize; j++) {
341 auto nack = make_shared<lp::Nack>(face.sentInterests[j]);
342 nack->setReason(lp::NackReason::CONGESTION);
343 face.receive(*nack);
344 advanceClocks(io, time::nanoseconds(1), 1);
345 }
346
347 // send nack for all the pipeline elements interests after the first
348 for (int i = 1; i <= opt.maxRetriesOnTimeoutOrNack; ++i) {
349 time::milliseconds backoffTime(static_cast<uint64_t>(std::pow(2, i)));
350 if (backoffTime > DataFetcher::MAX_CONGESTION_BACKOFF_TIME)
351 backoffTime = DataFetcher::MAX_CONGESTION_BACKOFF_TIME;
352
353 advanceClocks(io, backoffTime, 1);
354 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), opt.maxPipelineSize * (i +1));
355
356 // A single retry for every pipeline element
357 for (size_t j = 0; j < opt.maxPipelineSize; ++j) {
358 auto interest = face.sentInterests[(opt.maxPipelineSize * i) + j];
359 BOOST_CHECK_LT(static_cast<size_t>(interest.getName()[-1].toSegment()), opt.maxPipelineSize);
360 }
361
362 for (size_t j = 0; j < opt.maxPipelineSize; j++) {
363 auto nack = make_shared<lp::Nack>(face.sentInterests[(opt.maxPipelineSize * i) + j]);
364 nack->setReason(lp::NackReason::CONGESTION);
365 face.receive(*nack);
366 advanceClocks(io, time::nanoseconds(1), 1);
367 }
368 }
369
370 BOOST_CHECK_EQUAL(hasFailed, true);
371}
372
373BOOST_AUTO_TEST_SUITE_END() // TestPipelineInterests
374BOOST_AUTO_TEST_SUITE_END() // Chunks
375
376} // namespace tests
377} // namespace chunks
378} // namespace ndn