Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add const version of RetryScheduler::execute #119

Merged
merged 4 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/common/include/display_device/retry_scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ namespace display_device {
exec_fn(std::declval<T &>());
};

/**
* @brief Check if the function signature matches the acceptable signature for RetryScheduler::execute (const)
* without a stop token.
*/
template <class T, class FunctionT>
concept ExecuteWithoutStopTokenConst = requires(FunctionT exec_fn) {
exec_fn(std::declval<const T &>());
};

/**
* @brief Check if the function signature matches the acceptable signature for RetryScheduler::execute
* with a stop token.
Expand All @@ -81,11 +90,26 @@ namespace display_device {
exec_fn(std::declval<T &>(), std::declval<SchedulerStopToken &>());
};

/**
* @brief Check if the function signature matches the acceptable signature for RetryScheduler::execute (const)
* with a stop token.
*/
template <class T, class FunctionT>
concept ExecuteWithStopTokenConst = requires(FunctionT exec_fn) {
exec_fn(std::declval<const T &>(), std::declval<const SchedulerStopToken &>());
};

/**
* @brief Check if the function signature matches the acceptable signature for RetryScheduler::execute.
*/
template <class T, class FunctionT>
concept ExecuteCallbackLike = ExecuteWithoutStopToken<T, FunctionT> || ExecuteWithStopToken<T, FunctionT>;

/**
* @brief Check if the function signature matches the acceptable signature for RetryScheduler::execute (const).
*/
template <class T, class FunctionT>
concept ExecuteCallbackLikeConst = ExecuteWithoutStopTokenConst<T, FunctionT> || ExecuteWithStopTokenConst<T, FunctionT>;
} // namespace detail

/**
Expand Down Expand Up @@ -280,6 +304,16 @@ namespace display_device {
}
}

/**
* @brief A const variant of the `execute` method. See non-const variant for details.
*/
template <class FunctionT>
requires detail::ExecuteCallbackLikeConst<T, FunctionT>
auto
execute(FunctionT exec_fn) const {
return const_cast<RetryScheduler *>(this)->execute(std::move(exec_fn));
}

/**
* @brief Check whether anything is scheduled for execution.
* @return True if something is scheduled, false otherwise.
Expand Down
78 changes: 76 additions & 2 deletions tests/unit/general/test_retry_scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,26 @@ namespace {
std::vector<int> m_durations;
};

template <class FunctionT>
constexpr bool
isValidNonConstCallback(FunctionT) {
using namespace display_device::detail;
if constexpr (ExecuteCallbackLike<TestIface, FunctionT>) {
return true;
}
return false;
}

template <class FunctionT>
constexpr bool
isValidConstCallback(FunctionT) {
using namespace display_device::detail;
if constexpr (ExecuteCallbackLikeConst<TestIface, FunctionT>) {
return true;
}
return false;
}

// Test fixture(s) for this file
class RetrySchedulerTest: public BaseTest {
public:
Expand Down Expand Up @@ -315,8 +335,14 @@ TEST_F_S(Schedule, ExceptionThrown, DuringScheduledCall) {
m_impl.stop();
}

TEST_F_S(Execute, NullptrCallbackProvided) {
EXPECT_THAT([&]() { m_impl.execute(std::function<void(TestIface &)> {}); },
TEST_F_S(Execute, NonConst, NullptrCallbackProvided) {
EXPECT_THAT([this]() { auto &non_const_impl { m_impl }; non_const_impl.execute(std::function<void(TestIface &)> {}); },
ThrowsMessage<std::logic_error>(HasSubstr("Empty callback function provided in RetryScheduler::execute!")));
}

TEST_F_S(Execute, Const, NullptrCallbackProvided) {
// Note: this test verifies that non-const method is invoked from const method.
EXPECT_THAT([this]() { auto &const_impl { m_impl }; const_impl.execute(std::function<void(TestIface &)> {}); },
ThrowsMessage<std::logic_error>(HasSubstr("Empty callback function provided in RetryScheduler::execute!")));
}

Expand Down Expand Up @@ -435,6 +461,54 @@ TEST_F_S(Execute, ExceptionThrown, AfterStopToken) {
EXPECT_FALSE(m_impl.isScheduled());
}

TEST_F_S(Execute, ConstVsNonConst, WithoutStopToken) {
const auto const_callback = [](const TestIface &) { /* noop */ };
const auto non_const_callback = [](TestIface &) { /* noop */ };

// Verify that concepts are working as expected
static_assert(isValidNonConstCallback(const_callback), "Invalid non-const callback");
static_assert(isValidNonConstCallback(non_const_callback), "Invalid non-const callback");
static_assert(isValidConstCallback(const_callback), "Invalid const callback");
static_assert(!isValidConstCallback(non_const_callback), "Invalid const callback");

// Verify it compiles with non-const
auto &non_const_impl { m_impl };
non_const_impl.execute(const_callback);
non_const_impl.execute(non_const_callback);

// Verify it compiles with const
const auto &const_impl { m_impl };
const_impl.execute(const_callback);
}

TEST_F_S(Execute, ConstVsNonConst, WithStopToken) {
const auto const_const_callback = [](const TestIface &, const display_device::SchedulerStopToken &) { /* noop */ };
const auto const_non_const_callback = [](const TestIface &, display_device::SchedulerStopToken &) { /* noop */ };
const auto non_const_const_callback = [](TestIface &, const display_device::SchedulerStopToken &) { /* noop */ };
const auto non_const_non_const_callback = [](TestIface &, display_device::SchedulerStopToken &) { /* noop */ };

// Verify that concepts are working as expected
static_assert(isValidNonConstCallback(const_const_callback), "Invalid non-const callback");
static_assert(isValidNonConstCallback(const_non_const_callback), "Invalid non-const callback");
static_assert(isValidNonConstCallback(non_const_const_callback), "Invalid non-const callback");
static_assert(isValidNonConstCallback(non_const_non_const_callback), "Invalid non-const callback");
static_assert(isValidConstCallback(const_const_callback), "Invalid const callback");
static_assert(!isValidConstCallback(const_non_const_callback), "Invalid const callback");
static_assert(!isValidConstCallback(non_const_const_callback), "Invalid const callback");
static_assert(!isValidConstCallback(non_const_non_const_callback), "Invalid const callback");

// Verify it compiles with non-const
auto &non_const_impl { m_impl };
non_const_impl.execute(const_const_callback);
non_const_impl.execute(const_non_const_callback);
non_const_impl.execute(non_const_const_callback);
non_const_impl.execute(non_const_non_const_callback);

// Verify it compiles with const
const auto &const_impl { m_impl };
const_impl.execute(const_const_callback);
}

TEST_F_S(Stop) {
EXPECT_FALSE(m_impl.isScheduled());
m_impl.stop();
Expand Down
Loading