Skip to content

Commit 5ff9050

Browse files
Fix/asio scheduler (#79)
* test - AsioScheduler called onTimer after being destructed Signed-off-by: Alexey-N-Chernyshov <[email protected]> * fix AsioScheduler Signed-off-by: Alexey-N-Chernyshov <[email protected]> * add comment Signed-off-by: Alexey-N-Chernyshov <[email protected]>
1 parent 8b52253 commit 5ff9050

File tree

5 files changed

+59
-1
lines changed

5 files changed

+59
-1
lines changed

src/protocol/common/asio/asio_scheduler.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ namespace libp2p::protocol {
1515
interval_(config.period_msec),
1616
timer_(io, boost::posix_time::milliseconds(interval_)),
1717
started_(boost::posix_time::microsec_clock::local_time()),
18-
timer_cb_([this](const boost::system::error_code &) { onTimer(); }),
18+
timer_cb_([this](const boost::system::error_code &error) {
19+
if (!error)
20+
onTimer();
21+
}),
1922
immediate_cb_([this] { onImmediate(); }) {
2023
assert(interval_ > 0 && interval_ <= 1000);
2124
timer_.async_wait(timer_cb_);

test/libp2p/protocol/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# SPDX-License-Identifier: Apache-2.0
44
#
55

6+
add_subdirectory(common)
67
add_subdirectory(kademlia)
78
add_subdirectory(gossip)
89

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#
2+
# Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
5+
6+
add_subdirectory(asio)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#
2+
# Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
5+
6+
addtest(asio_scheduler_test
7+
asio_scheduler_test.cpp
8+
)
9+
target_link_libraries(asio_scheduler_test
10+
asio_scheduler
11+
12+
p2p_basic_host
13+
p2p_default_network
14+
p2p_peer_repository
15+
p2p_inmem_address_repository
16+
p2p_inmem_key_repository
17+
p2p_inmem_protocol_repository
18+
p2p_literals
19+
p2p_kad
20+
asio_scheduler
21+
)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include "libp2p/protocol/common/asio/asio_scheduler.hpp"
7+
8+
#include <gtest/gtest.h>
9+
#include <libp2p/injector/host_injector.hpp>
10+
11+
using libp2p::protocol::Scheduler;
12+
13+
/**
14+
* @given Constructs AsioScheduler and schedules on io context and then deletes
15+
* AsioScheduler
16+
* @when context is run
17+
* @then scheduler called and can handle cancellation timer without segfault
18+
*/
19+
TEST(AsioScheduler, Construct) {
20+
auto injector = libp2p::injector::makeHostInjector();
21+
auto context = injector.create<std::shared_ptr<boost::asio::io_context>>();
22+
std::shared_ptr<Scheduler> scheduler =
23+
std::make_shared<libp2p::protocol::AsioScheduler>(
24+
*context, libp2p::protocol::SchedulerConfig{});
25+
scheduler.reset();
26+
context->run();
27+
}

0 commit comments

Comments
 (0)