1'use strict';
2const common = require('../../common');
3
4if (process.argv[2] === 'child') {
5  const test_finalizer = require(`./build/${common.buildType}/test_finalizer`);
6
7  (() => {
8    const obj = {};
9    test_finalizer.addFinalizerFailOnJS(obj);
10  })();
11
12  // Collect garbage 10 times. At least one of those should throw the exception
13  // and cause the whole process to bail with it, its text printed to stderr and
14  // asserted by the parent process to match expectations.
15  let gcCount = 10;
16  (function gcLoop() {
17    global.gc();
18    if (--gcCount > 0) {
19      setImmediate(() => gcLoop());
20    }
21  })();
22  return;
23}
24
25const assert = require('assert');
26const { spawnSync } = require('child_process');
27const child = spawnSync(process.execPath, [
28  '--expose-gc', __filename, 'child',
29]);
30assert.strictEqual(child.signal, common.isWindows ? null : 'SIGABRT');
31assert.match(child.stderr.toString(), /Finalizer is calling a function that may affect GC state/);
32