diff --git a/packages/react-native/ReactCommon/react/utils/tests/LowPriorityExecutorTest.cpp b/packages/react-native/ReactCommon/react/utils/tests/LowPriorityExecutorTest.cpp new file mode 100644 index 000000000000..424a21167e96 --- /dev/null +++ b/packages/react-native/ReactCommon/react/utils/tests/LowPriorityExecutorTest.cpp @@ -0,0 +1,79 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#include + +#include +#include +#include + +#include +#include +#include +#include +#include + +namespace facebook::react { + +using namespace std::chrono_literals; + +TEST(LowPriorityExecutorTest, testExecuteRunsPostedWork) { + std::promise result; + + LowPriorityExecutor::execute([&result] { result.set_value(3 * 7); }); + + auto resultFuture = result.get_future(); + ASSERT_EQ(resultFuture.wait_for(5s), std::future_status::ready); + EXPECT_EQ(resultFuture.get(), 21); +} + +#ifdef __ANDROID__ + +TEST(LowPriorityExecutorTest, testExecuteRunsPostedWorkOnExecutorThread) { + auto callerThreadId = std::this_thread::get_id(); + std::promise executorThreadId; + + LowPriorityExecutor::execute([&executorThreadId] { + executorThreadId.set_value(std::this_thread::get_id()); + }); + + auto executorThreadIdFuture = executorThreadId.get_future(); + ASSERT_EQ(executorThreadIdFuture.wait_for(5s), std::future_status::ready); + EXPECT_NE(executorThreadIdFuture.get(), callerThreadId); +} + +TEST(LowPriorityExecutorTest, testExecuteRunsQueuedWorkInPostOrder) { + constexpr int kTaskCount = 5; + std::promise> observedOrder; + std::mutex orderMutex; + std::vector order; + + for (int i = 0; i < kTaskCount; ++i) { + LowPriorityExecutor::execute([&, i] { + std::vector currentOrder; + { + std::lock_guard lock(orderMutex); + order.push_back(i); + if (i == kTaskCount - 1) { + currentOrder = order; + } + } + + if (!currentOrder.empty()) { + observedOrder.set_value(std::move(currentOrder)); + } + }); + } + + auto observedOrderFuture = observedOrder.get_future(); + ASSERT_EQ(observedOrderFuture.wait_for(5s), std::future_status::ready); + EXPECT_THAT(observedOrderFuture.get(), ::testing::ElementsAre(0, 1, 2, 3, 4)); +} + +#endif + +} // namespace facebook::react