Skip to content

Commit

Permalink
Add ASSERT_TIMELY (#2633)
Browse files Browse the repository at this point in the history
* Add ASSERT_TIMELY and apply to request_aggregator.one

* Add lambda version
  • Loading branch information
cryptocode committed Mar 5, 2020
1 parent 83afb8e commit 583124e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
8 changes: 8 additions & 0 deletions nano/core_test/testutil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@
GTEST_TEST_ERROR_CODE ((condition.value () > 0), #condition, "An error was expected", "", \
GTEST_FATAL_FAILURE_)

/** Asserts that the condition becomes true within the deadline */
#define ASSERT_TIMELY(time, condition) \
system.deadline_set (time); \
while (!(condition)) \
{ \
ASSERT_NO_ERROR (system.poll ()); \
}

/* Convenience globals for core_test */
namespace nano
{
Expand Down
11 changes: 11 additions & 0 deletions nano/node/testing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,17 @@ std::error_code nano::system::poll (std::chrono::nanoseconds const & wait_time)
return ec;
}

std::error_code nano::system::poll_until_true (std::chrono::nanoseconds deadline_a, std::function<bool()> predicate_a)
{
std::error_code ec;
deadline_set (deadline_a);
while (!ec && !predicate_a ())
{
ec = poll ();
}
return ec;
}

namespace
{
class traffic_generator : public std::enable_shared_from_this<traffic_generator>
Expand Down
1 change: 1 addition & 0 deletions nano/node/testing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class system final
* @returns 0 or nano::deadline_expired
*/
std::error_code poll (const std::chrono::nanoseconds & sleep_time = std::chrono::milliseconds (50));
std::error_code poll_until_true (std::chrono::nanoseconds deadline, std::function<bool()>);
void stop ();
void deadline_set (const std::chrono::duration<double, std::nano> & delta);
std::shared_ptr<nano::node> add_node (nano::node_flags = nano::node_flags (), nano::transport::transport_type = nano::transport::transport_type::tcp);
Expand Down

0 comments on commit 583124e

Please sign in to comment.