Skip to content

Commit

Permalink
add AddPostFinalizer test
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinEady committed Jun 20, 2024
1 parent 7c949dd commit fc5dc94
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
45 changes: 45 additions & 0 deletions test/finalizer_order.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,56 @@ Napi::Value IsExternalGcFinalizerCalled(const Napi::CallbackInfo& info) {
return Napi::Boolean::New(info.Env(), externalGcFinalizerCalled);
}

Napi::Value AddPostFinalizer(const Napi::CallbackInfo& info) {
auto env = info.Env();

env.AddPostFinalizer(
[callback = Napi::Persistent(info[0].As<Napi::Function>())](
Napi::Env /*env*/) { callback.Call({}); });

return env.Undefined();
}

Napi::Value AddPostFinalizerWithData(const Napi::CallbackInfo& info) {
auto env = info.Env();

env.AddPostFinalizer(
[callback = Napi::Persistent(info[0].As<Napi::Function>())](
Napi::Env /*env*/, Napi::Reference<Napi::Value>* data) {
callback.Call({data->Value()});
delete data;
},
new Napi::Reference<Napi::Value>(Napi::Persistent(info[1])));
return env.Undefined();
}

Napi::Value AddPostFinalizerWithDataAndHint(const Napi::CallbackInfo& info) {
auto env = info.Env();

env.AddPostFinalizer(
[callback = Napi::Persistent(info[0].As<Napi::Function>())](
Napi::Env /*env*/,
Napi::Reference<Napi::Value>* data,
Napi::Reference<Napi::Value>* hint) {
callback.Call({data->Value(), hint->Value()});
delete data;
delete hint;
},
new Napi::Reference<Napi::Value>(Napi::Persistent(info[1])),
new Napi::Reference<Napi::Value>(Napi::Persistent(info[2])));
return env.Undefined();
}

} // namespace

Napi::Object InitFinalizerOrder(Napi::Env env) {
Napi::Object exports = Napi::Object::New(env);
Test::Initialize(env, exports);
exports["AddPostFinalizer"] = Napi::Function::New(env, AddPostFinalizer);
exports["AddPostFinalizerWithData"] =
Napi::Function::New(env, AddPostFinalizerWithData);
exports["AddPostFinalizerWithDataAndHint"] =
Napi::Function::New(env, AddPostFinalizerWithDataAndHint);
exports["CreateExternalNogc"] = Napi::Function::New(env, CreateExternalNogc);
exports["CreateExternalGc"] = Napi::Function::New(env, CreateExternalGc);
exports["isExternalNogcFinalizerCalled"] =
Expand Down
36 changes: 34 additions & 2 deletions test/finalizer_order.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/* eslint-disable no-unused-vars */

const assert = require('assert');
const common = require('./common');
const testUtil = require('./testUtil');

module.exports = require('./common').runTest(test);
Expand All @@ -12,7 +13,7 @@ function test (binding) {

let isCallbackCalled = false;

return testUtil.runGCTests([
const tests = [
'Finalizer Order - ObjectWrap',
() => {
let test = new binding.finalizer_order.Test(() => { isCallbackCalled = true; });
Expand Down Expand Up @@ -62,5 +63,36 @@ function test (binding) {
() => {
assert.strictEqual(binding.finalizer_order.isExternalGcFinalizerCalled(), true, 'Expected External gc finalizer to be called [after ticking]');
}
]);
];

if (binding.isExperimental) {
tests.push(...[
'AddPostFinalizer',
() => {
binding.finalizer_order.AddPostFinalizer(common.mustCall());
},

'AddPostFinalizerWithData',
() => {
const data = {};
const callback = (callbackData) => {
assert.strictEqual(callbackData, data);
};
binding.finalizer_order.AddPostFinalizerWithData(common.mustCall(callback), data);
},

'AddPostFinalizerWithDataAndHint',
() => {
const data = {};
const hint = {};
const callback = (callbackData, callbackHint) => {
assert.strictEqual(callbackData, data);
assert.strictEqual(callbackHint, hint);
};
binding.finalizer_order.AddPostFinalizerWithDataAndHint(common.mustCall(callback), data, hint);
}
]);
}

return testUtil.runGCTests(tests);
}

0 comments on commit fc5dc94

Please sign in to comment.