Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame^] | 3 | * Copyright (c) 2013-2015 Regents of the University of California, |
| 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. |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 12 | * |
| 13 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 14 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 15 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 18 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 19 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 20 | * |
| 21 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 22 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 23 | * <http://www.gnu.org/licenses/>. |
| 24 | * |
| 25 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 26 | */ |
| 27 | |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 28 | #include "util/notification-subscriber.hpp" |
| 29 | #include "util/notification-stream.hpp" |
| 30 | #include "simple-notification.hpp" |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 31 | #include "util/dummy-client-face.hpp" |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 32 | |
| 33 | #include "boost-test.hpp" |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 34 | #include "../unit-test-time-fixture.hpp" |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 35 | |
| 36 | namespace ndn { |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 37 | namespace util { |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 38 | namespace tests { |
| 39 | |
| 40 | BOOST_AUTO_TEST_SUITE(UtilNotificationSubscriber) |
| 41 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 42 | class EndToEndFixture : public ndn::tests::UnitTestTimeFixture |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 43 | { |
| 44 | public: |
| 45 | EndToEndFixture() |
| 46 | : streamPrefix("ndn:/NotificationSubscriberTest") |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 47 | , publisherFace(makeDummyClientFace(io)) |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 48 | , notificationStream(*publisherFace, streamPrefix, publisherKeyChain) |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 49 | , subscriberFace(makeDummyClientFace(io)) |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 50 | , subscriber(*subscriberFace, streamPrefix, time::seconds(1)) |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | /** \brief post one notification and deliver to subscriber |
| 55 | */ |
| 56 | void |
| 57 | deliverNotification(const std::string& msg) |
| 58 | { |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 59 | publisherFace->sentDatas.clear(); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 60 | SimpleNotification notification(msg); |
| 61 | notificationStream.postNotification(notification); |
Alexander Afanasyev | 370d260 | 2014-08-20 10:21:34 -0500 | [diff] [blame] | 62 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 63 | advanceClocks(time::milliseconds(1)); |
Alexander Afanasyev | 370d260 | 2014-08-20 10:21:34 -0500 | [diff] [blame] | 64 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 65 | BOOST_REQUIRE_EQUAL(publisherFace->sentDatas.size(), 1); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 66 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 67 | lastDeliveredSeqNo = publisherFace->sentDatas[0].getName().at(-1).toSequenceNumber(); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 68 | |
| 69 | lastNotification.setMessage(""); |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 70 | subscriberFace->receive(publisherFace->sentDatas[0]); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void |
| 74 | afterNotification(const SimpleNotification& notification) |
| 75 | { |
| 76 | lastNotification = notification; |
| 77 | } |
| 78 | |
| 79 | void |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 80 | afterTimeout() |
| 81 | { |
| 82 | hasTimeout = true; |
| 83 | } |
| 84 | |
| 85 | void |
| 86 | afterDecodeError(const Data& data) |
| 87 | { |
| 88 | lastDecodeErrorData = data; |
| 89 | } |
| 90 | |
| 91 | /** \return true if subscriberFace has an initial request (first sent Interest) |
| 92 | */ |
| 93 | bool |
| 94 | hasInitialRequest() const |
| 95 | { |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 96 | if (subscriberFace->sentInterests.empty()) |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 97 | return 0; |
| 98 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 99 | const Interest& interest = subscriberFace->sentInterests[0]; |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 100 | return interest.getName() == streamPrefix && |
| 101 | interest.getChildSelector() == 1 && |
| 102 | interest.getMustBeFresh() && |
| 103 | interest.getInterestLifetime() == subscriber.getInterestLifetime(); |
| 104 | } |
| 105 | |
| 106 | /** \return sequence number of the continuation request sent from subscriberFace |
| 107 | * or 0 if there's no such request as sole sent Interest |
| 108 | */ |
| 109 | uint64_t |
| 110 | getRequestSeqNo() const |
| 111 | { |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 112 | if (subscriberFace->sentInterests.size() != 1) |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 113 | return 0; |
| 114 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 115 | const Interest& interest = subscriberFace->sentInterests[0]; |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 116 | const Name& name = interest.getName(); |
| 117 | if (streamPrefix.isPrefixOf(name) && |
| 118 | name.size() == streamPrefix.size() + 1 && |
| 119 | interest.getInterestLifetime() == subscriber.getInterestLifetime()) |
| 120 | return name[-1].toSequenceNumber(); |
| 121 | else |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | protected: |
| 126 | Name streamPrefix; |
| 127 | shared_ptr<DummyClientFace> publisherFace; |
| 128 | ndn::KeyChain publisherKeyChain; |
| 129 | util::NotificationStream<SimpleNotification> notificationStream; |
| 130 | shared_ptr<DummyClientFace> subscriberFace; |
| 131 | util::NotificationSubscriber<SimpleNotification> subscriber; |
Junxiao Shi | 728873f | 2015-01-20 16:01:26 -0700 | [diff] [blame] | 132 | util::signal::Connection notificationConn; |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 133 | |
| 134 | uint64_t lastDeliveredSeqNo; |
| 135 | |
| 136 | SimpleNotification lastNotification; |
| 137 | bool hasTimeout; |
| 138 | Data lastDecodeErrorData; |
| 139 | }; |
| 140 | |
| 141 | BOOST_FIXTURE_TEST_CASE(EndToEnd, EndToEndFixture) |
| 142 | { |
| 143 | BOOST_REQUIRE_EQUAL(subscriber.isRunning(), false); |
| 144 | |
| 145 | // has no effect because onNotification has no handler |
| 146 | subscriber.start(); |
| 147 | BOOST_REQUIRE_EQUAL(subscriber.isRunning(), false); |
| 148 | |
Junxiao Shi | 728873f | 2015-01-20 16:01:26 -0700 | [diff] [blame] | 149 | notificationConn = subscriber.onNotification.connect( |
| 150 | bind(&EndToEndFixture::afterNotification, this, _1)); |
| 151 | subscriber.onTimeout.connect(bind(&EndToEndFixture::afterTimeout, this)); |
| 152 | subscriber.onDecodeError.connect(bind(&EndToEndFixture::afterDecodeError, this, _1)); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 153 | |
| 154 | // not received when subscriber is not running |
| 155 | this->deliverNotification("n1"); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 156 | advanceClocks(time::milliseconds(1)); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 157 | BOOST_CHECK(lastNotification.getMessage().empty()); |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 158 | BOOST_CHECK_EQUAL(subscriberFace->sentInterests.size(), 0); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 159 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 160 | subscriberFace->sentInterests.clear(); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 161 | subscriber.start(); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 162 | advanceClocks(time::milliseconds(1)); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 163 | BOOST_REQUIRE_EQUAL(subscriber.isRunning(), true); |
| 164 | BOOST_CHECK(this->hasInitialRequest()); |
| 165 | |
| 166 | // respond to initial request |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 167 | subscriberFace->sentInterests.clear(); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 168 | this->deliverNotification("n2"); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 169 | advanceClocks(time::milliseconds(1)); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 170 | BOOST_CHECK_EQUAL(lastNotification.getMessage(), "n2"); |
| 171 | BOOST_CHECK_EQUAL(this->getRequestSeqNo(), lastDeliveredSeqNo + 1); |
| 172 | |
| 173 | // respond to continuation request |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 174 | subscriberFace->sentInterests.clear(); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 175 | this->deliverNotification("n3"); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 176 | advanceClocks(time::milliseconds(1)); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 177 | BOOST_CHECK_EQUAL(lastNotification.getMessage(), "n3"); |
| 178 | BOOST_CHECK_EQUAL(this->getRequestSeqNo(), lastDeliveredSeqNo + 1); |
| 179 | |
| 180 | // timeout |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 181 | subscriberFace->sentInterests.clear(); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 182 | lastNotification.setMessage(""); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 183 | advanceClocks(subscriber.getInterestLifetime(), 2); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 184 | BOOST_CHECK(lastNotification.getMessage().empty()); |
| 185 | BOOST_CHECK_EQUAL(hasTimeout, true); |
| 186 | BOOST_CHECK(this->hasInitialRequest()); |
| 187 | |
| 188 | // decode error on sequence number |
| 189 | Name wrongName = streamPrefix; |
| 190 | wrongName.append("%07%07"); |
| 191 | Data wrongData(wrongName); |
| 192 | publisherKeyChain.sign(wrongData); |
| 193 | subscriberFace->receive(wrongData); |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 194 | subscriberFace->sentInterests.clear(); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 195 | lastNotification.setMessage(""); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 196 | advanceClocks(time::milliseconds(1)); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 197 | BOOST_CHECK(lastNotification.getMessage().empty()); |
| 198 | BOOST_CHECK_EQUAL(lastDecodeErrorData.getName(), wrongName); |
| 199 | BOOST_CHECK(this->hasInitialRequest()); |
| 200 | |
| 201 | // decode error in payload |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 202 | subscriberFace->sentInterests.clear(); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 203 | lastNotification.setMessage(""); |
| 204 | this->deliverNotification("\x07n4"); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 205 | advanceClocks(time::milliseconds(1)); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 206 | BOOST_CHECK(lastNotification.getMessage().empty()); |
| 207 | BOOST_CHECK(this->hasInitialRequest()); |
| 208 | |
| 209 | // stop if handlers are cleared |
Junxiao Shi | 728873f | 2015-01-20 16:01:26 -0700 | [diff] [blame] | 210 | notificationConn.disconnect(); |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 211 | subscriberFace->sentInterests.clear(); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 212 | this->deliverNotification("n5"); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 213 | advanceClocks(time::milliseconds(1)); |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 214 | BOOST_CHECK_EQUAL(subscriberFace->sentInterests.size(), 0); |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | BOOST_AUTO_TEST_SUITE_END() |
| 218 | |
| 219 | } // namespace tests |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 220 | } // namespace util |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 221 | } // namespace ndn |