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