Skip to content

Commit 9850984

Browse files
Add a TestCallInvoker (facebook#52652)
Summary: Changelog: [Internal] This adds a TestCallInvoker, suitable for unit testing Differential Revision: D78453676
1 parent 21cd09d commit 9850984

File tree

3 files changed

+63
-43
lines changed

3 files changed

+63
-43
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 <jsi/jsi.h>
12+
#include <list>
13+
#include <memory>
14+
15+
namespace facebook::react {
16+
17+
class TestCallInvoker : public CallInvoker {
18+
public:
19+
explicit TestCallInvoker(std::shared_ptr<facebook::jsi::Runtime> runtime)
20+
: runtime_(std::move(runtime)) {}
21+
22+
void invokeAsync(CallFunc&& func) noexcept override {
23+
queue_.push_back(std::move(func));
24+
}
25+
26+
void invokeSync(CallFunc&& func) override {
27+
func(*runtime_);
28+
}
29+
30+
std::list<CallFunc>& queue() {
31+
return queue_;
32+
}
33+
34+
void flushQueue() {
35+
while (!queue_.empty()) {
36+
queue_.front()(*runtime_);
37+
queue_.pop_front();
38+
runtime_->drainMicrotasks(); // Run microtasks every cycle.
39+
}
40+
}
41+
42+
private:
43+
std::list<CallFunc> queue_{};
44+
std::shared_ptr<facebook::jsi::Runtime> runtime_{};
45+
};
46+
47+
} // namespace facebook::react

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

Lines changed: 13 additions & 26 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,12 @@ 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();
68-
rt.drainMicrotasks(); // Run microtasks every cycle.
69-
}
56+
invoker->flushQueue();
7057
}
7158

72-
std::shared_ptr<TestCallInvoker> invoker;
73-
std::unique_ptr<jsi::Runtime> runtime;
59+
std::shared_ptr<jsi::Runtime> runtime;
7460
jsi::Runtime& rt;
61+
std::shared_ptr<TestCallInvoker> invoker;
7562
};
7663

7764
} // 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)