Reduce usage of std::bind()
C++14 lambdas are easier to read, easier to debug,
and can usually be better optimized by the compiler.
Change-Id: I294f275904f91942a8de946fe63e77078a7608a6
diff --git a/tests/daemon/face/face.t.cpp b/tests/daemon/face/face.t.cpp
index b592a24..641088f 100644
--- a/tests/daemon/face/face.t.cpp
+++ b/tests/daemon/face/face.t.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2014-2020, Regents of the University of California,
+ * Copyright (c) 2014-2021, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -101,9 +101,9 @@
size_t nReceivedInterests = 0;
size_t nReceivedData = 0;
size_t nReceivedNacks = 0;
- face1->afterReceiveInterest.connect(bind([&nReceivedInterests] { ++nReceivedInterests; }));
- face1->afterReceiveData.connect(bind([&nReceivedData] { ++nReceivedData; }));
- face1->afterReceiveNack.connect(bind([&nReceivedNacks] { ++nReceivedNacks; }));
+ face1->afterReceiveInterest.connect([&] (auto&&...) { ++nReceivedInterests; });
+ face1->afterReceiveData.connect([&] (auto&&...) { ++nReceivedData; });
+ face1->afterReceiveNack.connect([&] (auto&&...) { ++nReceivedNacks; });
for (size_t i = 0; i < nInInterests; ++i) {
face1->receiveInterest(*makeInterest("/JSQdqward4"), 0);
diff --git a/tests/daemon/face/internal-face.t.cpp b/tests/daemon/face/internal-face.t.cpp
index db72bcc..1f2b82a 100644
--- a/tests/daemon/face/internal-face.t.cpp
+++ b/tests/daemon/face/internal-face.t.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2014-2020, Regents of the University of California,
+ * Copyright (c) 2014-2021, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -87,9 +87,9 @@
bool hasTimeout = false;
clientFace->expressInterest(*interest,
- bind([] { BOOST_ERROR("unexpected Data"); }),
- bind([] { BOOST_ERROR("unexpected Nack"); }),
- bind([&hasTimeout] { hasTimeout = true; }));
+ [] (auto&&...) { BOOST_ERROR("unexpected Data"); },
+ [] (auto&&...) { BOOST_ERROR("unexpected Nack"); },
+ [&] (auto&&...) { hasTimeout = true; });
this->advanceClocks(1_ms, 10);
BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
@@ -106,12 +106,12 @@
bool hasReceivedData = false;
clientFace->expressInterest(*interest,
- [&hasReceivedData] (const Interest&, const Data& data) {
+ [&] (const Interest&, const Data& data) {
hasReceivedData = true;
BOOST_CHECK_EQUAL(data.getName(), "/PQstEJGdL/aI7oCrDXNX");
},
- bind([] { BOOST_ERROR("unexpected Nack"); }),
- bind([] { BOOST_ERROR("unexpected timeout"); }));
+ [] (auto&&...) { BOOST_ERROR("unexpected Nack"); },
+ [] (auto&&...) { BOOST_ERROR("unexpected timeout"); });
this->advanceClocks(1_ms, 10);
BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
@@ -129,12 +129,12 @@
bool hasReceivedNack = false;
clientFace->expressInterest(*interest,
- bind([] { BOOST_ERROR("unexpected Data"); }),
- [&hasReceivedNack] (const Interest&, const lp::Nack& nack) {
+ [] (auto&&...) { BOOST_ERROR("unexpected Data"); },
+ [&] (const Interest&, const lp::Nack& nack) {
hasReceivedNack = true;
BOOST_CHECK_EQUAL(nack.getReason(), lp::NackReason::NO_ROUTE);
},
- bind([] { BOOST_ERROR("unexpected timeout"); }));
+ [] (auto&&...) { BOOST_ERROR("unexpected timeout"); });
this->advanceClocks(1_ms, 10);
BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
@@ -197,9 +197,9 @@
bool hasTimeout = false;
clientFace->expressInterest(*interest,
- bind([] { BOOST_ERROR("unexpected Data"); }),
- bind([] { BOOST_ERROR("unexpected Nack"); }),
- bind([&hasTimeout] { hasTimeout = true; }));
+ [] (auto&&...) { BOOST_ERROR("unexpected Data"); },
+ [] (auto&&...) { BOOST_ERROR("unexpected Nack"); },
+ [&] (auto&&...) { hasTimeout = true; });
BOOST_CHECK_NO_THROW(this->advanceClocks(1_ms, 200));
BOOST_CHECK_EQUAL(receivedInterests.size(), 0);
diff --git a/tests/daemon/face/tcp-factory.t.cpp b/tests/daemon/face/tcp-factory.t.cpp
index 95e6c2b..a6335f9 100644
--- a/tests/daemon/face/tcp-factory.t.cpp
+++ b/tests/daemon/face/tcp-factory.t.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2014-2020, Regents of the University of California,
+ * Copyright (c) 2014-2021, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -458,10 +458,10 @@
{
createChannel("0.0.0.0", "20070");
factory.createFace({FaceUri("tcp4://192.0.2.1:20070"), {}, {}},
- bind(&CreateFaceTimeoutFixture::onFaceCreated, this, _1),
- bind(&CreateFaceTimeoutFixture::onConnectFailed, this, _2));
+ std::bind(&CreateFaceTimeoutFixture::onFaceCreated, this, _1),
+ std::bind(&CreateFaceTimeoutFixture::onConnectFailed, this, _2));
- BOOST_REQUIRE_EQUAL(limitedIo.run(1, 10_s), LimitedIo::EXCEED_OPS);
+ BOOST_CHECK_EQUAL(limitedIo.run(1, 10_s), LimitedIo::EXCEED_OPS);
BOOST_CHECK(face == nullptr);
}
diff --git a/tests/daemon/face/websocket-channel-fixture.hpp b/tests/daemon/face/websocket-channel-fixture.hpp
index 69b2e9b..e6e1e9f 100644
--- a/tests/daemon/face/websocket-channel-fixture.hpp
+++ b/tests/daemon/face/websocket-channel-fixture.hpp
@@ -38,7 +38,8 @@
{
protected:
shared_ptr<WebSocketChannel>
- makeChannel(const boost::asio::ip::address& addr, uint16_t port = 0, optional<size_t> mtu = nullopt) final
+ makeChannel(const boost::asio::ip::address& addr, uint16_t port = 0,
+ optional<size_t> mtu = nullopt) final
{
if (port == 0)
port = getNextPort();
@@ -55,7 +56,7 @@
listenerChannel = makeChannel(addr, 20030);
listenerChannel->setPingInterval(pingInterval);
listenerChannel->setPongTimeout(pongTimeout);
- listenerChannel->listen(bind(&WebSocketChannelFixture::listenerOnFaceCreated, this, _1));
+ listenerChannel->listen(std::bind(&WebSocketChannelFixture::listenerOnFaceCreated, this, _1));
}
void
@@ -65,9 +66,9 @@
client.clear_error_channels(websocketpp::log::elevel::all);
client.init_asio(&g_io);
- client.set_open_handler(bind(&WebSocketChannelFixture::clientHandleOpen, this, _1));
- client.set_message_handler(bind(&WebSocketChannelFixture::clientHandleMessage, this, _1, _2));
- client.set_ping_handler(bind(&WebSocketChannelFixture::clientHandlePing, this, _1, _2));
+ client.set_open_handler(std::bind(&WebSocketChannelFixture::clientHandleOpen, this, _1));
+ client.set_message_handler(std::bind(&WebSocketChannelFixture::clientHandleMessage, this, _1, _2));
+ client.set_ping_handler(std::bind(&WebSocketChannelFixture::clientHandlePing, this, _1, _2));
websocketpp::lib::error_code ec;
auto con = client.get_connection(FaceUri(listenerEp, "ws").toString(), ec);
@@ -100,20 +101,16 @@
listenerOnFaceCreated(const shared_ptr<Face>& newFace)
{
BOOST_REQUIRE(newFace != nullptr);
- newFace->afterReceiveInterest.connect(bind(&WebSocketChannelFixture::faceAfterReceiveInterest, this, _1));
+ newFace->afterReceiveInterest.connect([this] (const auto& interest, const auto&) {
+ faceReceivedInterests.push_back(interest);
+ limitedIo.afterOp();
+ });
connectFaceClosedSignal(*newFace, [this] { limitedIo.afterOp(); });
listenerFaces.push_back(newFace);
limitedIo.afterOp();
}
void
- faceAfterReceiveInterest(const Interest& interest)
- {
- faceReceivedInterests.push_back(interest);
- limitedIo.afterOp();
- }
-
- void
clientHandleOpen(websocketpp::connection_hdl hdl)
{
clientHandle = hdl;
@@ -131,8 +128,8 @@
clientHandlePing(websocketpp::connection_hdl, std::string)
{
auto now = time::steady_clock::now();
- if (m_prevPingRecvTime != time::steady_clock::TimePoint()) {
- measuredPingInterval = now - m_prevPingRecvTime;
+ if (m_prevPingRecvTime != time::steady_clock::time_point()) {
+ measuredPingIntervals.push_back(now - m_prevPingRecvTime);
}
m_prevPingRecvTime = now;
@@ -147,13 +144,13 @@
websocketpp::connection_hdl clientHandle;
std::vector<std::string> clientReceivedMessages;
- time::steady_clock::Duration measuredPingInterval;
+ std::vector<time::nanoseconds> measuredPingIntervals;
// set clientShouldPong to false to disable the pong response,
// which will eventually cause a timeout in listenerChannel
bool clientShouldPong = true;
private:
- time::steady_clock::TimePoint m_prevPingRecvTime;
+ time::steady_clock::time_point m_prevPingRecvTime;
};
} // namespace tests
diff --git a/tests/daemon/face/websocket-channel.t.cpp b/tests/daemon/face/websocket-channel.t.cpp
index d9fe929..5772daa 100644
--- a/tests/daemon/face/websocket-channel.t.cpp
+++ b/tests/daemon/face/websocket-channel.t.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2014-2020, Regents of the University of California,
+ * Copyright (c) 2014-2021, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -175,13 +175,17 @@
{
auto address = getTestIp(F::value, AddressScope::Loopback);
SKIP_IF_IP_UNAVAILABLE(address);
- const auto pingInterval = 1200_ms;
+ const auto pingInterval = 1500_ms;
this->initialize(address, pingInterval);
- BOOST_CHECK_EQUAL(limitedIo.run(2, // clientHandlePing
- pingInterval * 3), LimitedIo::EXCEED_OPS);
- BOOST_CHECK_LE(measuredPingInterval, pingInterval * 1.1);
- BOOST_CHECK_GE(measuredPingInterval, pingInterval * 0.9);
+ BOOST_CHECK_EQUAL(limitedIo.run(5, // clientHandlePing
+ 5 * pingInterval + 1_s), LimitedIo::EXCEED_OPS);
+ BOOST_CHECK_EQUAL(measuredPingIntervals.size(), 4);
+
+ auto avgPingInterval = std::accumulate(measuredPingIntervals.begin(), measuredPingIntervals.end(), 0_ns);
+ avgPingInterval /= measuredPingIntervals.size();
+ BOOST_CHECK_LE(avgPingInterval, pingInterval * 1.1);
+ BOOST_CHECK_GE(avgPingInterval, pingInterval * 0.9);
}
BOOST_AUTO_TEST_CASE_TEMPLATE(SetPongTimeOut, F, AddressFamilies)
diff --git a/tests/daemon/face/websocket-transport-fixture.hpp b/tests/daemon/face/websocket-transport-fixture.hpp
index 32934ab..b1700ed 100644
--- a/tests/daemon/face/websocket-transport-fixture.hpp
+++ b/tests/daemon/face/websocket-transport-fixture.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2014-2019, Regents of the University of California,
+ * Copyright (c) 2014-2021, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
@@ -62,11 +62,11 @@
server.clear_error_channels(websocketpp::log::elevel::all);
server.init_asio(&g_io);
- server.set_open_handler(bind(&WebSocketTransportFixture::serverHandleOpen, this, _1));
- server.set_close_handler(bind(&WebSocketTransportFixture::serverHandleClose, this));
- server.set_message_handler(bind(&WebSocketTransportFixture::serverHandleMessage, this, _2));
- server.set_pong_handler(bind(&WebSocketTransportFixture::serverHandlePong, this));
- server.set_pong_timeout_handler(bind(&WebSocketTransportFixture::serverHandlePongTimeout, this));
+ server.set_open_handler(std::bind(&WebSocketTransportFixture::serverHandleOpen, this, _1));
+ server.set_close_handler(std::bind(&WebSocketTransportFixture::serverHandleClose, this));
+ server.set_message_handler(std::bind(&WebSocketTransportFixture::serverHandleMessage, this, _2));
+ server.set_pong_handler(std::bind(&WebSocketTransportFixture::serverHandlePong, this));
+ server.set_pong_timeout_handler(std::bind(&WebSocketTransportFixture::serverHandlePongTimeout, this));
server.set_pong_timeout(pongTimeout.count());
server.set_reuse_addr(true);
@@ -84,9 +84,9 @@
client.clear_error_channels(websocketpp::log::elevel::all);
client.init_asio(&g_io);
- client.set_open_handler(bind(&WebSocketTransportFixture::clientHandleOpen, this, _1));
- client.set_message_handler(bind(&WebSocketTransportFixture::clientHandleMessage, this, _2));
- client.set_ping_handler(bind(&WebSocketTransportFixture::clientHandlePing, this));
+ client.set_open_handler(std::bind(&WebSocketTransportFixture::clientHandleOpen, this, _1));
+ client.set_message_handler(std::bind(&WebSocketTransportFixture::clientHandleMessage, this, _2));
+ client.set_ping_handler(std::bind(&WebSocketTransportFixture::clientHandlePing, this));
websocketpp::lib::error_code ec;
auto con = client.get_connection(uri, ec);