Files
scylladb/test/lib/eventually.hh
Benny Halevy 9d5e3f050e test: lib: eventually: make *EVENTUALLY_EQUAL inline functions
rather then macros.

This is a first cleanup step before adding a sleep function
parameter to support also manual_clock.

Also, add a call to BOOST_REQUIRE_EQUAL/BOOST_CHECK_EQUAL,
respectively, to make an error more visible in the test log
since those entry points print the offending values
when not equal.

Signed-off-by: Benny Halevy <bhalevy@scylladb.com>
(cherry picked from commit b509644972)
2025-02-03 15:50:22 +02:00

67 lines
1.4 KiB
C++

/*
* Copyright (C) 2018-present ScyllaDB
*/
/*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#pragma once
#include <boost/test/unit_test.hpp>
#include <seastar/core/sleep.hh>
#include <seastar/util/noncopyable_function.hh>
#include "seastarx.hh"
inline
void eventually(noncopyable_function<void ()> f, size_t max_attempts = 17) {
size_t attempts = 0;
while (true) {
try {
f();
break;
} catch (...) {
if (++attempts < max_attempts) {
sleep(std::chrono::milliseconds(1 << attempts)).get();
} else {
throw;
}
}
}
}
inline
bool eventually_true(noncopyable_function<bool ()> f) {
const unsigned max_attempts = 15;
unsigned attempts = 0;
while (true) {
if (f()) {
return true;
}
if (++attempts < max_attempts) {
seastar::sleep(std::chrono::milliseconds(1 << attempts)).get();
} else {
return false;
}
}
return false;
}
// Must be called in a seastar thread
template <typename T>
void REQUIRE_EVENTUALLY_EQUAL(std::function<T()> a, T b) {
eventually_true([&] { return a() == b; });
BOOST_REQUIRE_EQUAL(a(), b);
}
// Must be called in a seastar thread
template <typename T>
void CHECK_EVENTUALLY_EQUAL(std::function<T()> a, T b) {
eventually_true([&] { return a() == b; });
BOOST_CHECK_EQUAL(a(), b);
}