|
22 | 22 | #include <exception>
|
23 | 23 | #include <numeric>
|
24 | 24 | #include <ranges>
|
| 25 | +#include <any> |
25 | 26 |
|
26 | 27 | #include <seastar/core/circular_buffer.hh>
|
27 | 28 | #include <seastar/core/coroutine.hh>
|
|
35 | 36 | #include <seastar/coroutine/as_future.hh>
|
36 | 37 | #include <seastar/coroutine/exception.hh>
|
37 | 38 | #include <seastar/coroutine/generator.hh>
|
| 39 | +#include <seastar/coroutine/try_future.hh> |
38 | 40 | #include <seastar/testing/random.hh>
|
39 | 41 | #include <seastar/testing/test_case.hh>
|
| 42 | +#include <seastar/util/defer.hh> |
40 | 43 | #include <seastar/util/later.hh>
|
41 | 44 |
|
42 | 45 | using seastar::broken_promise;
|
@@ -1018,3 +1021,145 @@ SEASTAR_TEST_CASE(test_lambda_coroutine_in_continuation) {
|
1018 | 1021 | }));
|
1019 | 1022 | BOOST_REQUIRE_EQUAL(sin1, sin2);
|
1020 | 1023 | }
|
| 1024 | + |
| 1025 | +class test_exception : std::exception { }; |
| 1026 | + |
| 1027 | +future<> throw_void() { |
| 1028 | + fmt::print("throw_void()\n"); |
| 1029 | + co_await sleep(1ms); |
| 1030 | + throw test_exception{}; |
| 1031 | +} |
| 1032 | + |
| 1033 | +future<> return_ex_void() { |
| 1034 | + fmt::print("return_ex_void()\n"); |
| 1035 | + return make_exception_future<>(test_exception{}); |
| 1036 | +} |
| 1037 | + |
| 1038 | +future<> return_void() { |
| 1039 | + fmt::print("return_void()\n"); |
| 1040 | + co_await sleep(1ms); |
| 1041 | +} |
| 1042 | + |
| 1043 | +future<int> throw_int() { |
| 1044 | + fmt::print("throw_int()\n"); |
| 1045 | + co_await sleep(1ms); |
| 1046 | + throw test_exception{}; |
| 1047 | +} |
| 1048 | + |
| 1049 | +future<int> return_ex_int() { |
| 1050 | + fmt::print("return_ex_int()\n"); |
| 1051 | + return make_exception_future<int>(test_exception{}); |
| 1052 | +} |
| 1053 | + |
| 1054 | +future<int> return_int() { |
| 1055 | + fmt::print("return_int()\n"); |
| 1056 | + co_await sleep(1ms); |
| 1057 | + co_return 128; |
| 1058 | +} |
| 1059 | + |
| 1060 | +class dummy { |
| 1061 | + int& _c; |
| 1062 | + |
| 1063 | +public: |
| 1064 | + explicit dummy(int& c) : _c(c) { ++_c; } |
| 1065 | + dummy(const dummy& o) : _c(o._c) { ++_c; } |
| 1066 | + dummy(dummy&&) = delete; |
| 1067 | + ~dummy() { --_c; } |
| 1068 | +}; |
| 1069 | + |
| 1070 | +template <bool CheckPreempt, std::invocable<> F> |
| 1071 | +std::invoke_result_t<F> do_run_try_future_test(F underlying_func, int& ctor_dtor_counter, bool& run_past) { |
| 1072 | + const auto check_cxx_exceptions_on_exit = seastar::defer([cxx_exception_before = seastar::engine().cxx_exceptions()] () noexcept { |
| 1073 | + if (seastar::engine().cxx_exceptions() != cxx_exception_before) { |
| 1074 | + // We are in a destructor, cannot throw |
| 1075 | + std::abort(); |
| 1076 | + } |
| 1077 | + }); |
| 1078 | + |
| 1079 | + dummy d1{ctor_dtor_counter}; |
| 1080 | + dummy d2{ctor_dtor_counter}; |
| 1081 | + |
| 1082 | + std::vector<dummy> dummies; |
| 1083 | + for (unsigned i = 0; i < 10; ++i) { |
| 1084 | + dummies.emplace_back(ctor_dtor_counter); |
| 1085 | + } |
| 1086 | + |
| 1087 | + BOOST_REQUIRE_GT(ctor_dtor_counter, 0); |
| 1088 | + |
| 1089 | + using return_future_type = std::invoke_result_t<F>; |
| 1090 | + using return_type = typename return_future_type::value_type; |
| 1091 | + constexpr bool is_void = std::is_same_v<return_future_type, future<>>; |
| 1092 | + |
| 1093 | + std::any ret; |
| 1094 | + |
| 1095 | + try { |
| 1096 | + if constexpr (is_void) { |
| 1097 | + if constexpr (CheckPreempt) { |
| 1098 | + co_await seastar::coroutine::try_future(underlying_func()); |
| 1099 | + } else { |
| 1100 | + co_await seastar::coroutine::try_future_without_preemption_check(underlying_func()); |
| 1101 | + } |
| 1102 | + } else { |
| 1103 | + if constexpr (CheckPreempt) { |
| 1104 | + ret = co_await seastar::coroutine::try_future(underlying_func()); |
| 1105 | + } else { |
| 1106 | + ret = co_await seastar::coroutine::try_future_without_preemption_check(underlying_func()); |
| 1107 | + } |
| 1108 | + } |
| 1109 | + run_past = true; |
| 1110 | + } catch (...) { |
| 1111 | + BOOST_FAIL(fmt::format("Exception should be handled in try_future, bug caught: {}", std::current_exception())); |
| 1112 | + } |
| 1113 | + |
| 1114 | + if constexpr (!is_void) { |
| 1115 | + co_return std::any_cast<return_type>(ret); |
| 1116 | + } |
| 1117 | +} |
| 1118 | + |
| 1119 | +template <bool CheckPreempt, std::invocable<> F> |
| 1120 | +future<> run_try_future_test(F underlying_func, std::optional<std::any> expected_value, std::source_location sl = std::source_location::current()) { |
| 1121 | + fmt::print("running test case at {}:{}\n", sl.file_name(), sl.line()); |
| 1122 | + |
| 1123 | + int ctor_dtor_counter{0}; |
| 1124 | + bool run_past{false}; |
| 1125 | + |
| 1126 | + const bool throws = !expected_value.has_value(); |
| 1127 | + |
| 1128 | + using return_future_type = std::invoke_result_t<F>; |
| 1129 | + using return_type = typename return_future_type::value_type; |
| 1130 | + constexpr bool is_void = std::is_same_v<return_future_type, future<>>; |
| 1131 | + |
| 1132 | + try { |
| 1133 | + if constexpr (is_void) { |
| 1134 | + co_await do_run_try_future_test<CheckPreempt>(std::move(underlying_func), ctor_dtor_counter, run_past); |
| 1135 | + BOOST_REQUIRE(expected_value); |
| 1136 | + } else { |
| 1137 | + auto value = co_await do_run_try_future_test<CheckPreempt>(std::move(underlying_func), ctor_dtor_counter, run_past); |
| 1138 | + BOOST_REQUIRE(expected_value); |
| 1139 | + BOOST_REQUIRE_EQUAL(value, std::any_cast<return_type>(*expected_value)); |
| 1140 | + } |
| 1141 | + } catch (test_exception&) { |
| 1142 | + BOOST_REQUIRE(throws); |
| 1143 | + } catch (...) { |
| 1144 | + BOOST_FAIL(fmt::format("Unexpected exception {}", std::current_exception())); |
| 1145 | + } |
| 1146 | + |
| 1147 | + BOOST_REQUIRE_EQUAL(run_past, !throws); |
| 1148 | + BOOST_REQUIRE_EQUAL(ctor_dtor_counter, 0); |
| 1149 | +} |
| 1150 | + |
| 1151 | +SEASTAR_TEST_CASE(test_try_future) { |
| 1152 | + co_await run_try_future_test<true>(return_void, std::any{}); |
| 1153 | + co_await run_try_future_test<false>(return_void, std::any{}); |
| 1154 | + co_await run_try_future_test<true>(return_ex_void, std::nullopt); |
| 1155 | + co_await run_try_future_test<false>(return_ex_void, std::nullopt); |
| 1156 | + co_await run_try_future_test<true>(throw_void, std::nullopt); |
| 1157 | + co_await run_try_future_test<false>(throw_void, std::nullopt); |
| 1158 | + |
| 1159 | + co_await run_try_future_test<true>(return_int, 128); |
| 1160 | + co_await run_try_future_test<false>(return_int, 128); |
| 1161 | + co_await run_try_future_test<true>(return_ex_int, std::nullopt); |
| 1162 | + co_await run_try_future_test<false>(return_ex_int, std::nullopt); |
| 1163 | + co_await run_try_future_test<true>(throw_int, std::nullopt); |
| 1164 | + co_await run_try_future_test<false>(throw_int, std::nullopt); |
| 1165 | +} |
0 commit comments