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