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: Repeating operations for util::async #1776

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from

Conversation

godexsoft
Copy link
Collaborator

Async framework needed a way to do repeating operations (think simplest cases like AmendmentBlockHandler).
This PR implements the absolute minimum, barebones repeating operations that

  • Can't return any values (void)
  • Do not take any arguments in the user-provided function
  • Can't be scheduled (i.e. a delay before starting repeating)
  • Can't be stopped from inside the user block of code (i.e. does not have stop token or anything like that)
  • Can be stopped through the RepeatedOperation's abort() function (but not from the user-provided repeating function)

*/
Repeat(boost::asio::io_context& ioc);
Repeat(auto& ctx) : timer_(ctx)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes it usable with other types of contexts such as thread_pool or strand.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe worth to leave this comment in the code.


/**
* @brief Wait for the operation to get cancelled
* @warning Calling this from inside of the repeating operation yields a deadlock
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be worked around with custom code instead of util::Repeat but i decided not to make it more complicated than is necessary now. We can always improve it in the future as long as the interface remains the same or does not change too much.

Copy link

codecov bot commented Dec 11, 2024

Codecov Report

Attention: Patch coverage is 63.33333% with 11 lines in your changes missing coverage. Please review.

Project coverage is 71.90%. Comparing base (4ff2953) to head (7f1ae8f).
Report is 1 commits behind head on develop.

Files with missing lines Patch % Lines
src/util/async/Operation.hpp 54.54% 3 Missing and 2 partials ⚠️
src/util/async/AnyExecutionContext.hpp 63.63% 3 Missing and 1 partial ⚠️
src/util/async/impl/ErasedOperation.hpp 0.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1776      +/-   ##
===========================================
+ Coverage    71.72%   71.90%   +0.18%     
===========================================
  Files          295      297       +2     
  Lines        12679    12796     +117     
  Branches      6499     6550      +51     
===========================================
+ Hits          9094     9201     +107     
+ Misses        1874     1856      -18     
- Partials      1711     1739      +28     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

*/
Repeat(boost::asio::io_context& ioc);
Repeat(auto& ctx) : timer_(ctx)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe worth to leave this comment in the code.

using DataType = std::expected<void, ExecutionError>;
using StopSourceType = typename CtxType::StopSource;

std::unique_ptr<util::Repeat> repeat_;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why unique_ptr?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Repeat is not movable. But let me try with your PR #1775

* @warning Calling this from inside of the repeating operation yields a deadlock
*/
void
wait() noexcept
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably this is convenience for the whole framework but from method wait() I would expect waiting for an operation to finish.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, that's what it sort of does, waits until "finished" which in case of a repeating block of code is same as "aborted".. the naming may be not the best but it matches others in the framework

auto mockRepeatingOp = RepeatingOperationType<std::any>{};
EXPECT_CALL(mockRepeatingOp, wait());
EXPECT_CALL(
mockExecutionContext, executeRepeatedly(An<std::chrono::milliseconds>(), An<std::function<std::any()>>())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
mockExecutionContext, executeRepeatedly(An<std::chrono::milliseconds>(), An<std::function<std::any()>>())
mockExecutionContext, executeRepeatedly(A<std::chrono::milliseconds>(), A<std::function<std::any()>>())

Also why not 1 millisecond?

)
.WillOnce([&mockRepeatingOp] -> RepeatingOperationType<std::any> const& { return mockRepeatingOp; });

auto res = ctx.executeRepeatedly(std::chrono::milliseconds(1), [] -> void { throw 0; });
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why throwing? If you don't want this to be called probably strict function mock would be better.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just a way to make sure this function does not actually gets called or used - that's not the intention of this test. it's the same with all other checks in this file so i just went with the same style

{
testing::MockFunction<void()> call;

auto res = this->ctx.executeRepeatedly(std::chrono::milliseconds(1), [&] { call.Call(); });
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
auto res = this->ctx.executeRepeatedly(std::chrono::milliseconds(1), [&] { call.Call(); });
auto res = this->ctx.executeRepeatedly(std::chrono::milliseconds(1), call.AsStdFunction());

{
repeat_->start(interval, std::forward<decltype(fn)>(fn));
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you need a custom destructor here? It looks like op_ will die before repeat_ now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants