11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// Test exec() with a timeout that expires.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst common = require('../common');
61cb0ef41Sopenharmony_ciconst assert = require('assert');
71cb0ef41Sopenharmony_ciconst cp = require('child_process');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciconst {
101cb0ef41Sopenharmony_ci  cleanupStaleProcess,
111cb0ef41Sopenharmony_ci  logAfterTime,
121cb0ef41Sopenharmony_ci  kExpiringChildRunTime,
131cb0ef41Sopenharmony_ci  kExpiringParentTimer
141cb0ef41Sopenharmony_ci} = require('../common/child_process');
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciif (process.argv[2] === 'child') {
171cb0ef41Sopenharmony_ci  logAfterTime(kExpiringChildRunTime);
181cb0ef41Sopenharmony_ci  return;
191cb0ef41Sopenharmony_ci}
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciconst cmd = `"${process.execPath}" "${__filename}" child`;
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_cicp.exec(cmd, {
241cb0ef41Sopenharmony_ci  timeout: kExpiringParentTimer,
251cb0ef41Sopenharmony_ci}, common.mustCall((err, stdout, stderr) => {
261cb0ef41Sopenharmony_ci  console.log('[stdout]', stdout.trim());
271cb0ef41Sopenharmony_ci  console.log('[stderr]', stderr.trim());
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci  let sigterm = 'SIGTERM';
301cb0ef41Sopenharmony_ci  assert.strictEqual(err.killed, true);
311cb0ef41Sopenharmony_ci  // TODO OpenBSD returns a null signal and 143 for code
321cb0ef41Sopenharmony_ci  if (common.isOpenBSD) {
331cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, 143);
341cb0ef41Sopenharmony_ci    sigterm = null;
351cb0ef41Sopenharmony_ci  } else {
361cb0ef41Sopenharmony_ci    assert.strictEqual(err.code, null);
371cb0ef41Sopenharmony_ci  }
381cb0ef41Sopenharmony_ci  // At least starting with Darwin Kernel Version 16.4.0, sending a SIGTERM to a
391cb0ef41Sopenharmony_ci  // process that is still starting up kills it with SIGKILL instead of SIGTERM.
401cb0ef41Sopenharmony_ci  // See: https://github.com/libuv/libuv/issues/1226
411cb0ef41Sopenharmony_ci  if (common.isOSX)
421cb0ef41Sopenharmony_ci    assert.ok(err.signal === 'SIGTERM' || err.signal === 'SIGKILL');
431cb0ef41Sopenharmony_ci  else
441cb0ef41Sopenharmony_ci    assert.strictEqual(err.signal, sigterm);
451cb0ef41Sopenharmony_ci  assert.strictEqual(err.cmd, cmd);
461cb0ef41Sopenharmony_ci  assert.strictEqual(stdout.trim(), '');
471cb0ef41Sopenharmony_ci  assert.strictEqual(stderr.trim(), '');
481cb0ef41Sopenharmony_ci}));
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_cicleanupStaleProcess(__filename);
51