1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
/*
* This file is open source software, licensed to you under the terms
* of the Apache License, Version 2.0 (the "License"). See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. You may not use this file except in compliance with the License.
*
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* Copyright 2021 ScyllaDB
*/
#include <exception>
#include <functional>
#include <seastar/core/coroutine.hh>
#include <seastar/core/gate.hh>
#include <seastar/core/format.hh>
#include <seastar/core/sstring.hh>
#include <seastar/util/log.hh>
#include <seastar/testing/test_case.hh>
#include <seastar/testing/thread_test_case.hh>
using namespace seastar;
static_assert(std::is_nothrow_default_constructible_v<gate>);
static_assert(std::is_nothrow_move_constructible_v<gate>);
static_assert(std::is_nothrow_move_assignable_v<gate>);
static_assert(std::is_nothrow_default_constructible_v<named_gate>);
static_assert(std::is_nothrow_move_constructible_v<named_gate>);
static_assert(std::is_nothrow_move_assignable_v<named_gate>);
template <typename Func>
static future<> check_gate_closed_exception(Func func) {
try {
co_await futurize_invoke(func);
BOOST_FAIL("func was expected to throw gate_closed_exception");
} catch (const gate_closed_exception& e) {
BOOST_REQUIRE_EQUAL(e.what(), "gate closed");
} catch (...) {
BOOST_FAIL(format("unexpected exception: {}", std::current_exception()));
}
}
SEASTAR_TEST_CASE(basic_gate_test) {
gate g;
BOOST_REQUIRE_EQUAL(g.get_count(), 0);
BOOST_REQUIRE(!g.is_closed());
BOOST_REQUIRE_NO_THROW(g.check());
BOOST_REQUIRE_EQUAL(g.get_count(), 0);
BOOST_REQUIRE_NO_THROW(g.enter());
BOOST_REQUIRE_EQUAL(g.get_count(), 1);
auto gh0 = g.try_hold();
BOOST_REQUIRE(gh0.has_value());
BOOST_REQUIRE_EQUAL(g.get_count(), 2);
auto gh1 = g.hold();
BOOST_REQUIRE_EQUAL(g.get_count(), 3);
BOOST_REQUIRE(!g.is_closed());
auto f = g.close();
BOOST_REQUIRE(!f.available());
g.leave();
BOOST_REQUIRE_EQUAL(g.get_count(), 2);
gh0->release();
BOOST_REQUIRE_EQUAL(g.get_count(), 1);
gh1.release();
BOOST_REQUIRE_EQUAL(g.get_count(), 0);
BOOST_REQUIRE_NO_THROW(co_await std::move(f));
BOOST_REQUIRE(g.is_closed());
}
SEASTAR_TEST_CASE(gate_closed_test) {
gate g;
BOOST_REQUIRE(!g.is_closed());
BOOST_REQUIRE_NO_THROW(g.check());
BOOST_REQUIRE_NO_THROW(g.enter());
auto gh0 = g.try_hold();
BOOST_REQUIRE(gh0.has_value());
auto gh1 = g.hold();
BOOST_REQUIRE(!g.is_closed());
auto f = g.close();
BOOST_REQUIRE(!f.available());
g.leave();
gh0->release();
gh1.release();
BOOST_REQUIRE_NO_THROW(co_await std::move(f));
BOOST_REQUIRE(g.is_closed());
BOOST_REQUIRE(!g.try_hold().has_value());
co_await check_gate_closed_exception([&] { g.check(); });
co_await check_gate_closed_exception([&] { g.enter(); });
co_await check_gate_closed_exception([&] { g.hold(); });
co_await check_gate_closed_exception([&] () -> future<> { co_await with_gate(g, [] { return make_ready_future(); }); });
co_await check_gate_closed_exception([&] () -> future<> { co_await try_with_gate(g, [] { return make_ready_future(); }); });
}
template <typename Func>
static future<> check_named_gate_closed_exception(Func func, sstring name) {
try {
co_await futurize_invoke(func);
BOOST_FAIL("func was expected to throw gate_closed_exception");
} catch (const gate_closed_exception& e) {
BOOST_REQUIRE_EQUAL(e.what(), fmt::format("{} gate closed", name));
} catch (...) {
BOOST_FAIL(format("unexpected exception: {}", std::current_exception()));
}
}
SEASTAR_TEST_CASE(named_gate_closed_test) {
auto test_named_gate = [] (named_gate& g, const sstring name) -> future<> {
if (!g.is_closed()) {
BOOST_REQUIRE_NO_THROW(g.check());
BOOST_REQUIRE_NO_THROW(g.enter());
auto gh0 = g.try_hold();
BOOST_REQUIRE(gh0.has_value());
auto gh1 = g.hold();
BOOST_REQUIRE(!g.is_closed());
auto f = g.close();
BOOST_REQUIRE(!f.available());
g.leave();
gh0->release();
gh1.release();
BOOST_REQUIRE_NO_THROW(co_await std::move(f));
BOOST_REQUIRE(g.is_closed());
}
BOOST_REQUIRE(!g.try_hold().has_value());
co_await check_named_gate_closed_exception([&] { g.check(); }, name);
co_await check_named_gate_closed_exception([&] { g.enter(); }, name);
co_await check_named_gate_closed_exception([&] { g.hold(); }, name);
co_await check_named_gate_closed_exception([&] () -> future<> { co_await with_gate(g, [] { return make_ready_future(); }); }, name);
co_await check_named_gate_closed_exception([&] () -> future<> { co_await try_with_gate(g, [] { return make_ready_future(); }); }, name);
};
BOOST_TEST_MESSAGE("test_named_gate(default_constructed)");
named_gate default_constructed;
co_await test_named_gate(default_constructed, "named");
sstring foo = "foo";
BOOST_TEST_MESSAGE("test_named_gate(name_constructed)");
named_gate name_constructed(foo);
co_await test_named_gate(name_constructed, foo);
BOOST_TEST_MESSAGE("test_named_gate(move_constructed)");
auto move_constructed = std::move(name_constructed);
co_await test_named_gate(move_constructed, foo);
BOOST_TEST_MESSAGE("test_named_gate(move_assigned)");
sstring bar = "bar";
named_gate move_assigned(foo);
move_assigned = named_gate(bar);
co_await test_named_gate(move_assigned, bar);
}
|