Skip to content

Commit 3ae96ae

Browse files
joyeecheungruyadorno
authored andcommitted
test: make IsolateData per-isolate in cctest
This ensures that we only create one IsolateData for each isolate inthe cctest, since IsolateData are meant to be per-isolate. We need to make the isolate and isolate_data static in the test fixtures as a result, similar to how the event loops and array buffer allocators are managed in the NodeZeroIsolateTestFixture but it is fine because gtest ensures that the Setup() and TearDown() of the fixtures are always run in order and would never overlap in one process. PR-URL: #48450 Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent f18b287 commit 3ae96ae

5 files changed

+34
-29
lines changed

test/cctest/node_test_fixture.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ uv_loop_t NodeZeroIsolateTestFixture::current_loop;
66
NodePlatformUniquePtr NodeZeroIsolateTestFixture::platform;
77
TracingAgentUniquePtr NodeZeroIsolateTestFixture::tracing_agent;
88
bool NodeZeroIsolateTestFixture::node_initialized = false;
9-
9+
v8::Isolate* NodeTestFixture::isolate_ = nullptr;
10+
node::IsolateData* EnvironmentTestFixture::isolate_data_ = nullptr;
1011

1112
void NodeTestEnvironment::SetUp() {
1213
NodeZeroIsolateTestFixture::tracing_agent =

test/cctest/node_test_fixture.h

+26-13
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class NodeTestEnvironment final : public ::testing::Environment {
6767
void TearDown() override;
6868
};
6969

70+
class NodeTestFixture;
7071

7172
class NodeZeroIsolateTestFixture : public ::testing::Test {
7273
protected:
@@ -104,12 +105,13 @@ class NodeZeroIsolateTestFixture : public ::testing::Test {
104105
}
105106

106107
friend NodeTestEnvironment;
108+
friend NodeTestFixture;
107109
};
108110

109111

110112
class NodeTestFixture : public NodeZeroIsolateTestFixture {
111113
protected:
112-
v8::Isolate* isolate_;
114+
static v8::Isolate* isolate_;
113115

114116
void SetUp() override {
115117
NodeZeroIsolateTestFixture::SetUp();
@@ -130,7 +132,22 @@ class NodeTestFixture : public NodeZeroIsolateTestFixture {
130132

131133

132134
class EnvironmentTestFixture : public NodeTestFixture {
133-
public:
135+
protected:
136+
static node::IsolateData* isolate_data_;
137+
138+
void SetUp() override {
139+
NodeTestFixture::SetUp();
140+
isolate_data_ = node::CreateIsolateData(NodeTestFixture::isolate_,
141+
&NodeTestFixture::current_loop,
142+
platform.get());
143+
CHECK_NE(nullptr, isolate_data_);
144+
}
145+
146+
void TearDown() override {
147+
node::FreeIsolateData(isolate_data_);
148+
NodeTestFixture::TearDown();
149+
}
150+
134151
class Env {
135152
public:
136153
Env(const v8::HandleScope& handle_scope,
@@ -142,23 +159,20 @@ class EnvironmentTestFixture : public NodeTestFixture {
142159
CHECK(!context_.IsEmpty());
143160
context_->Enter();
144161

145-
isolate_data_ = node::CreateIsolateData(isolate,
146-
&NodeTestFixture::current_loop,
147-
platform.get());
148-
CHECK_NE(nullptr, isolate_data_);
149162
std::vector<std::string> args(*argv, *argv + 1);
150163
std::vector<std::string> exec_args(*argv, *argv + 1);
151-
environment_ = node::CreateEnvironment(isolate_data_,
152-
context_,
153-
args,
154-
exec_args,
155-
flags);
164+
DCHECK_EQ(EnvironmentTestFixture::isolate_data_->isolate(), isolate);
165+
environment_ =
166+
node::CreateEnvironment(EnvironmentTestFixture::isolate_data_,
167+
context_,
168+
args,
169+
exec_args,
170+
flags);
156171
CHECK_NE(nullptr, environment_);
157172
}
158173

159174
~Env() {
160175
node::FreeEnvironment(environment_);
161-
node::FreeIsolateData(isolate_data_);
162176
context_->Exit();
163177
}
164178

@@ -175,7 +189,6 @@ class EnvironmentTestFixture : public NodeTestFixture {
175189

176190
private:
177191
v8::Local<v8::Context> context_;
178-
node::IsolateData* isolate_data_;
179192
node::Environment* environment_;
180193
};
181194
};

test/cctest/test_environment.cc

+4-13
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ static std::string cb_1_arg; // NOLINT(runtime/string)
3030
class EnvironmentTest : public EnvironmentTestFixture {
3131
private:
3232
void TearDown() override {
33-
NodeTestFixture::TearDown();
33+
EnvironmentTestFixture::TearDown();
3434
called_cb_1 = false;
3535
called_cb_2 = false;
3636
called_cb_ordered_1 = false;
@@ -674,12 +674,8 @@ TEST_F(EnvironmentTest, NestedMicrotaskQueue) {
674674
v8::String::NewFromUtf8Literal(isolate_, "mustCall"),
675675
must_call).Check();
676676

677-
node::IsolateData* isolate_data = node::CreateIsolateData(
678-
isolate_, &NodeTestFixture::current_loop, platform.get());
679-
CHECK_NE(nullptr, isolate_data);
680-
681-
node::Environment* env = node::CreateEnvironment(
682-
isolate_data, context, {}, {});
677+
node::Environment* env =
678+
node::CreateEnvironment(isolate_data_, context, {}, {});
683679
CHECK_NE(nullptr, env);
684680

685681
v8::Local<v8::Function> eval_in_env = node::LoadEnvironment(
@@ -718,7 +714,6 @@ TEST_F(EnvironmentTest, NestedMicrotaskQueue) {
718714
EXPECT_EQ(callback_calls, (IntVec { 1, 3, 6, 2, 4, 7, 5 }));
719715

720716
node::FreeEnvironment(env);
721-
node::FreeIsolateData(isolate_data);
722717
}
723718

724719
static bool interrupted = false;
@@ -733,19 +728,15 @@ TEST_F(EnvironmentTest, RequestInterruptAtExit) {
733728
CHECK(!context.IsEmpty());
734729
context->Enter();
735730

736-
node::IsolateData* isolate_data = node::CreateIsolateData(
737-
isolate_, &NodeTestFixture::current_loop, platform.get());
738-
CHECK_NE(nullptr, isolate_data);
739731
std::vector<std::string> args(*argv, *argv + 1);
740732
std::vector<std::string> exec_args(*argv, *argv + 1);
741733
node::Environment* environment =
742-
node::CreateEnvironment(isolate_data, context, args, exec_args);
734+
node::CreateEnvironment(isolate_data_, context, args, exec_args);
743735
CHECK_NE(nullptr, environment);
744736

745737
node::RequestInterrupt(environment, OnInterrupt, nullptr);
746738
node::FreeEnvironment(environment);
747739
EXPECT_TRUE(interrupted);
748740

749-
node::FreeIsolateData(isolate_data);
750741
context->Exit();
751742
}

test/cctest/test_node_api.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class NodeApiTest : public EnvironmentTestFixture {
1616
private:
1717
void SetUp() override { EnvironmentTestFixture::SetUp(); }
1818

19-
void TearDown() override { NodeTestFixture::TearDown(); }
19+
void TearDown() override { EnvironmentTestFixture::TearDown(); }
2020
};
2121

2222
TEST_F(NodeApiTest, CreateNodeApiEnv) {

test/cctest/test_report.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ bool report_callback_called = false;
2020
class ReportTest : public EnvironmentTestFixture {
2121
private:
2222
void TearDown() override {
23-
NodeTestFixture::TearDown();
23+
EnvironmentTestFixture::TearDown();
2424
report_callback_called = false;
2525
}
2626
};

0 commit comments

Comments
 (0)