#include <exception>
#include <ranges>
#include <seastar/testing/test_case.hh>
#include <seastar/testing/thread_test_case.hh>
#include <seastar/core/gate.hh>
#include <seastar/core/loop.hh>
class expected_exception : public std::runtime_error {
public:
expected_exception() : runtime_error("expected") {}
};
SEASTAR_TEST_CASE(deferred_close_test) {
for (auto i = 0; i < expected; i++) {
++count;
});
}
}).then([&] {
BOOST_REQUIRE_EQUAL(count, expected);
});
});
}
SEASTAR_TEST_CASE(move_deferred_close_test) {
}).then([&] {
});
});
}
SEASTAR_TEST_CASE(close_now_test) {
for (auto i = 0; i < expected; i++) {
++count;
});
}
close_gate.close_now();
BOOST_REQUIRE_EQUAL(count, expected);
});
});
}
SEASTAR_TEST_CASE(cancel_deferred_close_test) {
{
close_gate.cancel();
}
return make_ready_future<>();
}
SEASTAR_TEST_CASE(with_closeable_test) {
return do_with(0, 42, [] (
int& count,
int& expected) {
return with_closeable(
gate(), [&] (
gate& g) {
for (auto i = 0; i < expected; i++) {
++count;
});
}
return 17;
}).then([&] (int res) {
BOOST_REQUIRE_EQUAL(res, 17);
BOOST_REQUIRE_EQUAL(count, expected);
});
});
}
SEASTAR_TEST_CASE(with_closeable_exception_test) {
return do_with(0, 42, [] (
int& count,
int& expected) {
return with_closeable(
gate(), [&] (
gate& g) {
for (auto i = 0; i < expected; i++) {
++count;
});
}
throw expected_exception();
}).handle_exception_type([&] (const expected_exception&) {
BOOST_REQUIRE_EQUAL(count, expected);
});
});
}
namespace {
class count_stops {
int _count = -1;
int* _ptr = nullptr;
public:
count_stops(int* ptr = nullptr) noexcept
: _ptr(ptr ? ptr : &_count)
{
*_ptr = 0;
}
count_stops(count_stops&& o) noexcept {
std::exchange(_count, o._count);
if (o._ptr == &o._count) {
_ptr = &_count;
} else {
std::exchange(_ptr, o._ptr);
}
}
++*_ptr;
return make_ready_future<>();
}
int stopped() const noexcept {
return *_ptr;
}
};
}
SEASTAR_TEST_CASE(cancel_deferred_stop_test) {
count_stops cs;
{
stop.cancel();
}
BOOST_REQUIRE_EQUAL(cs.stopped(), 0);
return make_ready_future<>();
}
SEASTAR_TEST_CASE(deferred_stop_test) {
return do_with(count_stops(), [] (count_stops& cs) {
}).then([&] {
BOOST_REQUIRE_EQUAL(cs.stopped(), 1);
});
});
}
SEASTAR_TEST_CASE(move_deferred_stop_test) {
return do_with(count_stops(), [] (count_stops& cs) {
}).then([&] {
BOOST_REQUIRE_EQUAL(cs.stopped(), 1);
});
});
}
SEASTAR_TEST_CASE(stop_now_test) {
return do_with(count_stops(), [] (count_stops& cs) {
stop_counting.stop_now();
BOOST_REQUIRE_EQUAL(cs.stopped(), 1);
}).then([&] {
BOOST_REQUIRE_EQUAL(cs.stopped(), 1);
});
});
}
SEASTAR_TEST_CASE(with_stoppable_test) {
return do_with(0, [] (
int& stopped) {
return with_stoppable(count_stops(&stopped), [] (count_stops& cs) {
return 17;
}).then([&] (int res) {
BOOST_REQUIRE_EQUAL(res, 17);
BOOST_REQUIRE_EQUAL(stopped, 1);
});
});
}
SEASTAR_TEST_CASE(with_stoppable_exception_test) {
return do_with(0, [] (
int& stopped) {
return with_stoppable(count_stops(&stopped), [] (count_stops& cs) {
throw expected_exception();
}).handle_exception_type([&] (const expected_exception&) {
BOOST_REQUIRE_EQUAL(stopped, 1);
});
});
}
SEASTAR_THREAD_TEST_CASE(move_open_gate_test) {
}
SEASTAR_THREAD_TEST_CASE(move_closing_gate_test) {
fut.get();
}
SEASTAR_THREAD_TEST_CASE(move_closed_gate_test) {
}
SEASTAR_THREAD_TEST_CASE(gate_holder_basic_test) {
BOOST_CHECK(!fut.available());
gh.release();
fut.get();
}
SEASTAR_THREAD_TEST_CASE(gate_holder_closed_test) {
}
SEASTAR_THREAD_TEST_CASE(gate_holder_move_test) {
BOOST_CHECK(!fut.available());
auto gh1 = std::move(gh0);
BOOST_CHECK(!fut.available());
gh1.release();
fut.get();
}
SEASTAR_THREAD_TEST_CASE(gate_holder_copy_test) {
auto gh1 = gh0;
BOOST_CHECK(!fut.available());
gh0.release();
BOOST_CHECK(!fut.available());
gh1.release();
fut.get();
}
SEASTAR_THREAD_TEST_CASE(gate_holder_copy_and_move_test) {
auto gh01 = gh00;
BOOST_CHECK(!fut0.available());
BOOST_CHECK(!fut1.available());
gh01.release();
BOOST_CHECK(!fut0.available());
BOOST_CHECK(!fut1.available());
gh00 = std::move(gh1);
fut0.get();
BOOST_CHECK(!fut1.available());
gh00.release();
fut1.get();
}
SEASTAR_THREAD_TEST_CASE(gate_holder_copy_after_close_test) {
BOOST_CHECK(!fut.available());
gh0.release();
BOOST_CHECK(!fut.available());
fut.get();
}
SEASTAR_TEST_CASE(gate_holder_parallel_copy_test) {
constexpr int expected = 42;
return do_with(0, [expected] (
int& count) {
return with_closeable(
gate(), [&] (
gate& g) {
count++;
return make_ready_future<>();
});
return 17;
}).then([&, expected] (int res) {
BOOST_REQUIRE_EQUAL(res, 17);
BOOST_REQUIRE_EQUAL(count, expected);
});
});
}
SEASTAR_THREAD_TEST_CASE(gate_holder_try_close_test) {
BOOST_CHECK(gh0.has_value());
BOOST_CHECK(!failed_gh.has_value());
auto gh1 = std::move(gh0);
BOOST_CHECK(!fut.available());
gh0.reset();
BOOST_CHECK(!fut.available());
gh1.reset();
fut.get();
}
Definition: closeable.hh:49
Definition: closeable.hh:118
A representation of a possibly not-yet-computed value.
Definition: future.hh:1240
value_type && get()
gets the value returned by the computation
Definition: future.hh:1342
void release() noexcept
Leave the held gate.
Definition: gate.hh:270
bool is_closed() const noexcept
Returns whether the gate is closed.
Definition: gate.hh:158
future close() noexcept
Definition: gate.hh:143
size_t get_count() const noexcept
Returns a current number of registered in-progress requests.
Definition: gate.hh:153
void check() const
Definition: gate.hh:133
std::optional< holder > try_hold() noexcept
Definition: gate.hh:286
holder hold()
Definition: gate.hh:279
void enter()
Definition: gate.hh:109
void leave() noexcept
Definition: gate.hh:118
auto with_gate(gate &g, Func &&func)
Definition: gate.hh:333
auto do_with(T1 &&rv1, T2 &&rv2, More &&... more) noexcept
Definition: do_with.hh:135
future parallel_for_each(Iterator begin, Sentinel end, Func &&func) noexcept
Run tasks in parallel (iterator version).
Definition: loop.hh:568
futurize_t< std::invoke_result_t< Func, Args... > > async(thread_attributes attr, Func &&func, Args &&... args) noexcept
Definition: thread.hh:245
Seastar API namespace.
Definition: abort_on_ebadf.hh:26