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 | |
| 94 | BOOST_FIXTURE_TEST_CASE (Unix, FacesFixture) |
| 95 | { |
| 96 | Face face; |
| 97 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 98 | face.expressInterest(Interest("/%C1.M.S.localhost/%C1.M.SRV/ndnd/KEY", time::milliseconds(1000)), |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 99 | ptr_lib::bind(&FacesFixture::onData, this), |
| 100 | ptr_lib::bind(&FacesFixture::onTimeout, this)); |
| 101 | |
| 102 | BOOST_REQUIRE_NO_THROW(face.processEvents()); |
| 103 | |
| 104 | BOOST_CHECK_EQUAL(dataCount, 1); |
| 105 | BOOST_CHECK_EQUAL(timeoutCount, 0); |
| 106 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 107 | face.expressInterest(Interest("/localhost/non-existing/data/should/not/exist/anywhere", time::milliseconds(50)), |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 108 | ptr_lib::bind(&FacesFixture::onData, this), |
| 109 | ptr_lib::bind(&FacesFixture::onTimeout, this)); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 110 | |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 111 | BOOST_REQUIRE_NO_THROW(face.processEvents()); |
| 112 | |
| 113 | BOOST_CHECK_EQUAL(dataCount, 1); |
| 114 | BOOST_CHECK_EQUAL(timeoutCount, 1); |
| 115 | } |
| 116 | |
| 117 | BOOST_FIXTURE_TEST_CASE (Tcp, FacesFixture) |
| 118 | { |
| 119 | Face face("localhost"); |
| 120 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 121 | face.expressInterest(Interest("/%C1.M.S.localhost/%C1.M.SRV/ndnd/KEY", time::milliseconds(1000)), |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 122 | bind(&FacesFixture::onData, this), |
| 123 | bind(&FacesFixture::onTimeout, this)); |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 124 | |
| 125 | BOOST_REQUIRE_NO_THROW(face.processEvents()); |
| 126 | |
| 127 | BOOST_CHECK_EQUAL(dataCount, 1); |
| 128 | BOOST_CHECK_EQUAL(timeoutCount, 0); |
| 129 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 130 | face.expressInterest(Interest("/localhost/non-existing/data/should/not/exist/anywhere", time::milliseconds(50)), |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 131 | bind(&FacesFixture::onData, this), |
| 132 | bind(&FacesFixture::onTimeout, this)); |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 133 | |
| 134 | BOOST_REQUIRE_NO_THROW(face.processEvents()); |
| 135 | |
| 136 | BOOST_CHECK_EQUAL(dataCount, 1); |
| 137 | BOOST_CHECK_EQUAL(timeoutCount, 1); |
| 138 | } |
| 139 | |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 140 | |
| 141 | BOOST_FIXTURE_TEST_CASE (SetFilter, FacesFixture) |
| 142 | { |
| 143 | Face face; |
| 144 | Face face2(face.ioService()); |
| 145 | Scheduler scheduler(*face.ioService()); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 146 | scheduler.scheduleEvent(time::milliseconds(300), |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 147 | bind(&FacesFixture::terminate, this, func_lib::ref(face))); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 148 | |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 149 | regPrefixId = face.setInterestFilter("/Hello/World", |
| 150 | bind(&FacesFixture::onInterest, this, func_lib::ref(face)), |
| 151 | bind(&FacesFixture::onRegFailed, this)); |
| 152 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 153 | scheduler.scheduleEvent(time::milliseconds(200), |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 154 | bind(&FacesFixture::expressInterest, this, |
| 155 | func_lib::ref(face2), Name("/Hello/World/!"))); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 156 | |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 157 | BOOST_REQUIRE_NO_THROW(face.processEvents()); |
| 158 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 159 | BOOST_CHECK_EQUAL(regFailedCount, 0); |
| 160 | BOOST_CHECK_EQUAL(inInterestCount, 1); |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 161 | BOOST_CHECK_EQUAL(timeoutCount, 1); |
| 162 | BOOST_CHECK_EQUAL(dataCount, 0); |
| 163 | } |
| 164 | |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 165 | BOOST_FIXTURE_TEST_CASE (SetTwoFilters, FacesFixture) |
| 166 | { |
| 167 | Face face; |
| 168 | Face face2(face.ioService()); |
| 169 | Scheduler scheduler(*face.ioService()); |
Obaid | 6e7f5f1 | 2014-03-11 14:46:10 -0500 | [diff] [blame] | 170 | scheduler.scheduleEvent(time::seconds(1), |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 171 | bind(&FacesFixture::terminate, this, func_lib::ref(face))); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 172 | |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 173 | regPrefixId = face.setInterestFilter("/Hello/World", |
| 174 | bind(&FacesFixture::onInterest, this, func_lib::ref(face)), |
| 175 | bind(&FacesFixture::onRegFailed, this)); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 176 | |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 177 | regPrefixId2 = face.setInterestFilter("/Los/Angeles/Lakers", |
| 178 | bind(&FacesFixture::onInterest2, this, func_lib::ref(face)), |
| 179 | bind(&FacesFixture::onRegFailed, this)); |
| 180 | |
| 181 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 182 | scheduler.scheduleEvent(time::milliseconds(200), |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 183 | bind(&FacesFixture::expressInterest, this, |
| 184 | func_lib::ref(face2), Name("/Hello/World/!"))); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 185 | |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 186 | BOOST_REQUIRE_NO_THROW(face.processEvents()); |
| 187 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 188 | BOOST_CHECK_EQUAL(regFailedCount, 0); |
| 189 | BOOST_CHECK_EQUAL(inInterestCount, 1); |
| 190 | BOOST_CHECK_EQUAL(inInterestCount2, 0); |
Alexander Afanasyev | a68aa7f | 2014-02-11 15:42:33 -0800 | [diff] [blame] | 191 | BOOST_CHECK_EQUAL(timeoutCount, 1); |
| 192 | BOOST_CHECK_EQUAL(dataCount, 0); |
| 193 | } |
| 194 | |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 195 | BOOST_AUTO_TEST_SUITE_END() |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 196 | |
| 197 | } // namespace ndn |