Skip to content

Commit b212eb3

Browse files
camillobrunipthier
authored and
pthier
committed
2021 12 02 fix V8 initialization order (nodejs#133)
Fix initialization order in testrunner
1 parent 29fc0e9 commit b212eb3

File tree

3 files changed

+47
-16
lines changed

3 files changed

+47
-16
lines changed

test/cctest/node_test_fixture.cc

+32
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,39 @@
11
#include "node_test_fixture.h"
2+
#include "cppgc/platform.h"
23

34
ArrayBufferUniquePtr NodeZeroIsolateTestFixture::allocator{nullptr, nullptr};
45
uv_loop_t NodeZeroIsolateTestFixture::current_loop;
56
NodePlatformUniquePtr NodeZeroIsolateTestFixture::platform;
67
TracingAgentUniquePtr NodeZeroIsolateTestFixture::tracing_agent;
78
bool NodeZeroIsolateTestFixture::node_initialized = false;
9+
10+
11+
void NodeTestEnvironment::SetUp() {
12+
NodeZeroIsolateTestFixture::tracing_agent =
13+
std::make_unique<node::tracing::Agent>();
14+
node::tracing::TraceEventHelper::SetAgent(
15+
NodeZeroIsolateTestFixture::tracing_agent.get());
16+
node::tracing::TracingController* tracing_controller =
17+
NodeZeroIsolateTestFixture::tracing_agent->GetTracingController();
18+
static constexpr int kV8ThreadPoolSize = 4;
19+
NodeZeroIsolateTestFixture::platform.reset(
20+
new node::NodePlatform(kV8ThreadPoolSize, tracing_controller));
21+
v8::V8::InitializePlatform(NodeZeroIsolateTestFixture::platform.get());
22+
#ifdef V8_VIRTUAL_MEMORY_CAGE
23+
ASSERT_TRUE(v8::V8::InitializeVirtualMemoryCage());
24+
#endif
25+
cppgc::InitializeProcess(
26+
NodeZeroIsolateTestFixture::platform->GetPageAllocator());
27+
v8::V8::Initialize();
28+
}
29+
30+
void NodeTestEnvironment::TearDown() {
31+
v8::V8::Dispose();
32+
v8::V8::ShutdownPlatform();
33+
NodeZeroIsolateTestFixture::platform->Shutdown();
34+
NodeZeroIsolateTestFixture::platform.reset(nullptr);
35+
NodeZeroIsolateTestFixture::tracing_agent.reset(nullptr);
36+
}
37+
38+
::testing::Environment* const node_env =
39+
::testing::AddGlobalTestEnvironment(new NodeTestEnvironment());

test/cctest/node_test_fixture.h

+14-16
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,26 @@ using ArrayBufferUniquePtr = std::unique_ptr<node::ArrayBufferAllocator,
6060
using TracingAgentUniquePtr = std::unique_ptr<node::tracing::Agent>;
6161
using NodePlatformUniquePtr = std::unique_ptr<node::NodePlatform>;
6262

63+
class NodeTestEnvironment final : public ::testing::Environment {
64+
public:
65+
NodeTestEnvironment() = default;
66+
void SetUp() override;
67+
void TearDown() override;
68+
};
69+
70+
6371
class NodeZeroIsolateTestFixture : public ::testing::Test {
6472
protected:
65-
static ArrayBufferUniquePtr allocator;
66-
static TracingAgentUniquePtr tracing_agent;
67-
static NodePlatformUniquePtr platform;
6873
static uv_loop_t current_loop;
6974
static bool node_initialized;
75+
static ArrayBufferUniquePtr allocator;
76+
static NodePlatformUniquePtr platform;
77+
static TracingAgentUniquePtr tracing_agent;
7078

7179
static void SetUpTestCase() {
7280
if (!node_initialized) {
73-
uv_os_unsetenv("NODE_OPTIONS");
7481
node_initialized = true;
82+
uv_os_unsetenv("NODE_OPTIONS");
7583
std::vector<std::string> argv { "cctest" };
7684
std::vector<std::string> exec_argv;
7785
std::vector<std::string> errors;
@@ -80,32 +88,22 @@ class NodeZeroIsolateTestFixture : public ::testing::Test {
8088
CHECK_EQ(exitcode, 0);
8189
CHECK(errors.empty());
8290
}
83-
84-
tracing_agent = std::make_unique<node::tracing::Agent>();
85-
node::tracing::TraceEventHelper::SetAgent(tracing_agent.get());
86-
node::tracing::TracingController* tracing_controller =
87-
tracing_agent->GetTracingController();
8891
CHECK_EQ(0, uv_loop_init(&current_loop));
89-
static constexpr int kV8ThreadPoolSize = 4;
90-
platform.reset(
91-
new node::NodePlatform(kV8ThreadPoolSize, tracing_controller));
92-
v8::V8::InitializePlatform(platform.get());
93-
v8::V8::Initialize();
9492
}
9593

9694
static void TearDownTestCase() {
97-
platform->Shutdown();
9895
while (uv_loop_alive(&current_loop)) {
9996
uv_run(&current_loop, UV_RUN_ONCE);
10097
}
101-
v8::V8::DisposePlatform();
10298
CHECK_EQ(0, uv_loop_close(&current_loop));
10399
}
104100

105101
void SetUp() override {
106102
allocator = ArrayBufferUniquePtr(node::CreateArrayBufferAllocator(),
107103
&node::FreeArrayBufferAllocator);
108104
}
105+
106+
friend NodeTestEnvironment;
109107
};
110108

111109

tools/code_cache/mkcodecache.cc

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ int main(int argc, char* argv[]) {
6868
}
6969
isolate->Dispose();
7070

71+
v8::V8::Dispose();
7172
v8::V8::DisposePlatform();
7273
return 0;
7374
}

0 commit comments

Comments
 (0)