Delay function for unit tests (#3767)

Sometimes, it is useful it useful to be able to delay a unit test and have
the back async processing running too. This function continuously calls
io_ctx.run_one_for in system class for the specified amount of time.
This commit is contained in:
Dimitrios Siganos 2022-03-30 11:42:49 +01:00 committed by GitHub
commit ec2796a0fc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 0 deletions

View file

@ -307,6 +307,22 @@ std::error_code nano::system::poll_until_true (std::chrono::nanoseconds deadline
return ec;
}
/**
* This function repetitively calls io_ctx.run_one_for until delay number of milliseconds elapse.
* It can be used to sleep for a duration in unit tests whilst allowing the background io contexts to continue processing.
* @param delay milliseconds of delay
*/
void nano::system::delay_ms (std::chrono::milliseconds const & delay)
{
auto now = std::chrono::steady_clock::now ();
auto endtime = now + delay;
while (now <= endtime)
{
io_ctx.run_one_for (endtime - now);
now = std::chrono::steady_clock::now ();
}
}
namespace
{
class traffic_generator : public std::enable_shared_from_this<traffic_generator>

View file

@ -43,6 +43,7 @@ public:
*/
std::error_code poll (std::chrono::nanoseconds const & sleep_time = std::chrono::milliseconds (50));
std::error_code poll_until_true (std::chrono::nanoseconds deadline, std::function<bool ()>);
void delay_ms (std::chrono::milliseconds const & delay);
void stop ();
void deadline_set (std::chrono::duration<double, std::nano> const & delta);
std::shared_ptr<nano::node> add_node (nano::node_flags = nano::node_flags (), nano::transport::transport_type = nano::transport::transport_type::tcp);