|
| 1 | +#include "base_object-inl.h" |
| 2 | +#include "node_binding.h" |
| 3 | +#include "node_test_fixture.h" |
| 4 | +#include "node_v8.h" |
| 5 | + |
| 6 | +#include <string> |
| 7 | + |
| 8 | +using node::v8_utils::GCProfiler; |
| 9 | + |
| 10 | +namespace { |
| 11 | + |
| 12 | +v8::Local<v8::Value> Get(v8::Local<v8::Context> context, |
| 13 | + v8::Local<v8::Object> object, |
| 14 | + const char* name) { |
| 15 | + v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 16 | + return object |
| 17 | + ->Get(context, v8::String::NewFromUtf8(isolate, name).ToLocalChecked()) |
| 18 | + .ToLocalChecked(); |
| 19 | +} |
| 20 | + |
| 21 | +void CallMethod(v8::Local<v8::Context> context, |
| 22 | + v8::Local<v8::Object> object, |
| 23 | + const char* name) { |
| 24 | + Get(context, object, name) |
| 25 | + .As<v8::Function>() |
| 26 | + ->Call(context, object, 0, nullptr) |
| 27 | + .ToLocalChecked(); |
| 28 | +} |
| 29 | + |
| 30 | +class GCProfilerTest : public EnvironmentTestFixture {}; |
| 31 | + |
| 32 | +// Refs: https://github.com/nodejs/node/issues/67790 |
| 33 | +TEST_F(GCProfilerTest, StopsRecordingAfterStop) { |
| 34 | + const v8::HandleScope handle_scope(isolate_); |
| 35 | + const Argv argv; |
| 36 | + Env test_env{handle_scope, argv}; |
| 37 | + v8::Local<v8::Context> context = isolate_->GetCurrentContext(); |
| 38 | + node::LoadEnvironment(*test_env, ""); |
| 39 | + |
| 40 | + v8::Local<v8::Function> get_internal_binding = |
| 41 | + v8::Function::New(context, node::binding::GetInternalBinding) |
| 42 | + .ToLocalChecked(); |
| 43 | + v8::Local<v8::Value> binding_name = |
| 44 | + v8::String::NewFromUtf8Literal(isolate_, "v8"); |
| 45 | + v8::Local<v8::Object> binding = |
| 46 | + get_internal_binding |
| 47 | + ->Call(context, v8::Undefined(isolate_), 1, &binding_name) |
| 48 | + .ToLocalChecked() |
| 49 | + .As<v8::Object>(); |
| 50 | + |
| 51 | + v8::Local<v8::Object> object = Get(context, binding, "GCProfiler") |
| 52 | + .As<v8::Function>() |
| 53 | + ->NewInstance(context) |
| 54 | + .ToLocalChecked(); |
| 55 | + GCProfiler* profiler = node::BaseObject::Unwrap<GCProfiler>(object); |
| 56 | + ASSERT_NE(profiler, nullptr); |
| 57 | + |
| 58 | + CallMethod(context, object, "start"); |
| 59 | + isolate_->LowMemoryNotification(); |
| 60 | + CallMethod(context, object, "stop"); |
| 61 | + const size_t profile_size = profiler->out_stream()->str().size(); |
| 62 | + EXPECT_NE(profiler->out_stream()->str().find("\"gcType\""), |
| 63 | + std::string::npos); |
| 64 | + |
| 65 | + // `object` is still reachable here, so the profiler is not destroyed and |
| 66 | + // only stop() can have removed its GC callbacks. |
| 67 | + isolate_->LowMemoryNotification(); |
| 68 | + EXPECT_EQ(profiler->out_stream()->str().size(), profile_size); |
| 69 | +} |
| 70 | + |
| 71 | +} // namespace |
0 commit comments