Skip to content

Commit 25ce5c3

Browse files
danbevrvagg
authored andcommitted
test: introduce SetUpTestCase/TearDownTestCase
This commit add SetUpTestCase and TearDownTestCase functions that will be called once per test case. Currently we only have SetUp/TearDown which are called for each test. This commit moves the initialization and configuration of Node and V8 to be done on a per test case basis, but gives each test a new Isolate. PR-URL: #18558 Reviewed-By: Ben Noordhuis <[email protected]>
1 parent e98efcf commit 25ce5c3

File tree

2 files changed

+31
-30
lines changed

2 files changed

+31
-30
lines changed

test/cctest/node_test_fixture.cc

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
#include "node_test_fixture.h"
22

3-
uv_loop_t current_loop;
3+
uv_loop_t NodeTestFixture::current_loop;
4+
std::unique_ptr<node::NodePlatform> NodeTestFixture::platform;
5+
std::unique_ptr<v8::ArrayBuffer::Allocator> NodeTestFixture::allocator;
6+
std::unique_ptr<v8::TracingController> NodeTestFixture::tracing_controller;
7+
v8::Isolate::CreateParams NodeTestFixture::params;

test/cctest/node_test_fixture.h

+26-29
Original file line numberDiff line numberDiff line change
@@ -53,49 +53,46 @@ struct Argv {
5353
int nr_args_;
5454
};
5555

56-
extern uv_loop_t current_loop;
5756

5857
class NodeTestFixture : public ::testing::Test {
59-
public:
60-
static uv_loop_t* CurrentLoop() { return &current_loop; }
61-
62-
node::MultiIsolatePlatform* Platform() const { return platform_; }
63-
6458
protected:
59+
static std::unique_ptr<v8::ArrayBuffer::Allocator> allocator;
60+
static std::unique_ptr<v8::TracingController> tracing_controller;
61+
static std::unique_ptr<node::NodePlatform> platform;
62+
static v8::Isolate::CreateParams params;
63+
static uv_loop_t current_loop;
6564
v8::Isolate* isolate_;
6665

67-
virtual void SetUp() {
66+
static void SetUpTestCase() {
67+
platform.reset(new node::NodePlatform(4, nullptr));
68+
tracing_controller.reset(new v8::TracingController());
69+
allocator.reset(v8::ArrayBuffer::Allocator::NewDefaultAllocator());
70+
params.array_buffer_allocator = allocator.get();
71+
node::tracing::TraceEventHelper::SetTracingController(
72+
tracing_controller.get());
6873
CHECK_EQ(0, uv_loop_init(&current_loop));
69-
platform_ = new node::NodePlatform(8, nullptr);
70-
v8::V8::InitializePlatform(platform_);
74+
v8::V8::InitializePlatform(platform.get());
7175
v8::V8::Initialize();
72-
v8::Isolate::CreateParams params_;
73-
params_.array_buffer_allocator = allocator_.get();
74-
isolate_ = v8::Isolate::New(params_);
75-
76-
// As the TracingController is stored globally, we only need to create it
77-
// one time for all tests.
78-
if (node::tracing::TraceEventHelper::GetTracingController() == nullptr) {
79-
node::tracing::TraceEventHelper::SetTracingController(
80-
new v8::TracingController());
81-
}
8276
}
8377

84-
virtual void TearDown() {
85-
platform_->Shutdown();
78+
static void TearDownTestCase() {
79+
platform->Shutdown();
8680
while (uv_loop_alive(&current_loop)) {
8781
uv_run(&current_loop, UV_RUN_ONCE);
8882
}
8983
v8::V8::ShutdownPlatform();
90-
delete platform_;
91-
platform_ = nullptr;
9284
CHECK_EQ(0, uv_loop_close(&current_loop));
9385
}
9486

95-
private:
96-
node::NodePlatform* platform_ = nullptr;
97-
std::unique_ptr<v8::ArrayBuffer::Allocator> allocator_{
98-
v8::ArrayBuffer::Allocator::NewDefaultAllocator()};
87+
virtual void SetUp() {
88+
isolate_ = v8::Isolate::New(params);
89+
CHECK_NE(isolate_, nullptr);
90+
}
91+
92+
virtual void TearDown() {
93+
isolate_->Dispose();
94+
isolate_ = nullptr;
95+
}
9996
};
10097

10198

@@ -112,8 +109,8 @@ class EnvironmentTestFixture : public NodeTestFixture {
112109
context_->Enter();
113110

114111
isolate_data_ = node::CreateIsolateData(isolate,
115-
NodeTestFixture::CurrentLoop(),
116-
test_fixture->Platform());
112+
&NodeTestFixture::current_loop,
113+
platform.get());
117114
CHECK_NE(nullptr, isolate_data_);
118115
environment_ = node::CreateEnvironment(isolate_data_,
119116
context_,

0 commit comments

Comments
 (0)