Browse Source

Fix/asio scheduler (#79)

* test - AsioScheduler called onTimer after being destructed

Signed-off-by: Alexey-N-Chernyshov <alexey.n.chernyshov@gmail.com>

* fix AsioScheduler

Signed-off-by: Alexey-N-Chernyshov <alexey.n.chernyshov@gmail.com>

* add comment

Signed-off-by: Alexey-N-Chernyshov <alexey.n.chernyshov@gmail.com>
pull/80/head
Alexey 4 years ago
committed by GitHub
parent
commit
5ff9050a33
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      src/protocol/common/asio/asio_scheduler.cpp
  2. 1
      test/libp2p/protocol/CMakeLists.txt
  3. 6
      test/libp2p/protocol/common/CMakeLists.txt
  4. 21
      test/libp2p/protocol/common/asio/CMakeLists.txt
  5. 27
      test/libp2p/protocol/common/asio/asio_scheduler_test.cpp

5
src/protocol/common/asio/asio_scheduler.cpp

@ -15,7 +15,10 @@ namespace libp2p::protocol {
interval_(config.period_msec),
timer_(io, boost::posix_time::milliseconds(interval_)),
started_(boost::posix_time::microsec_clock::local_time()),
timer_cb_([this](const boost::system::error_code &) { onTimer(); }),
timer_cb_([this](const boost::system::error_code &error) {
if (!error)
onTimer();
}),
immediate_cb_([this] { onImmediate(); }) {
assert(interval_ > 0 && interval_ <= 1000);
timer_.async_wait(timer_cb_);

1
test/libp2p/protocol/CMakeLists.txt

@ -3,6 +3,7 @@
# SPDX-License-Identifier: Apache-2.0
#
add_subdirectory(common)
add_subdirectory(kademlia)
add_subdirectory(gossip)

6
test/libp2p/protocol/common/CMakeLists.txt

@ -0,0 +1,6 @@
#
# Copyright Soramitsu Co., Ltd. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
#
add_subdirectory(asio)

21
test/libp2p/protocol/common/asio/CMakeLists.txt

@ -0,0 +1,21 @@
#
# Copyright Soramitsu Co., Ltd. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
#
addtest(asio_scheduler_test
asio_scheduler_test.cpp
)
target_link_libraries(asio_scheduler_test
asio_scheduler
p2p_basic_host
p2p_default_network
p2p_peer_repository
p2p_inmem_address_repository
p2p_inmem_key_repository
p2p_inmem_protocol_repository
p2p_literals
p2p_kad
asio_scheduler
)

27
test/libp2p/protocol/common/asio/asio_scheduler_test.cpp

@ -0,0 +1,27 @@
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
#include "libp2p/protocol/common/asio/asio_scheduler.hpp"
#include <gtest/gtest.h>
#include <libp2p/injector/host_injector.hpp>
using libp2p::protocol::Scheduler;
/**
* @given Constructs AsioScheduler and schedules on io context and then deletes
* AsioScheduler
* @when context is run
* @then scheduler called and can handle cancellation timer without segfault
*/
TEST(AsioScheduler, Construct) {
auto injector = libp2p::injector::makeHostInjector();
auto context = injector.create<std::shared_ptr<boost::asio::io_context>>();
std::shared_ptr<Scheduler> scheduler =
std::make_shared<libp2p::protocol::AsioScheduler>(
*context, libp2p::protocol::SchedulerConfig{});
scheduler.reset();
context->run();
}
Loading…
Cancel
Save