From ce03977f30b8959817cd4d160e63572d5ea32a58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Fri, 5 May 2017 14:43:54 +0200 Subject: [PATCH] test: fix napi test_reference for recent V8 Backport-PR-URL: https://github.com/nodejs/node/pull/19447 PR-URL: https://github.com/nodejs/node/pull/12864 Ref: https://github.com/nodejs/node/pull/12551#issuecomment-297949361 Reviewed-By: James M Snell Reviewed-By: Michael Dawson --- test/addons-napi/test_reference/test.js | 29 ++++++++++++++----------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/test/addons-napi/test_reference/test.js b/test/addons-napi/test_reference/test.js index ddfec58f1f9d7d..30effe7eec0922 100644 --- a/test/addons-napi/test_reference/test.js +++ b/test/addons-napi/test_reference/test.js @@ -33,19 +33,6 @@ assert.strictEqual(test_reference.finalizeCount, 0); assert.strictEqual(test_reference.finalizeCount, 1); } -{ - // Weak reference - let value = test_reference.createExternalWithFinalize(); - assert.strictEqual(test_reference.finalizeCount, 0); - test_reference.createReference(value, 0); - assert.strictEqual(test_reference.referenceValue, value); - value = null; - global.gc(); // Value should be GC'd because there is only a weak ref - assert.strictEqual(test_reference.referenceValue, undefined); - assert.strictEqual(test_reference.finalizeCount, 1); - test_reference.deleteReference(); -} - { // Strong reference let value = test_reference.createExternalWithFinalize(); @@ -85,3 +72,19 @@ assert.strictEqual(test_reference.finalizeCount, 0); global.gc(); // Value was already GC'd assert.strictEqual(test_reference.finalizeCount, 1); } + +{ + // Weak reference + let value = test_reference.createExternalWithFinalize(); + assert.strictEqual(test_reference.finalizeCount, 0); + test_reference.createReference(value, 0); + assert.strictEqual(test_reference.referenceValue, value); + value = null; + setImmediate(common.mustCall(() => { + // This test only works if gc() is called from an immediate callback. + global.gc(); // Value should be GC'd because there is only a weak ref + assert.strictEqual(test_reference.referenceValue, undefined); + assert.strictEqual(test_reference.finalizeCount, 1); + test_reference.deleteReference(); + })); +}