1'use strict';
2if (process.argv[2] === 'child') {
3  const common = require('../../common');
4  // Trying, catching the exception, and finding the bindings at the `Error`'s
5  // `binding` property is done intentionally, because we're also testing what
6  // happens when the add-on entry point throws. See test.js.
7  try {
8    require(`./build/${common.buildType}/test_exception`);
9  } catch (anException) {
10    anException.binding.createExternal();
11  }
12
13  // Collect garbage 10 times. At least one of those should throw the exception
14  // and cause the whole process to bail with it, its text printed to stderr and
15  // asserted by the parent process to match expectations.
16  let gcCount = 10;
17  (function gcLoop() {
18    global.gc();
19    if (--gcCount > 0) {
20      setImmediate(() => gcLoop());
21    }
22  })();
23  return;
24}
25
26const assert = require('assert');
27const { spawnSync } = require('child_process');
28const child = spawnSync(process.execPath, [
29  '--expose-gc', __filename, 'child',
30]);
31assert.strictEqual(child.signal, null);
32assert.match(child.stderr.toString(), /Error during Finalize/);
33