|
| 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/Expected.h> |
| 18 | +#include <folly/coro/Collect.h> |
| 19 | +#include <folly/coro/GtestHelpers.h> |
| 20 | +#include <gtest/gtest.h> |
| 21 | + |
| 22 | +#include "cachelib/interface/utils/CoroFiberAdapter.h" |
| 23 | + |
| 24 | +using namespace facebook::cachelib; |
| 25 | +using namespace facebook::cachelib::interface::utils; |
| 26 | + |
| 27 | +class CoroFiberAdapterTest : public testing::Test { |
| 28 | + public: |
| 29 | + template <typename T> |
| 30 | + using Result = folly::Expected<T, navy::Status>; |
| 31 | + |
| 32 | + navy::NavyThread thread_{"test"}; |
| 33 | +}; |
| 34 | + |
| 35 | +CO_TEST_F(CoroFiberAdapterTest, success) { |
| 36 | + auto result = co_await onWorkerThread( |
| 37 | + thread_, |
| 38 | + []() -> Result<int> { return 42; }, |
| 39 | + [](auto&&) { FAIL() << "should not have called cleanup function"; }); |
| 40 | + CO_ASSERT_TRUE(result.hasValue()); |
| 41 | + EXPECT_EQ(result.value(), 42); |
| 42 | +} |
| 43 | + |
| 44 | +CO_TEST_F(CoroFiberAdapterTest, defaultCleanup) { |
| 45 | + auto result = co_await onWorkerThread(thread_, []() -> Result<int> { |
| 46 | + return folly::makeUnexpected(navy::Status::DeviceError); |
| 47 | + }); |
| 48 | + CO_ASSERT_TRUE(result.hasError()); |
| 49 | + EXPECT_EQ(result.error(), navy::Status::DeviceError); |
| 50 | +} |
| 51 | + |
| 52 | +CO_TEST_F(CoroFiberAdapterTest, retryThenSuccess) { |
| 53 | + size_t callCount = 0; |
| 54 | + auto result = co_await onWorkerThread( |
| 55 | + thread_, |
| 56 | + [&callCount]() -> Result<int> { |
| 57 | + if (callCount++ < 3) { |
| 58 | + return folly::makeUnexpected(navy::Status::Retry); |
| 59 | + } |
| 60 | + return 100; |
| 61 | + }, |
| 62 | + [](auto&&) { FAIL() << "should not have called cleanup function"; }); |
| 63 | + CO_ASSERT_TRUE(result.hasValue()); |
| 64 | + EXPECT_EQ(result.value(), 100); |
| 65 | + EXPECT_EQ(callCount, 4); |
| 66 | +} |
| 67 | + |
| 68 | +CO_TEST_F(CoroFiberAdapterTest, returnError) { |
| 69 | + auto result = co_await onWorkerThread( |
| 70 | + thread_, |
| 71 | + []() -> Result<int> { |
| 72 | + return folly::makeUnexpected(navy::Status::DeviceError); |
| 73 | + }, |
| 74 | + [](auto&&) { FAIL() << "should not have called cleanup function"; }); |
| 75 | + CO_ASSERT_TRUE(result.hasError()); |
| 76 | + EXPECT_EQ(result.error(), navy::Status::DeviceError); |
| 77 | +} |
| 78 | + |
| 79 | +CO_TEST_F(CoroFiberAdapterTest, throws) { |
| 80 | + auto result = co_await folly::coro::co_awaitTry(onWorkerThread( |
| 81 | + thread_, |
| 82 | + []() -> Result<int> { throw std::runtime_error("error"); }, |
| 83 | + [](auto&&) { FAIL() << "should not have called cleanup function"; })); |
| 84 | + CO_ASSERT_TRUE(result.hasException<std::runtime_error>()); |
| 85 | +} |
| 86 | + |
| 87 | +CO_TEST_F(CoroFiberAdapterTest, cancellation) { |
| 88 | + folly::CancellationSource cs; |
| 89 | + folly::fibers::Baton fiberReady, cancelReady; |
| 90 | + bool ranCleanup = false; |
| 91 | + |
| 92 | + // Ensure the fiber runs the cleanup function in the following situation to |
| 93 | + // avoid leaking resources: |
| 94 | + // |
| 95 | + // 1. Fiber is running an async operation |
| 96 | + // 2. Awaiting coro is cancelled (e.g., times out) |
| 97 | + // 3. Fiber finishes and sets the promise |
| 98 | + |
| 99 | + auto fiberFunc = [&]() -> Result<int> { |
| 100 | + fiberReady.post(); |
| 101 | + cancelReady.wait(); |
| 102 | + return 42; |
| 103 | + }; |
| 104 | + auto cleanupFunc = [&ranCleanup](auto&& result) { |
| 105 | + ranCleanup = true; |
| 106 | + ASSERT_TRUE(result.hasValue()); |
| 107 | + EXPECT_EQ(result.value(), 42); |
| 108 | + }; |
| 109 | + auto cancelTask = [&]() -> folly::coro::Task<void> { |
| 110 | + // Ensure the fiber is inside fiberFunc() before cancellation, otherwise |
| 111 | + // onWorkerThread() will skip calling it |
| 112 | + fiberReady.wait(); |
| 113 | + cs.requestCancellation(); |
| 114 | + cancelReady.post(); |
| 115 | + co_return; |
| 116 | + }; |
| 117 | + |
| 118 | + auto result = co_await folly::coro::collectAllTry( |
| 119 | + folly::coro::co_withCancellation(cs.getToken(), |
| 120 | + onWorkerThread(thread_, |
| 121 | + std::move(fiberFunc), |
| 122 | + std::move(cleanupFunc))), |
| 123 | + cancelTask()); |
| 124 | + EXPECT_TRUE(std::get<0>(result).hasException<folly::OperationCancelled>()); |
| 125 | + thread_.drain(); // ensure fiberFunc() finishes running & sets the promise |
| 126 | + EXPECT_TRUE(ranCleanup); |
| 127 | +} |
| 128 | + |
| 129 | +CO_TEST_F(CoroFiberAdapterTest, concurrent) { |
| 130 | + auto task = [&](int value) -> folly::coro::Task<void> { |
| 131 | + auto result = co_await onWorkerThread( |
| 132 | + thread_, |
| 133 | + [value]() -> Result<int> { return value * 2; }, |
| 134 | + [](auto&&) { FAIL() << "should not have called cleanup function"; }); |
| 135 | + CO_ASSERT_TRUE(result.hasValue()); |
| 136 | + EXPECT_EQ(result.value(), value * 2); |
| 137 | + }; |
| 138 | + co_await folly::coro::collectAll(task(1), task(2), task(3), task(4), task(5)); |
| 139 | +} |
0 commit comments