File: test.js

package info (click to toggle)
nodejs 20.19.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 219,072 kB
  • sloc: cpp: 1,277,408; javascript: 565,332; ansic: 129,476; python: 58,536; sh: 3,841; makefile: 2,725; asm: 1,732; perl: 248; lisp: 222; xml: 42
file content (45 lines) | stat: -rw-r--r-- 1,538 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
'use strict';
// Flags: --expose-gc

const common = require('../../common');
const test_finalizer = require(`./build/${common.buildType}/test_finalizer`);
const assert = require('assert');

const { gcUntil } = require('../../common/gc');

// The goal of this test is to show that we can run "pure" finalizers in the
// current JS loop tick. Thus, we do not use gcUntil function works
// asynchronously using micro tasks.
// We use IIFE for the obj scope instead of {} to be compatible with
// non-V8 JS engines that do not support scoped variables.
(() => {
  const obj = {};
  test_finalizer.addFinalizer(obj);
})();

for (let i = 0; i < 10; ++i) {
  global.gc();
  if (test_finalizer.getFinalizerCallCount() === 1) {
    break;
  }
}

assert.strictEqual(test_finalizer.getFinalizerCallCount(), 1);

// The finalizer that access JS cannot run synchronously. They are run in the
// next JS loop tick. Thus, we must use gcUntil.
async function runAsyncTests() {
  // We do not use common.mustCall() because we want to see the finalizer
  // called in response to GC and not as a part of env destruction.
  let js_is_called = false;
  // We use IIFE for the obj scope instead of {} to be compatible with
  // non-V8 JS engines that do not support scoped variables.
  (() => {
    const obj = {};
    test_finalizer.addFinalizerWithJS(obj, () => { js_is_called = true; });
  })();
  await gcUntil('ensure JS finalizer called',
                () => (test_finalizer.getFinalizerCallCount() === 2));
  assert(js_is_called);
}
runAsyncTests();