diff --git a/packages/react-native/ReactCommon/callinvoker/ReactCommon/tests/TestCallInvoker.h b/packages/react-native/ReactCommon/callinvoker/ReactCommon/tests/TestCallInvoker.h new file mode 100644 index 00000000000000..1c48519631b23d --- /dev/null +++ b/packages/react-native/ReactCommon/callinvoker/ReactCommon/tests/TestCallInvoker.h @@ -0,0 +1,47 @@ +/* + * 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. + */ + +#pragma once + +#include +#include +#include +#include + +namespace facebook::react { + +class TestCallInvoker : public CallInvoker { + public: + explicit TestCallInvoker(std::shared_ptr runtime) + : runtime_(runtime) {} + + void invokeAsync(CallFunc&& func) noexcept override { + queue_.push_back(std::move(func)); + } + + void invokeSync(CallFunc&& func) override { + if (auto runtime = runtime_.lock()) { + func(*runtime); + } + } + + void flushQueue() { + if (auto runtime = runtime_.lock()) { + while (!queue_.empty()) { + queue_.front()(*runtime); + queue_.pop_front(); + runtime->drainMicrotasks(); // Run microtasks every cycle. + } + } + } + + private: + std::list queue_{}; + std::weak_ptr runtime_{}; +}; + +} // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/bridging/tests/BridgingTest.h b/packages/react-native/ReactCommon/react/bridging/tests/BridgingTest.h index 7fef3c9726a258..973fcad5b127ef 100644 --- a/packages/react-native/ReactCommon/react/bridging/tests/BridgingTest.h +++ b/packages/react-native/ReactCommon/react/bridging/tests/BridgingTest.h @@ -7,6 +7,7 @@ #pragma once +#include #include #include #include @@ -15,34 +16,24 @@ namespace facebook::react { -class TestCallInvoker : public CallInvoker { +class BridgingTest : public ::testing::Test { public: - void invokeAsync(CallFunc&& fn) noexcept override { - queue_.push_back(std::move(fn)); - } - - void invokeSync(CallFunc&&) override { - FAIL() << "JSCallInvoker does not support invokeSync()"; - } - - private: - friend class BridgingTest; + BridgingTest(BridgingTest& other) = delete; + BridgingTest& operator=(BridgingTest& other) = delete; + BridgingTest(BridgingTest&& other) = delete; + BridgingTest& operator=(BridgingTest&& other) = delete; - std::list queue_; -}; - -class BridgingTest : public ::testing::Test { protected: BridgingTest() - : invoker(std::make_shared()), - runtime(hermes::makeHermesRuntime( + : runtime(hermes::makeHermesRuntime( ::hermes::vm::RuntimeConfig::Builder() // Make promises work with Hermes microtasks. .withMicrotaskQueue(true) .build())), - rt(*runtime) {} + rt(*runtime), + invoker(std::make_shared(runtime)) {} - ~BridgingTest() { + ~BridgingTest() override { LongLivedObjectCollection::get(rt).clear(); } @@ -62,16 +53,12 @@ class BridgingTest : public ::testing::Test { } void flushQueue() { - while (!invoker->queue_.empty()) { - invoker->queue_.front()(*runtime); - invoker->queue_.pop_front(); - rt.drainMicrotasks(); // Run microtasks every cycle. - } + invoker->flushQueue(); } - std::shared_ptr invoker; - std::unique_ptr runtime; + std::shared_ptr runtime; jsi::Runtime& rt; + std::shared_ptr invoker; }; } // namespace facebook::react diff --git a/packages/react-native/ReactCxxPlatform/react/io/tests/NetworkingModuleTests.cpp b/packages/react-native/ReactCxxPlatform/react/io/tests/NetworkingModuleTests.cpp index 62b113d0989333..1ff920cb37f607 100644 --- a/packages/react-native/ReactCxxPlatform/react/io/tests/NetworkingModuleTests.cpp +++ b/packages/react-native/ReactCxxPlatform/react/io/tests/NetworkingModuleTests.cpp @@ -10,7 +10,7 @@ #include #endif -#include +#include #include #include #include @@ -21,25 +21,11 @@ namespace facebook::react { -class TestCallInvoker : public CallInvoker { - public: - void invokeAsync(CallFunc&& fn) noexcept override { - queue_.push_back(std::move(fn)); - } - - void invokeSync(CallFunc&& /*func*/) override { - FAIL() << "JSCallInvoker does not support invokeSync()"; - } - - private: - std::list queue_; -}; - class NetworkingModuleTests : public testing::Test { protected: void SetUp() override { rt_ = facebook::hermes::makeHermesRuntime(); - jsInvoker_ = std::make_shared(); + jsInvoker_ = std::make_shared(rt_); } static void verifyFormData( @@ -55,7 +41,7 @@ class NetworkingModuleTests : public testing::Test { } } - std::unique_ptr rt_; + std::shared_ptr rt_; std::shared_ptr jsInvoker_; };