Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
tools,test: fix V8 initialization order
  • Loading branch information
camillobruni authored and targos committed Apr 12, 2022
commit a398c245359397382e326076d71150bd0fc46039
32 changes: 32 additions & 0 deletions test/cctest/node_test_fixture.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,39 @@
#include "node_test_fixture.h"
#include "cppgc/platform.h"

ArrayBufferUniquePtr NodeZeroIsolateTestFixture::allocator{nullptr, nullptr};
uv_loop_t NodeZeroIsolateTestFixture::current_loop;
NodePlatformUniquePtr NodeZeroIsolateTestFixture::platform;
TracingAgentUniquePtr NodeZeroIsolateTestFixture::tracing_agent;
bool NodeZeroIsolateTestFixture::node_initialized = false;


void NodeTestEnvironment::SetUp() {
NodeZeroIsolateTestFixture::tracing_agent =
std::make_unique<node::tracing::Agent>();
node::tracing::TraceEventHelper::SetAgent(
NodeZeroIsolateTestFixture::tracing_agent.get());
node::tracing::TracingController* tracing_controller =
NodeZeroIsolateTestFixture::tracing_agent->GetTracingController();
static constexpr int kV8ThreadPoolSize = 4;
NodeZeroIsolateTestFixture::platform.reset(
new node::NodePlatform(kV8ThreadPoolSize, tracing_controller));
v8::V8::InitializePlatform(NodeZeroIsolateTestFixture::platform.get());
#ifdef V8_SANDBOX
ASSERT_TRUE(v8::V8::InitializeSandbox());
#endif
cppgc::InitializeProcess(
NodeZeroIsolateTestFixture::platform->GetPageAllocator());
v8::V8::Initialize();
}

void NodeTestEnvironment::TearDown() {
v8::V8::Dispose();
v8::V8::DisposePlatform();
NodeZeroIsolateTestFixture::platform->Shutdown();
NodeZeroIsolateTestFixture::platform.reset(nullptr);
NodeZeroIsolateTestFixture::tracing_agent.reset(nullptr);
}

::testing::Environment* const node_env =
::testing::AddGlobalTestEnvironment(new NodeTestEnvironment());
30 changes: 14 additions & 16 deletions test/cctest/node_test_fixture.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,26 @@ using ArrayBufferUniquePtr = std::unique_ptr<node::ArrayBufferAllocator,
using TracingAgentUniquePtr = std::unique_ptr<node::tracing::Agent>;
using NodePlatformUniquePtr = std::unique_ptr<node::NodePlatform>;

class NodeTestEnvironment final : public ::testing::Environment {
public:
NodeTestEnvironment() = default;
void SetUp() override;
void TearDown() override;
};


class NodeZeroIsolateTestFixture : public ::testing::Test {
protected:
static ArrayBufferUniquePtr allocator;
static TracingAgentUniquePtr tracing_agent;
static NodePlatformUniquePtr platform;
static uv_loop_t current_loop;
static bool node_initialized;
static ArrayBufferUniquePtr allocator;
static NodePlatformUniquePtr platform;
static TracingAgentUniquePtr tracing_agent;

static void SetUpTestCase() {
if (!node_initialized) {
uv_os_unsetenv("NODE_OPTIONS");
node_initialized = true;
uv_os_unsetenv("NODE_OPTIONS");
std::vector<std::string> argv { "cctest" };
std::vector<std::string> exec_argv;
std::vector<std::string> errors;
Expand All @@ -80,32 +88,22 @@ class NodeZeroIsolateTestFixture : public ::testing::Test {
CHECK_EQ(exitcode, 0);
CHECK(errors.empty());
}

tracing_agent = std::make_unique<node::tracing::Agent>();
node::tracing::TraceEventHelper::SetAgent(tracing_agent.get());
node::tracing::TracingController* tracing_controller =
tracing_agent->GetTracingController();
CHECK_EQ(0, uv_loop_init(&current_loop));
static constexpr int kV8ThreadPoolSize = 4;
platform.reset(
new node::NodePlatform(kV8ThreadPoolSize, tracing_controller));
v8::V8::InitializePlatform(platform.get());
v8::V8::Initialize();
}

static void TearDownTestCase() {
platform->Shutdown();
while (uv_loop_alive(&current_loop)) {
uv_run(&current_loop, UV_RUN_ONCE);
}
v8::V8::DisposePlatform();
CHECK_EQ(0, uv_loop_close(&current_loop));
}

void SetUp() override {
allocator = ArrayBufferUniquePtr(node::CreateArrayBufferAllocator(),
&node::FreeArrayBufferAllocator);
}

friend NodeTestEnvironment;
};


Expand Down
1 change: 1 addition & 0 deletions tools/code_cache/mkcodecache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ int main(int argc, char* argv[]) {
}
isolate->Dispose();

v8::V8::Dispose();
v8::V8::DisposePlatform();
return 0;
}