|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include <folly/coro/AwaitResult.h> |
| 18 | +#include <folly/coro/GtestHelpers.h> |
| 19 | +#include <folly/coro/Result.h> |
| 20 | +#include <folly/coro/safe/NowTask.h> |
| 21 | + |
| 22 | +/// Besides `co_await_result`, this test also covers the `folly::result<T>` |
| 23 | +/// integration with `coro::co_result` from `coro/Result.h`. |
| 24 | + |
| 25 | +#if FOLLY_HAS_RESULT |
| 26 | + |
| 27 | +namespace folly::coro { |
| 28 | + |
| 29 | +CO_TEST(AwaitResultTest, co_await_result_of_error) { |
| 30 | + auto voidErrorTask = []() -> NowTask<void> { |
| 31 | + co_yield co_error(std::runtime_error("foo")); |
| 32 | + }; |
| 33 | + { // Capture the error |
| 34 | + auto res = co_await co_await_result(voidErrorTask()); |
| 35 | + EXPECT_STREQ("foo", get_exception<std::runtime_error>(res)->what()); |
| 36 | + } |
| 37 | + { // Also test `coro::co_result` integration |
| 38 | + auto res = co_await co_await_result([&]() -> NowTask<void> { |
| 39 | + co_yield co_result(co_await co_await_result(voidErrorTask())); |
| 40 | + }()); |
| 41 | + EXPECT_STREQ("foo", get_exception<std::runtime_error>(res)->what()); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +CO_TEST(AwaitResultTest, co_await_result_of_value) { |
| 46 | + // Return a move-only thing to make sure we don't copy |
| 47 | + auto valueTask = []() -> NowTask<std::unique_ptr<int>> { |
| 48 | + co_return std::make_unique<int>(1337); |
| 49 | + }; |
| 50 | + { // Capture the value |
| 51 | + auto res = co_await co_await_result(valueTask()); |
| 52 | + // Real code should use `co_await co_ready(res)` to unpack! |
| 53 | + EXPECT_EQ(1337, *res.value_or_throw()); |
| 54 | + } |
| 55 | + { // Also test `coro::co_result` integration |
| 56 | + auto res = co_await co_await_result([&]() -> NowTask<std::unique_ptr<int>> { |
| 57 | + co_yield co_result(co_await co_await_result(valueTask())); |
| 58 | + }()); |
| 59 | + EXPECT_EQ(1337, *res.value_or_throw()); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +CO_TEST(AwaitResultTest, co_await_result_of_void) { |
| 64 | + int numAwaited = 0; |
| 65 | + auto voidTask = [&]() -> NowTask<void> { |
| 66 | + ++numAwaited; |
| 67 | + co_return; |
| 68 | + }; |
| 69 | + { // Capturing a "value" completion |
| 70 | + auto res = co_await co_await_result(voidTask()); |
| 71 | + static_assert(std::is_same_v<decltype(res), result<>>); |
| 72 | + EXPECT_TRUE(res.has_value()); |
| 73 | + EXPECT_EQ(1, numAwaited); |
| 74 | + } |
| 75 | + { // Also test `coro::co_result` integration |
| 76 | + auto res = co_await co_await_result([&]() -> NowTask<void> { |
| 77 | + co_yield co_result(co_await co_await_result(voidTask())); |
| 78 | + }()); |
| 79 | + static_assert(std::is_same_v<decltype(res), result<>>); |
| 80 | + EXPECT_TRUE(res.has_value()); |
| 81 | + EXPECT_EQ(2, numAwaited); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +CO_TEST(AwaitResultTest, co_await_result_stopped) { |
| 86 | + auto stoppedTask = [&]() -> NowTask<void> { |
| 87 | + co_yield co_cancelled; |
| 88 | + LOG(FATAL) << "not reached"; |
| 89 | + }; |
| 90 | + { // Capturing a "stopped" completion |
| 91 | + auto res = co_await co_await_result(stoppedTask()); |
| 92 | + EXPECT_TRUE(res.has_stopped()); |
| 93 | + } |
| 94 | + { // Also test `coro::co_result` integration |
| 95 | + auto res = co_await co_await_result([&]() -> NowTask<void> { |
| 96 | + co_yield co_result(co_await co_await_result(stoppedTask())); |
| 97 | + LOG(FATAL) << "not reached"; |
| 98 | + }()); |
| 99 | + EXPECT_TRUE(res.has_stopped()); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +struct ThrowingMove { |
| 104 | + ThrowingMove(const ThrowingMove&) = default; |
| 105 | + ThrowingMove& operator=(const ThrowingMove&) = default; |
| 106 | + ThrowingMove(ThrowingMove&&) {} |
| 107 | + ThrowingMove& operator=(ThrowingMove&&) { return *this; } |
| 108 | +}; |
| 109 | +struct NothrowMove {}; |
| 110 | + |
| 111 | +template <typename T> |
| 112 | +using TestAwaiter = detail::ResultAwaiter<TaskWithExecutor<T>>; |
| 113 | + |
| 114 | +// While it's unclear if anything cares about `await_resume()` `noexcept` |
| 115 | +// discipline, it's easy enough to check. |
| 116 | +static_assert(noexcept(FOLLY_DECLVAL(TestAwaiter<int>).await_resume())); |
| 117 | +static_assert(noexcept(FOLLY_DECLVAL(TestAwaiter<NothrowMove>).await_resume())); |
| 118 | +static_assert( |
| 119 | + !noexcept(FOLLY_DECLVAL(TestAwaiter<ThrowingMove>).await_resume())); |
| 120 | + |
| 121 | +} // namespace folly::coro |
| 122 | + |
| 123 | +#endif |
0 commit comments