Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2014, Regents of the University of California. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 7 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 8 | * |
| 9 | * This file licensed under New BSD License. See COPYING for detailed information about |
| 10 | * ndn-cxx library copyright, permissions, and redistribution restrictions. |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 11 | */ |
| 12 | |
Alexander Afanasyev | 09c613f | 2014-01-29 00:23:58 -0800 | [diff] [blame] | 13 | #include "face.hpp" |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 14 | #include "util/scheduler.hpp" |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 15 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 16 | #include "boost-test.hpp" |
| 17 | |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 18 | namespace ndn { |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 19 | |
| 20 | BOOST_AUTO_TEST_SUITE(TestFaces) |
| 21 | |
| 22 | struct FacesFixture |
| 23 | { |
| 24 | FacesFixture() |
| 25 | : dataCount(0) |
| 26 | , timeoutCount(0) |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 27 | , regPrefixId(0) |
| 28 | , inInterestCount(0) |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 29 | , inInterestCount2(0) |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 30 | , regFailedCount(0) |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 31 | { |
| 32 | } |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 33 | |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 34 | void |
| 35 | onData() |
| 36 | { |
| 37 | ++dataCount; |
| 38 | } |
| 39 | |
| 40 | void |
| 41 | onTimeout() |
| 42 | { |
| 43 | ++timeoutCount; |
| 44 | } |
| 45 | |
| 46 | void |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 47 | onInterest(Face& face) |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 48 | { |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 49 | ++inInterestCount; |
| 50 | |
| 51 | face.unsetInterestFilter(regPrefixId); |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | void |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 55 | onInterest2(Face& face) |
| 56 | { |
| 57 | ++inInterestCount2; |
| 58 | |
| 59 | face.unsetInterestFilter(regPrefixId2); |
| 60 | } |
| 61 | |
| 62 | void |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 63 | onRegFailed() |
| 64 | { |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 65 | ++regFailedCount; |
| 66 | } |
| 67 | |
| 68 | void |
| 69 | expressInterest(Face& face, const Name& name) |
| 70 | { |
| 71 | Interest i(name); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 72 | i.setInterestLifetime(time::milliseconds(50)); |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 73 | face.expressInterest(i, |
| 74 | bind(&FacesFixture::onData, this), |
| 75 | bind(&FacesFixture::onTimeout, this)); |
| 76 | } |
| 77 | |
| 78 | void |
| 79 | terminate(Face& face) |
| 80 | { |
| 81 | face.shutdown(); |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | uint32_t dataCount; |
| 85 | uint32_t timeoutCount; |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 86 | |
| 87 | const RegisteredPrefixId* regPrefixId; |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 88 | const RegisteredPrefixId* regPrefixId2; |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 89 | uint32_t inInterestCount; |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 90 | uint32_t inInterestCount2; |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 91 | uint32_t regFailedCount; |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 92 | }; |
| 93 | |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame^] | 94 | BOOST_FIXTURE_TEST_CASE(Unix, FacesFixture) |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 95 | { |
| 96 | Face face; |
| 97 | |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame^] | 98 | face.expressInterest(Interest("/", time::milliseconds(1000)), |
| 99 | bind(&FacesFixture::onData, this), |
| 100 | bind(&FacesFixture::onTimeout, this)); |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 101 | |
| 102 | BOOST_REQUIRE_NO_THROW(face.processEvents()); |
| 103 | |
| 104 | BOOST_CHECK_EQUAL(dataCount, 1); |
| 105 | BOOST_CHECK_EQUAL(timeoutCount, 0); |
| 106 | |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame^] | 107 | face.expressInterest(Interest("/localhost/non-existing/data/should/not/exist/anywhere", |
| 108 | time::milliseconds(50)), |
| 109 | bind(&FacesFixture::onData, this), |
| 110 | bind(&FacesFixture::onTimeout, this)); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 111 | |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 112 | BOOST_REQUIRE_NO_THROW(face.processEvents()); |
| 113 | |
| 114 | BOOST_CHECK_EQUAL(dataCount, 1); |
| 115 | BOOST_CHECK_EQUAL(timeoutCount, 1); |
| 116 | } |
| 117 | |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame^] | 118 | BOOST_FIXTURE_TEST_CASE(Tcp, FacesFixture) |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 119 | { |
| 120 | Face face("localhost"); |
| 121 | |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame^] | 122 | face.expressInterest(Interest("/", time::milliseconds(1000)), |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 123 | bind(&FacesFixture::onData, this), |
| 124 | bind(&FacesFixture::onTimeout, this)); |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 125 | |
| 126 | BOOST_REQUIRE_NO_THROW(face.processEvents()); |
| 127 | |
| 128 | BOOST_CHECK_EQUAL(dataCount, 1); |
| 129 | BOOST_CHECK_EQUAL(timeoutCount, 0); |
| 130 | |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame^] | 131 | face.expressInterest(Interest("/localhost/non-existing/data/should/not/exist/anywhere", |
| 132 | time::milliseconds(50)), |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 133 | bind(&FacesFixture::onData, this), |
| 134 | bind(&FacesFixture::onTimeout, this)); |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 135 | |
| 136 | BOOST_REQUIRE_NO_THROW(face.processEvents()); |
| 137 | |
| 138 | BOOST_CHECK_EQUAL(dataCount, 1); |
| 139 | BOOST_CHECK_EQUAL(timeoutCount, 1); |
| 140 | } |
| 141 | |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 142 | |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame^] | 143 | BOOST_FIXTURE_TEST_CASE(SetFilter, FacesFixture) |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 144 | { |
| 145 | Face face; |
| 146 | Face face2(face.ioService()); |
| 147 | Scheduler scheduler(*face.ioService()); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 148 | scheduler.scheduleEvent(time::milliseconds(300), |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame^] | 149 | bind(&FacesFixture::terminate, this, ref(face))); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 150 | |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 151 | regPrefixId = face.setInterestFilter("/Hello/World", |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame^] | 152 | bind(&FacesFixture::onInterest, this, ref(face)), |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 153 | bind(&FacesFixture::onRegFailed, this)); |
| 154 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 155 | scheduler.scheduleEvent(time::milliseconds(200), |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 156 | bind(&FacesFixture::expressInterest, this, |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame^] | 157 | ref(face2), Name("/Hello/World/!"))); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 158 | |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 159 | BOOST_REQUIRE_NO_THROW(face.processEvents()); |
| 160 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 161 | BOOST_CHECK_EQUAL(regFailedCount, 0); |
| 162 | BOOST_CHECK_EQUAL(inInterestCount, 1); |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 163 | BOOST_CHECK_EQUAL(timeoutCount, 1); |
| 164 | BOOST_CHECK_EQUAL(dataCount, 0); |
| 165 | } |
| 166 | |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame^] | 167 | BOOST_FIXTURE_TEST_CASE(SetTwoFilters, FacesFixture) |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 168 | { |
| 169 | Face face; |
| 170 | Face face2(face.ioService()); |
| 171 | Scheduler scheduler(*face.ioService()); |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 172 | scheduler.scheduleEvent(time::seconds(1), |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame^] | 173 | bind(&FacesFixture::terminate, this, ref(face))); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 174 | |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 175 | regPrefixId = face.setInterestFilter("/Hello/World", |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame^] | 176 | bind(&FacesFixture::onInterest, this, ref(face)), |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 177 | bind(&FacesFixture::onRegFailed, this)); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 178 | |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 179 | regPrefixId2 = face.setInterestFilter("/Los/Angeles/Lakers", |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame^] | 180 | bind(&FacesFixture::onInterest2, this, ref(face)), |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 181 | bind(&FacesFixture::onRegFailed, this)); |
| 182 | |
| 183 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 184 | scheduler.scheduleEvent(time::milliseconds(200), |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 185 | bind(&FacesFixture::expressInterest, this, |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame^] | 186 | ref(face2), Name("/Hello/World/!"))); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 187 | |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 188 | BOOST_REQUIRE_NO_THROW(face.processEvents()); |
| 189 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 190 | BOOST_CHECK_EQUAL(regFailedCount, 0); |
| 191 | BOOST_CHECK_EQUAL(inInterestCount, 1); |
| 192 | BOOST_CHECK_EQUAL(inInterestCount2, 0); |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 193 | BOOST_CHECK_EQUAL(timeoutCount, 1); |
| 194 | BOOST_CHECK_EQUAL(dataCount, 0); |
| 195 | } |
| 196 | |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 197 | BOOST_AUTO_TEST_SUITE_END() |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 198 | |
| 199 | } // namespace ndn |