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) |
| 22 | , regFailedCount(0) |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 23 | { |
| 24 | } |
| 25 | |
| 26 | void |
| 27 | onData() |
| 28 | { |
| 29 | ++dataCount; |
| 30 | } |
| 31 | |
| 32 | void |
| 33 | onTimeout() |
| 34 | { |
| 35 | ++timeoutCount; |
| 36 | } |
| 37 | |
| 38 | void |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 39 | onInterest(Face& face) |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 40 | { |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 41 | ++inInterestCount; |
| 42 | |
| 43 | face.unsetInterestFilter(regPrefixId); |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | void |
| 47 | onRegFailed() |
| 48 | { |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 49 | ++regFailedCount; |
| 50 | } |
| 51 | |
| 52 | void |
| 53 | expressInterest(Face& face, const Name& name) |
| 54 | { |
| 55 | Interest i(name); |
| 56 | i.setInterestLifetime(50); |
| 57 | face.expressInterest(i, |
| 58 | bind(&FacesFixture::onData, this), |
| 59 | bind(&FacesFixture::onTimeout, this)); |
| 60 | } |
| 61 | |
| 62 | void |
| 63 | terminate(Face& face) |
| 64 | { |
| 65 | face.shutdown(); |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | uint32_t dataCount; |
| 69 | uint32_t timeoutCount; |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 70 | |
| 71 | const RegisteredPrefixId* regPrefixId; |
| 72 | uint32_t inInterestCount; |
| 73 | uint32_t regFailedCount; |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | BOOST_FIXTURE_TEST_CASE (Unix, FacesFixture) |
| 77 | { |
| 78 | Face face; |
| 79 | |
| 80 | face.expressInterest(Interest("/%C1.M.S.localhost/%C1.M.SRV/ndnd/KEY", 1000), |
| 81 | ptr_lib::bind(&FacesFixture::onData, this), |
| 82 | ptr_lib::bind(&FacesFixture::onTimeout, this)); |
| 83 | |
| 84 | BOOST_REQUIRE_NO_THROW(face.processEvents()); |
| 85 | |
| 86 | BOOST_CHECK_EQUAL(dataCount, 1); |
| 87 | BOOST_CHECK_EQUAL(timeoutCount, 0); |
| 88 | |
| 89 | face.expressInterest(Interest("/localhost/non-existing/data/should/not/exist/anywhere", 50), |
| 90 | ptr_lib::bind(&FacesFixture::onData, this), |
| 91 | ptr_lib::bind(&FacesFixture::onTimeout, this)); |
| 92 | |
| 93 | BOOST_REQUIRE_NO_THROW(face.processEvents()); |
| 94 | |
| 95 | BOOST_CHECK_EQUAL(dataCount, 1); |
| 96 | BOOST_CHECK_EQUAL(timeoutCount, 1); |
| 97 | } |
| 98 | |
| 99 | BOOST_FIXTURE_TEST_CASE (Tcp, FacesFixture) |
| 100 | { |
| 101 | Face face("localhost"); |
| 102 | |
| 103 | face.expressInterest(Interest("/%C1.M.S.localhost/%C1.M.SRV/ndnd/KEY", 1000), |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 104 | bind(&FacesFixture::onData, this), |
| 105 | bind(&FacesFixture::onTimeout, this)); |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 106 | |
| 107 | BOOST_REQUIRE_NO_THROW(face.processEvents()); |
| 108 | |
| 109 | BOOST_CHECK_EQUAL(dataCount, 1); |
| 110 | BOOST_CHECK_EQUAL(timeoutCount, 0); |
| 111 | |
| 112 | face.expressInterest(Interest("/localhost/non-existing/data/should/not/exist/anywhere", 50), |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 113 | bind(&FacesFixture::onData, this), |
| 114 | bind(&FacesFixture::onTimeout, this)); |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 115 | |
| 116 | BOOST_REQUIRE_NO_THROW(face.processEvents()); |
| 117 | |
| 118 | BOOST_CHECK_EQUAL(dataCount, 1); |
| 119 | BOOST_CHECK_EQUAL(timeoutCount, 1); |
| 120 | } |
| 121 | |
Alexander Afanasyev | bf9671d | 2014-02-11 13:44:13 -0800 | [diff] [blame] | 122 | |
| 123 | BOOST_FIXTURE_TEST_CASE (SetFilter, FacesFixture) |
| 124 | { |
| 125 | Face face; |
| 126 | Face face2(face.ioService()); |
| 127 | Scheduler scheduler(*face.ioService()); |
| 128 | scheduler.scheduleEvent(time::seconds(0.3), |
| 129 | bind(&FacesFixture::terminate, this, func_lib::ref(face))); |
| 130 | |
| 131 | regPrefixId = face.setInterestFilter("/Hello/World", |
| 132 | bind(&FacesFixture::onInterest, this, func_lib::ref(face)), |
| 133 | bind(&FacesFixture::onRegFailed, this)); |
| 134 | |
| 135 | scheduler.scheduleEvent(time::seconds(0.2), |
| 136 | bind(&FacesFixture::expressInterest, this, |
| 137 | func_lib::ref(face2), Name("/Hello/World/!"))); |
| 138 | |
| 139 | BOOST_REQUIRE_NO_THROW(face.processEvents()); |
| 140 | |
| 141 | BOOST_CHECK_EQUAL(regFailedCount, 0); |
| 142 | BOOST_CHECK_EQUAL(inInterestCount, 1); |
| 143 | BOOST_CHECK_EQUAL(timeoutCount, 1); |
| 144 | BOOST_CHECK_EQUAL(dataCount, 0); |
| 145 | } |
| 146 | |
Alexander Afanasyev | 20d2c58 | 2014-01-26 15:32:51 -0800 | [diff] [blame] | 147 | BOOST_AUTO_TEST_SUITE_END() |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 148 | |
| 149 | } // namespace ndn |