Add ASSERT_TIMELY (#2633)

* Add ASSERT_TIMELY and apply to request_aggregator.one

* Add lambda version
This commit is contained in:
cryptocode 2020-03-05 13:40:58 +01:00 committed by GitHub
commit 583124e368
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 0 deletions

View file

@ -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
{

View file

@ -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>

View file

@ -38,6 +38,7 @@ public:
* @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);