11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst path = require('path');
61cb0ef41Sopenharmony_ciconst spawnSync = require('child_process').spawnSync;
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst binding = path.resolve(__dirname, `./build/${common.buildType}/binding`);
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ciObject.defineProperty(globalThis, 'interrupt', {
111cb0ef41Sopenharmony_ci  get: () => {
121cb0ef41Sopenharmony_ci    return null;
131cb0ef41Sopenharmony_ci  },
141cb0ef41Sopenharmony_ci  set: () => {
151cb0ef41Sopenharmony_ci    throw new Error('should not calling into js');
161cb0ef41Sopenharmony_ci  },
171cb0ef41Sopenharmony_ci});
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciif (process.argv[2] === 'child-busyloop') {
201cb0ef41Sopenharmony_ci  (function childMain() {
211cb0ef41Sopenharmony_ci    const addon = require(binding);
221cb0ef41Sopenharmony_ci    addon[process.argv[3]]();
231cb0ef41Sopenharmony_ci    while (true) {
241cb0ef41Sopenharmony_ci      /** wait for interrupt */
251cb0ef41Sopenharmony_ci    }
261cb0ef41Sopenharmony_ci  })();
271cb0ef41Sopenharmony_ci  return;
281cb0ef41Sopenharmony_ci}
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ciif (process.argv[2] === 'child-idle') {
311cb0ef41Sopenharmony_ci  (function childMain() {
321cb0ef41Sopenharmony_ci    const addon = require(binding);
331cb0ef41Sopenharmony_ci    addon[process.argv[3]]();
341cb0ef41Sopenharmony_ci    // wait for interrupt
351cb0ef41Sopenharmony_ci    setTimeout(() => {}, 10_000_000);
361cb0ef41Sopenharmony_ci  })();
371cb0ef41Sopenharmony_ci  return;
381cb0ef41Sopenharmony_ci}
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_cifor (const type of ['busyloop', 'idle']) {
411cb0ef41Sopenharmony_ci  {
421cb0ef41Sopenharmony_ci    const child = spawnSync(process.execPath, [ __filename, `child-${type}`, 'scheduleInterrupt' ]);
431cb0ef41Sopenharmony_ci    assert.strictEqual(child.status, 0, `${type} should exit with code 0`);
441cb0ef41Sopenharmony_ci  }
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  {
471cb0ef41Sopenharmony_ci    const child = spawnSync(process.execPath, [ __filename, `child-${type}`, 'ScheduleInterruptWithJS' ]);
481cb0ef41Sopenharmony_ci    assert(common.nodeProcessAborted(child.status, child.signal));
491cb0ef41Sopenharmony_ci  }
501cb0ef41Sopenharmony_ci}
51