Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 84d62cb | 2017-07-12 16:15:18 +0000 | [diff] [blame] | 2 | /* |
Alexander Afanasyev | a6bd7da | 2019-04-25 17:26:11 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, Regents of the University of California, |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 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. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
Eric Newberry | 65caf20 | 2015-12-17 15:08:16 -0700 | [diff] [blame] | 26 | #include "transport-test-common.hpp" |
| 27 | |
Eric Newberry | f68f378 | 2015-12-11 00:31:32 -0700 | [diff] [blame] | 28 | #include "tcp-transport-fixture.hpp" |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 29 | |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 30 | #include <boost/mpl/vector.hpp> |
| 31 | |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 32 | namespace nfd { |
| 33 | namespace face { |
| 34 | namespace tests { |
| 35 | |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 36 | BOOST_AUTO_TEST_SUITE(Face) |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 37 | BOOST_FIXTURE_TEST_SUITE(TestTcpTransport, IpTransportFixture<TcpTransportFixture>) |
Eric Newberry | 8717e87 | 2015-11-23 12:41:50 -0700 | [diff] [blame] | 38 | |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 39 | using TcpTransportFixtures = boost::mpl::vector< |
| 40 | GENERATE_IP_TRANSPORT_FIXTURE_INSTANTIATIONS(TcpTransportFixture) |
| 41 | >; |
| 42 | |
| 43 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(StaticProperties, T, TcpTransportFixtures, T) |
Eric Newberry | 8717e87 | 2015-11-23 12:41:50 -0700 | [diff] [blame] | 44 | { |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 45 | TRANSPORT_TEST_INIT(); |
Eric Newberry | 8717e87 | 2015-11-23 12:41:50 -0700 | [diff] [blame] | 46 | |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 47 | checkStaticPropertiesInitialized(*this->transport); |
Eric Newberry | 8717e87 | 2015-11-23 12:41:50 -0700 | [diff] [blame] | 48 | |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 49 | BOOST_CHECK_EQUAL(this->transport->getLocalUri(), FaceUri(tcp::endpoint(this->address, this->localEp.port()))); |
| 50 | BOOST_CHECK_EQUAL(this->transport->getRemoteUri(), FaceUri(tcp::endpoint(this->address, 7070))); |
| 51 | BOOST_CHECK_EQUAL(this->transport->getScope(), |
| 52 | this->addressScope == AddressScope::Loopback ? ndn::nfd::FACE_SCOPE_LOCAL |
| 53 | : ndn::nfd::FACE_SCOPE_NON_LOCAL); |
| 54 | BOOST_CHECK_EQUAL(this->transport->getPersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT); |
| 55 | BOOST_CHECK_EQUAL(this->transport->getLinkType(), ndn::nfd::LINK_TYPE_POINT_TO_POINT); |
| 56 | BOOST_CHECK_EQUAL(this->transport->getMtu(), MTU_UNLIMITED); |
Eric Newberry | b49313d | 2017-12-24 20:22:27 -0700 | [diff] [blame] | 57 | BOOST_CHECK_EQUAL(this->transport->getSendQueueCapacity(), QUEUE_UNSUPPORTED); |
Eric Newberry | 8717e87 | 2015-11-23 12:41:50 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Davide Pesavento | 3206565 | 2017-01-15 01:52:21 -0500 | [diff] [blame] | 60 | BOOST_AUTO_TEST_CASE(PersistencyChange) |
| 61 | { |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 62 | TRANSPORT_TEST_INIT(); |
Davide Pesavento | 3206565 | 2017-01-15 01:52:21 -0500 | [diff] [blame] | 63 | |
| 64 | BOOST_CHECK_EQUAL(transport->canChangePersistencyTo(ndn::nfd::FACE_PERSISTENCY_ON_DEMAND), true); |
| 65 | BOOST_CHECK_EQUAL(transport->canChangePersistencyTo(ndn::nfd::FACE_PERSISTENCY_PERSISTENT), true); |
| 66 | BOOST_CHECK_EQUAL(transport->canChangePersistencyTo(ndn::nfd::FACE_PERSISTENCY_PERMANENT), true); |
| 67 | } |
| 68 | |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 69 | BOOST_AUTO_TEST_CASE(ChangePersistencyFromPermanentWhenDown) |
| 70 | { |
| 71 | // when persistency is changed out of permanent while transport is DOWN, |
| 72 | // the transport immediately goes into FAILED state |
| 73 | |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 74 | TRANSPORT_TEST_INIT(ndn::nfd::FACE_PERSISTENCY_PERMANENT); |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 75 | |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 76 | transport->afterStateChange.connectSingleShot( |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 77 | [this] (auto oldState, auto newState) { |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 78 | BOOST_CHECK_EQUAL(oldState, TransportState::UP); |
| 79 | BOOST_CHECK_EQUAL(newState, TransportState::DOWN); |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 80 | this->limitedIo.afterOp(); |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 81 | }); |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 82 | remoteSocket.close(); |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 83 | BOOST_REQUIRE_EQUAL(limitedIo.run(1, 1_s), LimitedIo::EXCEED_OPS); |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 84 | |
| 85 | bool didStateChange = false; |
| 86 | transport->afterStateChange.connectSingleShot( |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 87 | [&didStateChange] (auto oldState, auto newState) { |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 88 | didStateChange = true; |
| 89 | BOOST_CHECK_EQUAL(oldState, TransportState::DOWN); |
| 90 | BOOST_CHECK_EQUAL(newState, TransportState::FAILED); |
| 91 | }); |
| 92 | transport->setPersistency(ndn::nfd::FACE_PERSISTENCY_PERSISTENT); |
| 93 | BOOST_CHECK(didStateChange); |
| 94 | } |
| 95 | |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 96 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(PermanentReconnect, T, TcpTransportFixtures, T) |
| 97 | { |
| 98 | TRANSPORT_TEST_INIT(ndn::nfd::FACE_PERSISTENCY_PERMANENT); |
| 99 | |
| 100 | this->transport->afterStateChange.connectSingleShot( |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 101 | [this] (auto oldState, auto newState) { |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 102 | BOOST_CHECK_EQUAL(oldState, TransportState::UP); |
| 103 | BOOST_CHECK_EQUAL(newState, TransportState::DOWN); |
| 104 | this->limitedIo.afterOp(); |
| 105 | }); |
| 106 | this->remoteSocket.close(); |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 107 | BOOST_REQUIRE_EQUAL(this->limitedIo.run(1, 1_s), LimitedIo::EXCEED_OPS); |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 108 | |
| 109 | this->transport->afterStateChange.connectSingleShot( |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 110 | [this] (auto oldState, auto newState) { |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 111 | BOOST_CHECK_EQUAL(oldState, TransportState::DOWN); |
| 112 | BOOST_CHECK_EQUAL(newState, TransportState::UP); |
| 113 | this->limitedIo.afterOp(); |
| 114 | }); |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 115 | BOOST_REQUIRE_EQUAL(this->limitedIo.run(1, 1_s), LimitedIo::EXCEED_OPS); |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 116 | } |
| 117 | |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 118 | class PermanentTcpTransportReconnectObserver : public TcpTransport |
| 119 | { |
| 120 | public: |
| 121 | PermanentTcpTransportReconnectObserver(protocol::socket&& socket, LimitedIo& io) |
Alexander Afanasyev | ded1742 | 2018-04-03 19:00:23 -0400 | [diff] [blame] | 122 | : TcpTransport(std::move(socket), ndn::nfd::FACE_PERSISTENCY_PERMANENT, ndn::nfd::FACE_SCOPE_LOCAL) |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 123 | , m_io(io) |
| 124 | { |
| 125 | } |
| 126 | |
| 127 | protected: |
| 128 | void |
| 129 | reconnect() final |
| 130 | { |
| 131 | TcpTransport::reconnect(); |
| 132 | m_io.afterOp(); |
| 133 | } |
| 134 | |
| 135 | void |
| 136 | handleReconnect(const boost::system::error_code& error) final |
| 137 | { |
| 138 | TcpTransport::handleReconnect(error); |
Davide Pesavento | bc79bd6 | 2017-10-28 01:52:16 -0400 | [diff] [blame] | 139 | |
| 140 | // don't count this invocation as an "op" if the reconnection attempt failed, |
| 141 | // because in that case we will instead count the handleReconnectTimeout() |
| 142 | // that will eventually be called as well |
| 143 | if (getState() == TransportState::UP) |
| 144 | m_io.afterOp(); |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void |
| 148 | handleReconnectTimeout() final |
| 149 | { |
| 150 | TcpTransport::handleReconnectTimeout(); |
| 151 | m_io.afterOp(); |
| 152 | } |
| 153 | |
| 154 | private: |
| 155 | LimitedIo& m_io; |
| 156 | }; |
| 157 | |
| 158 | static double |
Davide Pesavento | b3a23ca | 2019-05-04 20:40:21 -0400 | [diff] [blame] | 159 | asFloatMilliseconds(time::nanoseconds t) |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 160 | { |
| 161 | return static_cast<double>(t.count()) / 1000000.0; |
| 162 | } |
| 163 | |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 164 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(PermanentReconnectWithExponentialBackoff, T, TcpTransportFixtures, T) |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 165 | { |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 166 | TRANSPORT_TEST_CHECK_PRECONDITIONS(); |
| 167 | // do not initialize |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 168 | |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 169 | tcp::endpoint remoteEp(this->address, 7070); |
| 170 | this->startAccept(remoteEp); |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 171 | |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 172 | tcp::socket sock(this->g_io); |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 173 | sock.async_connect(remoteEp, [this] (const boost::system::error_code& error) { |
| 174 | BOOST_REQUIRE_EQUAL(error, boost::system::errc::success); |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 175 | this->limitedIo.afterOp(); |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 176 | }); |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 177 | BOOST_REQUIRE_EQUAL(this->limitedIo.run(2, 1_s), LimitedIo::EXCEED_OPS); |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 178 | |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 179 | auto transportObserver = |
Alexander Afanasyev | a6bd7da | 2019-04-25 17:26:11 -0400 | [diff] [blame] | 180 | make_unique<PermanentTcpTransportReconnectObserver>(std::move(sock), this->limitedIo); |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 181 | BOOST_REQUIRE_EQUAL(transportObserver->getState(), TransportState::UP); |
| 182 | |
| 183 | // break the TCP connection |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 184 | this->stopAccept(); |
| 185 | this->remoteSocket.close(); |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 186 | |
| 187 | // measure retry intervals |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 188 | auto retryTime1 = time::steady_clock::now(); |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 189 | |
Davide Pesavento | bc79bd6 | 2017-10-28 01:52:16 -0400 | [diff] [blame] | 190 | auto expectedWait1 = TcpTransport::s_initialReconnectWait; |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 191 | BOOST_REQUIRE_EQUAL(this->limitedIo.run(2, expectedWait1 + 1_s), // add some slack |
Davide Pesavento | bc79bd6 | 2017-10-28 01:52:16 -0400 | [diff] [blame] | 192 | LimitedIo::EXCEED_OPS); |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 193 | auto retryTime2 = time::steady_clock::now(); |
| 194 | BOOST_CHECK_EQUAL(transportObserver->getState(), TransportState::DOWN); |
| 195 | |
Davide Pesavento | bc79bd6 | 2017-10-28 01:52:16 -0400 | [diff] [blame] | 196 | auto expectedWait2 = time::duration_cast<time::nanoseconds>(expectedWait1 * |
| 197 | TcpTransport::s_reconnectWaitMultiplier); |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 198 | BOOST_REQUIRE_EQUAL(this->limitedIo.run(2, expectedWait2 + 1_s), // add some slack |
Davide Pesavento | bc79bd6 | 2017-10-28 01:52:16 -0400 | [diff] [blame] | 199 | LimitedIo::EXCEED_OPS); |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 200 | auto retryTime3 = time::steady_clock::now(); |
| 201 | BOOST_CHECK_EQUAL(transportObserver->getState(), TransportState::DOWN); |
| 202 | |
| 203 | // check that the backoff algorithm works |
| 204 | BOOST_CHECK_CLOSE(asFloatMilliseconds(retryTime2 - retryTime1), |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 205 | asFloatMilliseconds(expectedWait1), 20.0); // 200ms tolerance |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 206 | BOOST_CHECK_CLOSE(asFloatMilliseconds(retryTime3 - retryTime2), |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 207 | asFloatMilliseconds(expectedWait2), 10.0); // 200ms tolerance |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 208 | |
| 209 | // reestablish the TCP connection |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 210 | this->startAccept(remoteEp); |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 211 | |
Davide Pesavento | bc79bd6 | 2017-10-28 01:52:16 -0400 | [diff] [blame] | 212 | auto expectedWait3 = time::duration_cast<time::nanoseconds>(expectedWait2 * |
| 213 | TcpTransport::s_reconnectWaitMultiplier); |
| 214 | BOOST_REQUIRE_EQUAL(this->limitedIo.run(3, // reconnect, handleReconnect, async_accept |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 215 | expectedWait3 + 1_s), LimitedIo::EXCEED_OPS); |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 216 | BOOST_CHECK_EQUAL(transportObserver->getState(), TransportState::UP); |
| 217 | |
| 218 | // break the TCP connection again |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 219 | this->stopAccept(); |
| 220 | this->remoteSocket.close(); |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 221 | |
| 222 | // measure retry intervals |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 223 | auto retryTime4 = time::steady_clock::now(); |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 224 | BOOST_REQUIRE_EQUAL(this->limitedIo.run(2, expectedWait1 + 1_s), // add some slack |
Davide Pesavento | bc79bd6 | 2017-10-28 01:52:16 -0400 | [diff] [blame] | 225 | LimitedIo::EXCEED_OPS); |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 226 | auto retryTime5 = time::steady_clock::now(); |
| 227 | BOOST_CHECK_EQUAL(transportObserver->getState(), TransportState::DOWN); |
| 228 | |
| 229 | // check that the timeout restarts from the initial value after a successful reconnection |
| 230 | BOOST_CHECK_CLOSE(asFloatMilliseconds(retryTime5 - retryTime4), |
Davide Pesavento | 0033578 | 2018-02-10 22:31:33 -0500 | [diff] [blame] | 231 | asFloatMilliseconds(expectedWait1), 20.0); // 200ms tolerance |
Weiwei Liu | dcdf621 | 2016-08-31 14:34:22 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Yukai Tu | 16aabbc | 2015-10-06 05:08:42 -0700 | [diff] [blame] | 234 | BOOST_AUTO_TEST_SUITE_END() // TestTcpTransport |
| 235 | BOOST_AUTO_TEST_SUITE_END() // Face |
| 236 | |
| 237 | } // namespace tests |
| 238 | } // namespace face |
| 239 | } // namespace nfd |