Skip to content

Commit

Permalink
address some review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
yperbasis committed Sep 17, 2024
1 parent db06a0f commit 3da64d2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
13 changes: 7 additions & 6 deletions silkworm/infra/common/timer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "timer.hpp"

#include <array>
#include <string>
#include <thread>

Expand All @@ -25,14 +26,14 @@
namespace silkworm {

struct TimerTest {
const std::vector<uint32_t> intervals{100, 10, 1}; // milliseconds
static constexpr std::array<uint32_t, 3> kIntervals{100, 10, 1}; // milliseconds
boost::asio::io_context io_context;
};

TEST_CASE_METHOD(TimerTest, "Periodic timer", "[infra][common][timer]") {
constexpr static size_t kExpectedExpirations{2};
size_t expired_count{0}; // The lambda capture-list content *must* outlive the scheduler execution loop
for (const auto interval : intervals) {
for (const auto interval : kIntervals) {
Timer periodic_timer{io_context.get_executor(), interval, [&]() -> bool {
++expired_count;
// Stop the timer scheduler after multiple expirations
Expand Down Expand Up @@ -60,7 +61,7 @@ TEST_CASE_METHOD(TimerTest, "Periodic timer", "[infra][common][timer]") {

TEST_CASE_METHOD(TimerTest, "One shot timer", "[infra][common][timer]") {
bool timer_expired{false}; // The lambda capture-list content *must* outlive the scheduler execution loop
for (const auto interval : intervals) {
for (const auto interval : kIntervals) {
Timer one_shot_timer{io_context.get_executor(), interval, [&]() -> bool {
io_context.stop();
timer_expired = true;
Expand All @@ -86,7 +87,7 @@ TEST_CASE_METHOD(TimerTest, "One shot timer", "[infra][common][timer]") {

TEST_CASE_METHOD(TimerTest, "Cancellation before expiration", "[infra][common][timer]") {
bool timer_expired{false}; // The lambda capture-list content *must* outlive the scheduler execution loop
for (const auto interval : intervals) {
for (const auto interval : kIntervals) {
SECTION("Duration " + std::to_string(interval) + "ms") {
Timer async_timer{
io_context.get_executor(), interval, [&]() -> bool {
Expand All @@ -103,7 +104,7 @@ TEST_CASE_METHOD(TimerTest, "Cancellation before expiration", "[infra][common][t
}

TEST_CASE_METHOD(TimerTest, "Lifecycle race condition", "[infra][common][timer]") {
for (const auto interval : intervals) {
for (const auto interval : kIntervals) {
SECTION("Duration " + std::to_string(interval) + "ms") {
{
Timer async_timer{io_context.get_executor(), interval, []() -> bool { return true; }};
Expand All @@ -117,7 +118,7 @@ TEST_CASE_METHOD(TimerTest, "Lifecycle race condition", "[infra][common][timer]"
}

TEST_CASE_METHOD(TimerTest, "Explicit stop not necessary", "[infra][common][timer]") {
for (const auto interval : intervals) {
for (const auto interval : kIntervals) {
SECTION("Duration " + std::to_string(interval) + "ms: stopped") {
{
Timer async_timer{io_context.get_executor(), interval, []() -> bool { return true; }};
Expand Down
11 changes: 8 additions & 3 deletions silkworm/infra/grpc/client/dispatcher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,21 @@
namespace silkworm::detail {

template <typename Executor>
struct ExecutorDispatcher {
Executor executor;
class ExecutorDispatcher {
public:
// NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions)
ExecutorDispatcher(Executor executor) : executor_{std::move(executor)} {}

template <typename CompletionToken, typename... Args>
void dispatch(CompletionToken&& token, Args&&... args) {
boost::asio::dispatch(
boost::asio::bind_executor(executor,
boost::asio::bind_executor(executor_,
boost::asio::append(std::forward<CompletionToken>(token),
std::forward<Args>(args)...)));
}

private:
Executor executor_;
};

struct InlineDispatcher {
Expand Down
3 changes: 2 additions & 1 deletion silkworm/rpc/ethdb/kv/remote_database_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@

namespace silkworm::rpc::ethdb::kv {

struct RemoteDatabaseTest : db::test_util::KVTestBase {
class RemoteDatabaseTest : public db::test_util::KVTestBase {
public:
// RemoteDatabase holds the KV stub by std::unique_ptr, so we cannot rely on mock stub from base class
StrictMockKVStub* kv_stub = new StrictMockKVStub;
test::BackEndMock backend;
Expand Down

0 comments on commit 3da64d2

Please sign in to comment.