Klaus Schneider | cf1d0c0 | 2019-08-31 19:05:40 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, Regents of the University of California, |
Klaus Schneider | cf1d0c0 | 2019-08-31 19:05:40 -0700 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #include "fw/random-strategy.hpp" |
Klaus Schneider | cf1d0c0 | 2019-08-31 19:05:40 -0700 | [diff] [blame] | 27 | |
| 28 | #include "tests/test-common.hpp" |
| 29 | #include "tests/daemon/face/dummy-face.hpp" |
| 30 | #include "strategy-tester.hpp" |
| 31 | |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 32 | #include <unordered_map> |
| 33 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 34 | namespace nfd::tests { |
Klaus Schneider | cf1d0c0 | 2019-08-31 19:05:40 -0700 | [diff] [blame] | 35 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 36 | using RandomStrategyTester = StrategyTester<fw::RandomStrategy>; |
Klaus Schneider | cf1d0c0 | 2019-08-31 19:05:40 -0700 | [diff] [blame] | 37 | NFD_REGISTER_STRATEGY(RandomStrategyTester); |
| 38 | |
| 39 | BOOST_AUTO_TEST_SUITE(Fw) |
| 40 | |
| 41 | class RandomStrategyFixture : public GlobalIoTimeFixture |
| 42 | { |
| 43 | protected: |
| 44 | RandomStrategyFixture() |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 45 | : face1(make_shared<DummyFace>()) |
Klaus Schneider | cf1d0c0 | 2019-08-31 19:05:40 -0700 | [diff] [blame] | 46 | , face2(make_shared<DummyFace>()) |
| 47 | , face3(make_shared<DummyFace>()) |
| 48 | , face4(make_shared<DummyFace>()) |
| 49 | { |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 50 | faceTable.add(face1); |
| 51 | faceTable.add(face2); |
| 52 | faceTable.add(face3); |
| 53 | faceTable.add(face4); |
Klaus Schneider | cf1d0c0 | 2019-08-31 19:05:40 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 56 | protected: |
| 57 | FaceTable faceTable; |
| 58 | Forwarder forwarder{faceTable}; |
| 59 | RandomStrategyTester strategy{forwarder}; |
| 60 | Fib& fib{forwarder.getFib()}; |
| 61 | Pit& pit{forwarder.getPit()}; |
Klaus Schneider | cf1d0c0 | 2019-08-31 19:05:40 -0700 | [diff] [blame] | 62 | |
| 63 | shared_ptr<DummyFace> face1; |
| 64 | shared_ptr<DummyFace> face2; |
| 65 | shared_ptr<DummyFace> face3; |
| 66 | shared_ptr<DummyFace> face4; |
| 67 | }; |
| 68 | |
| 69 | BOOST_FIXTURE_TEST_SUITE(TestRandomStrategy, RandomStrategyFixture) |
| 70 | |
| 71 | BOOST_AUTO_TEST_CASE(Forward) |
| 72 | { |
| 73 | fib::Entry& fibEntry = *fib.insert(Name()).first; |
| 74 | fib.addOrUpdateNextHop(fibEntry, *face2, 10); |
| 75 | fib.addOrUpdateNextHop(fibEntry, *face3, 20); |
| 76 | fib.addOrUpdateNextHop(fibEntry, *face4, 30); |
| 77 | |
| 78 | // Send 1000 Interests |
| 79 | for (int i = 0; i < 1000; ++i) { |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 80 | auto interest = makeInterest("ndn:/BzgFBchqA" + std::to_string(i)); |
| 81 | auto pitEntry = pit.insert(*interest).first; |
Klaus Schneider | cf1d0c0 | 2019-08-31 19:05:40 -0700 | [diff] [blame] | 82 | |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 83 | pitEntry->insertOrUpdateInRecord(*face1, *interest); |
Teng Liang | d94b7b3 | 2022-07-10 21:29:37 +0800 | [diff] [blame] | 84 | strategy.afterReceiveInterest(*interest, FaceEndpoint(*face1), pitEntry); |
Klaus Schneider | cf1d0c0 | 2019-08-31 19:05:40 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 87 | // Map outFaceId -> SentInterests |
| 88 | std::unordered_map<FaceId, int> faceInterestMap; |
Klaus Schneider | cf1d0c0 | 2019-08-31 19:05:40 -0700 | [diff] [blame] | 89 | for (const auto& i : strategy.sendInterestHistory) { |
| 90 | faceInterestMap[i.outFaceId]++; |
| 91 | } |
| 92 | |
| 93 | // Check that all faces received at least 10 Interest |
| 94 | for (const auto& x : faceInterestMap) { |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 95 | BOOST_TEST(x.second >= 10); |
Klaus Schneider | cf1d0c0 | 2019-08-31 19:05:40 -0700 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
| 99 | BOOST_AUTO_TEST_SUITE_END() // TestRandomStrategy |
| 100 | BOOST_AUTO_TEST_SUITE_END() // Fw |
| 101 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 102 | } // namespace nfd::tests |