From d9ce2ae14cfb6c0a808a020b499dbcde6e3cfb0f Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Mon, 6 Nov 2023 18:39:10 +0100 Subject: [PATCH] Modify the GC test so that it fails as expected --- api/node/__test__/api.spec.ts | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/api/node/__test__/api.spec.ts b/api/node/__test__/api.spec.ts index 3df57f5753a..9dcfc2d2285 100644 --- a/api/node/__test__/api.spec.ts +++ b/api/node/__test__/api.spec.ts @@ -67,11 +67,9 @@ test('callback closure cyclic references do not prevent GC', async (t) => { // Setup: // A component instance with a callback installed from JS: - // * The callback captures the surrounding environment, which - // includes an extra reference to the component instance itself - // --> a cyclic reference - // * Invoking the callback clears the reference in the outer captured - // environment. + // The callback captures the surrounding environment, which + // includes an extra reference to the component instance itself + // --> a cyclic reference // // Note: WeakRef's deref is used to observe the GC. This means that we must // separate the test into different jobs with await, to permit for collection. @@ -81,13 +79,16 @@ test('callback closure cyclic references do not prevent GC', async (t) => { let demo = new demo_module.Test(); t.is(demo.check, "initial value"); t.true(Object.hasOwn(demo, "say_hello")); - let callback_invoked = false; + let demo_weak = new WeakRef(demo); - demo.say_hello = () => { - demo = null; - callback_invoked = true; - }; + function scope() { + let copy = demo; + copy.say_hello = () => { + console.log(copy.check); + }; + } + scope(); t.true(demo_weak.deref() !== undefined); @@ -98,10 +99,8 @@ test('callback closure cyclic references do not prevent GC', async (t) => { t.true(demo_weak.deref() !== undefined); - // Invoke the callback, to clear "demo" - demo.say_hello(); - t.true(callback_invoked); - t.true(demo === null); + // Clear the strong reference here + demo = null; // After the this GC call, the instance should have been collected. Strong references // in Rust should not keep it alive.