1'use strict';
2
3const common = require('../../common');
4const assert = require('assert');
5const path = require('path');
6const spawnSync = require('child_process').spawnSync;
7
8const binding = path.resolve(__dirname, `./build/${common.buildType}/binding`);
9
10Object.defineProperty(globalThis, 'interrupt', {
11  get: () => {
12    return null;
13  },
14  set: () => {
15    throw new Error('should not calling into js');
16  },
17});
18
19if (process.argv[2] === 'child-busyloop') {
20  (function childMain() {
21    const addon = require(binding);
22    addon[process.argv[3]]();
23    while (true) {
24      /** wait for interrupt */
25    }
26  })();
27  return;
28}
29
30if (process.argv[2] === 'child-idle') {
31  (function childMain() {
32    const addon = require(binding);
33    addon[process.argv[3]]();
34    // wait for interrupt
35    setTimeout(() => {}, 10_000_000);
36  })();
37  return;
38}
39
40for (const type of ['busyloop', 'idle']) {
41  {
42    const child = spawnSync(process.execPath, [ __filename, `child-${type}`, 'scheduleInterrupt' ]);
43    assert.strictEqual(child.status, 0, `${type} should exit with code 0`);
44  }
45
46  {
47    const child = spawnSync(process.execPath, [ __filename, `child-${type}`, 'ScheduleInterruptWithJS' ]);
48    assert(common.nodeProcessAborted(child.status, child.signal));
49  }
50}
51