Skip to content

Commit 35aa1a7

Browse files
Add a TestCallInvoker
Summary: Changelog: [Internal] This adds a TestCallInvoker, suitable for unit testing Differential Revision: D78453676
1 parent 21cd09d commit 35aa1a7

File tree

3 files changed

+60
-41
lines changed

3 files changed

+60
-41
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#pragma once
9+
10+
#include <ReactCommon/CallInvoker.h>
11+
#include <list>
12+
#include <memory>
13+
14+
namespace facebook::jsi {
15+
class Runtime;
16+
}
17+
18+
namespace facebook::react {
19+
20+
class TestCallInvoker : public CallInvoker {
21+
public:
22+
explicit TestCallInvoker(std::shared_ptr<facebook::jsi::Runtime> runtime)
23+
: runtime_(std::move(runtime)) {}
24+
25+
void invokeAsync(CallFunc&& func) noexcept override {
26+
queue_.push_back(std::move(func));
27+
}
28+
29+
void invokeSync(CallFunc&& func) override {
30+
func(*runtime_);
31+
}
32+
33+
std::list<CallFunc>& queue() {
34+
return queue_;
35+
}
36+
37+
private:
38+
std::list<CallFunc> queue_{};
39+
std::shared_ptr<facebook::jsi::Runtime> runtime_{};
40+
};
41+
42+
} // namespace facebook::react

packages/react-native/ReactCommon/react/bridging/tests/BridgingTest.h

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#pragma once
99

10+
#include <ReactCommon/TestCallInvoker.h>
1011
#include <gtest/gtest.h>
1112
#include <hermes/hermes.h>
1213
#include <react/bridging/Bridging.h>
@@ -15,34 +16,24 @@
1516

1617
namespace facebook::react {
1718

18-
class TestCallInvoker : public CallInvoker {
19+
class BridgingTest : public ::testing::Test {
1920
public:
20-
void invokeAsync(CallFunc&& fn) noexcept override {
21-
queue_.push_back(std::move(fn));
22-
}
23-
24-
void invokeSync(CallFunc&&) override {
25-
FAIL() << "JSCallInvoker does not support invokeSync()";
26-
}
27-
28-
private:
29-
friend class BridgingTest;
21+
BridgingTest(BridgingTest& other) = delete;
22+
BridgingTest& operator=(BridgingTest& other) = delete;
23+
BridgingTest(BridgingTest&& other) = delete;
24+
BridgingTest& operator=(BridgingTest&& other) = delete;
3025

31-
std::list<CallFunc> queue_;
32-
};
33-
34-
class BridgingTest : public ::testing::Test {
3526
protected:
3627
BridgingTest()
37-
: invoker(std::make_shared<TestCallInvoker>()),
38-
runtime(hermes::makeHermesRuntime(
28+
: runtime(hermes::makeHermesRuntime(
3929
::hermes::vm::RuntimeConfig::Builder()
4030
// Make promises work with Hermes microtasks.
4131
.withMicrotaskQueue(true)
4232
.build())),
43-
rt(*runtime) {}
33+
rt(*runtime),
34+
invoker(std::make_shared<TestCallInvoker>(runtime)) {}
4435

45-
~BridgingTest() {
36+
~BridgingTest() override {
4637
LongLivedObjectCollection::get(rt).clear();
4738
}
4839

@@ -62,16 +53,16 @@ class BridgingTest : public ::testing::Test {
6253
}
6354

6455
void flushQueue() {
65-
while (!invoker->queue_.empty()) {
66-
invoker->queue_.front()(*runtime);
67-
invoker->queue_.pop_front();
56+
while (!invoker->queue().empty()) {
57+
invoker->queue().front()(*runtime);
58+
invoker->queue().pop_front();
6859
rt.drainMicrotasks(); // Run microtasks every cycle.
6960
}
7061
}
7162

72-
std::shared_ptr<TestCallInvoker> invoker;
73-
std::unique_ptr<jsi::Runtime> runtime;
63+
std::shared_ptr<jsi::Runtime> runtime;
7464
jsi::Runtime& rt;
65+
std::shared_ptr<TestCallInvoker> invoker;
7566
};
7667

7768
} // namespace facebook::react

packages/react-native/ReactCxxPlatform/react/io/tests/NetworkingModuleTests.cpp

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <folly/portability/Windows.h>
1111
#endif
1212

13-
#include <ReactCommon/CallInvoker.h>
13+
#include <ReactCommon/TestCallInvoker.h>
1414
#include <folly/json.h>
1515
#include <gtest/gtest.h>
1616
#include <hermes/hermes.h>
@@ -21,25 +21,11 @@
2121

2222
namespace facebook::react {
2323

24-
class TestCallInvoker : public CallInvoker {
25-
public:
26-
void invokeAsync(CallFunc&& fn) noexcept override {
27-
queue_.push_back(std::move(fn));
28-
}
29-
30-
void invokeSync(CallFunc&& /*func*/) override {
31-
FAIL() << "JSCallInvoker does not support invokeSync()";
32-
}
33-
34-
private:
35-
std::list<CallFunc> queue_;
36-
};
37-
3824
class NetworkingModuleTests : public testing::Test {
3925
protected:
4026
void SetUp() override {
4127
rt_ = facebook::hermes::makeHermesRuntime();
42-
jsInvoker_ = std::make_shared<TestCallInvoker>();
28+
jsInvoker_ = std::make_shared<TestCallInvoker>(rt_);
4329
}
4430

4531
static void verifyFormData(
@@ -55,7 +41,7 @@ class NetworkingModuleTests : public testing::Test {
5541
}
5642
}
5743

58-
std::unique_ptr<facebook::hermes::HermesRuntime> rt_;
44+
std::shared_ptr<facebook::hermes::HermesRuntime> rt_;
5945
std::shared_ptr<CallInvoker> jsInvoker_;
6046
};
6147

0 commit comments

Comments
 (0)