#include <exception>
#include <boost/range/irange.hpp>
#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) {
return do_with(gate(), 0, 42, [] (gate& g,
int& count,
int& expected) {
auto close_gate = deferred_close(g);
for (auto i = 0; i < expected; i++) {
(void)with_gate(g, [&count] {
++count;
});
}
}).then([&] {
BOOST_REQUIRE(g.is_closed());
BOOST_REQUIRE_EQUAL(count, expected);
});
});
}
SEASTAR_TEST_CASE(move_deferred_close_test) {
return do_with(gate(), [] (gate& g) {
auto close_gate = make_shared(deferred_close(g));
BOOST_REQUIRE(!g.is_closed());
}).then([&] {
BOOST_REQUIRE(g.is_closed());
});
});
}
SEASTAR_TEST_CASE(close_now_test) {
return do_with(gate(), 0, 42, [] (gate& g,
int& count,
int& expected) {
auto close_gate = deferred_close(g);
for (auto i = 0; i < expected; i++) {
(void)with_gate(g, [&count] {
++count;
});
}
close_gate.close_now();
BOOST_REQUIRE(g.is_closed());
BOOST_REQUIRE_EQUAL(count, expected);
});
});
}
SEASTAR_TEST_CASE(cancel_deferred_close_test) {
gate g;
{
auto close_gate = deferred_close(g);
close_gate.cancel();
}
g.check();
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++) {
(void)with_gate(g, [&count] {
++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++) {
(void)with_gate(g, [&count] {
++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;
{
auto stop = deferred_stop(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) {
auto stop_counting = deferred_stop(cs);
}).then([&] {
BOOST_REQUIRE_EQUAL(cs.stopped(), 1);
});
});
}
SEASTAR_TEST_CASE(move_deferred_stop_test) {
return do_with(count_stops(), [] (count_stops& cs) {
auto stop = make_shared(deferred_stop(cs));
}).then([&] {
BOOST_REQUIRE_EQUAL(cs.stopped(), 1);
});
});
}
SEASTAR_TEST_CASE(stop_now_test) {
return do_with(count_stops(), [] (count_stops& cs) {
auto stop_counting = deferred_stop(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) {
gate g1;
g1.enter();
gate g2 = std::move(g1);
BOOST_CHECK_EQUAL(g1.get_count(), 0);
BOOST_REQUIRE_EQUAL(g2.get_count(), 1);
g2.leave();
g2.close().get();
BOOST_CHECK(!g1.is_closed());
BOOST_CHECK(g2.is_closed());
}
SEASTAR_THREAD_TEST_CASE(move_closing_gate_test) {
gate g1;
g1.enter();
auto fut = g1.close();
gate g2 = std::move(g1);
BOOST_CHECK_EQUAL(g1.get_count(), 0);
BOOST_REQUIRE_EQUAL(g2.get_count(), 1);
g2.leave();
fut.get();
BOOST_CHECK(!g1.is_closed());
BOOST_CHECK(g2.is_closed());
}
SEASTAR_THREAD_TEST_CASE(move_closed_gate_test) {
gate g1;
g1.close().get();
gate g2 = std::move(g1);
BOOST_CHECK_EQUAL(g1.get_count(), 0);
BOOST_CHECK_EQUAL(g2.get_count(), 0);
BOOST_CHECK(!g1.is_closed());
BOOST_CHECK(g2.is_closed());
}
SEASTAR_THREAD_TEST_CASE(gate_holder_basic_test) {
gate g;
auto gh = g.hold();
auto fut = g.close();
BOOST_CHECK(!fut.available());
gh.release();
fut.get();
}
SEASTAR_THREAD_TEST_CASE(gate_holder_closed_test) {
gate g;
g.close().get();
BOOST_REQUIRE_THROW(g.hold(), gate_closed_exception);
}
SEASTAR_THREAD_TEST_CASE(gate_holder_move_test) {
gate g;
auto gh0 = g.hold();
auto fut = g.close();
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) {
gate g;
auto gh0 = g.hold();
auto gh1 = gh0;
auto fut = g.close();
BOOST_CHECK(!fut.available());
gh0.release();
BOOST_CHECK(!fut.available());
gh1.release();
fut.get();
}
SEASTAR_THREAD_TEST_CASE(gate_holder_copy_and_move_test) {
gate g0;
auto gh00 = g0.hold();
auto gh01 = gh00;
auto fut0 = g0.close();
BOOST_CHECK(!fut0.available());
gate g1;
auto gh1 = g1.hold();
auto fut1 = g1.close();
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) {
gate g;
auto gh0 = g.hold();
auto fut = g.close();
BOOST_CHECK(g.is_closed());
gate::holder gh1 = gh0;
BOOST_CHECK(!fut.available());
gh0.release();
BOOST_CHECK(!fut.available());
gh1.release();
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) {
auto gh = g.hold();
count++;
return make_ready_future<>();
});
return 17;
}).then([&, expected] (int res) {
BOOST_REQUIRE_EQUAL(res, 17);
BOOST_REQUIRE_EQUAL(count, expected);
});
});
}
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:247
Seastar API namespace.
Definition: abort_on_ebadf.hh:26