• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2// Test API calls for instance data.
3
4const common = require('../../common');
5
6if (module !== require.main) {
7  // When required as a module, run the tests.
8  const test_instance_data =
9    require(`./build/${common.buildType}/test_instance_data`);
10
11  // Test that instance data can be used in an async work callback.
12  new Promise((resolve) => test_instance_data.asyncWorkCallback(resolve))
13
14    // Test that the buffer finalizer can access the instance data.
15    .then(() => new Promise((resolve) => {
16      test_instance_data.testBufferFinalizer(resolve);
17      global.gc();
18    }))
19
20    // Test that the thread-safe function can access the instance data.
21    .then(() => new Promise((resolve) =>
22      test_instance_data.testThreadsafeFunction(common.mustCall(),
23                                                common.mustCall(resolve))));
24} else {
25  // When launched as a script, run tests in either a child process or in a
26  // worker thread.
27  const assert = require('assert');
28  const requireAs = require('../../common/require-as');
29  const runOptions = { stdio: ['inherit', 'pipe', 'inherit'] };
30  const { spawnSync } = require('child_process');
31
32  // Run tests in a child process.
33  requireAs(__filename, ['--expose-gc'], runOptions, 'child');
34
35  // Run tests in a worker thread in a child process.
36  requireAs(__filename, ['--expose-gc'], runOptions, 'worker');
37
38  function testProcessExit(addonName) {
39    // Make sure that process exit is clean when the instance data has
40    // references to JS objects.
41    const path = require
42      .resolve(`./build/${common.buildType}/${addonName}`)
43      // Replace any backslashes with double backslashes because they'll be re-
44      // interpreted back to single backslashes in the command line argument
45      // to the child process. Windows needs this.
46      .replace(/\\/g, '\\\\');
47    const child = spawnSync(process.execPath, ['-e', `require('${path}');`]);
48    assert.strictEqual(child.signal, null);
49    assert.strictEqual(child.status, 0);
50    assert.strictEqual(child.stderr.toString(), 'addon_free');
51  }
52
53  testProcessExit('test_ref_then_set');
54  testProcessExit('test_set_then_ref');
55}
56