11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst assert = require('assert');
41cb0ef41Sopenharmony_ciconst exec = require('child_process').exec;
51cb0ef41Sopenharmony_ciconst { promisify } = require('util');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst execPromisifed = promisify(exec);
81cb0ef41Sopenharmony_ciconst invalidArgTypeError = {
91cb0ef41Sopenharmony_ci  code: 'ERR_INVALID_ARG_TYPE',
101cb0ef41Sopenharmony_ci  name: 'TypeError'
111cb0ef41Sopenharmony_ci};
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst waitCommand = common.isWindows ?
141cb0ef41Sopenharmony_ci  `${process.execPath} -e "setInterval(()=>{}, 99)"` :
151cb0ef41Sopenharmony_ci  'sleep 2m';
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci{
181cb0ef41Sopenharmony_ci  const ac = new AbortController();
191cb0ef41Sopenharmony_ci  const signal = ac.signal;
201cb0ef41Sopenharmony_ci  const promise = execPromisifed(waitCommand, { signal });
211cb0ef41Sopenharmony_ci  assert.rejects(promise, {
221cb0ef41Sopenharmony_ci    name: 'AbortError',
231cb0ef41Sopenharmony_ci    cause: new DOMException('This operation was aborted', 'AbortError'),
241cb0ef41Sopenharmony_ci  }).then(common.mustCall());
251cb0ef41Sopenharmony_ci  ac.abort();
261cb0ef41Sopenharmony_ci}
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci{
291cb0ef41Sopenharmony_ci  const err = new Error('boom');
301cb0ef41Sopenharmony_ci  const ac = new AbortController();
311cb0ef41Sopenharmony_ci  const signal = ac.signal;
321cb0ef41Sopenharmony_ci  const promise = execPromisifed(waitCommand, { signal });
331cb0ef41Sopenharmony_ci  assert.rejects(promise, {
341cb0ef41Sopenharmony_ci    name: 'AbortError',
351cb0ef41Sopenharmony_ci    cause: err
361cb0ef41Sopenharmony_ci  }).then(common.mustCall());
371cb0ef41Sopenharmony_ci  ac.abort(err);
381cb0ef41Sopenharmony_ci}
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci{
411cb0ef41Sopenharmony_ci  const ac = new AbortController();
421cb0ef41Sopenharmony_ci  const signal = ac.signal;
431cb0ef41Sopenharmony_ci  const promise = execPromisifed(waitCommand, { signal });
441cb0ef41Sopenharmony_ci  assert.rejects(promise, {
451cb0ef41Sopenharmony_ci    name: 'AbortError',
461cb0ef41Sopenharmony_ci    cause: 'boom'
471cb0ef41Sopenharmony_ci  }).then(common.mustCall());
481cb0ef41Sopenharmony_ci  ac.abort('boom');
491cb0ef41Sopenharmony_ci}
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ci{
521cb0ef41Sopenharmony_ci  assert.throws(() => {
531cb0ef41Sopenharmony_ci    execPromisifed(waitCommand, { signal: {} });
541cb0ef41Sopenharmony_ci  }, invalidArgTypeError);
551cb0ef41Sopenharmony_ci}
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci{
581cb0ef41Sopenharmony_ci  function signal() {}
591cb0ef41Sopenharmony_ci  assert.throws(() => {
601cb0ef41Sopenharmony_ci    execPromisifed(waitCommand, { signal });
611cb0ef41Sopenharmony_ci  }, invalidArgTypeError);
621cb0ef41Sopenharmony_ci}
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci{
651cb0ef41Sopenharmony_ci  const signal = AbortSignal.abort(); // Abort in advance
661cb0ef41Sopenharmony_ci  const promise = execPromisifed(waitCommand, { signal });
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci  assert.rejects(promise, { name: 'AbortError' })
691cb0ef41Sopenharmony_ci        .then(common.mustCall());
701cb0ef41Sopenharmony_ci}
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci{
731cb0ef41Sopenharmony_ci  const err = new Error('boom');
741cb0ef41Sopenharmony_ci  const signal = AbortSignal.abort(err); // Abort in advance
751cb0ef41Sopenharmony_ci  const promise = execPromisifed(waitCommand, { signal });
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci  assert.rejects(promise, { name: 'AbortError', cause: err })
781cb0ef41Sopenharmony_ci        .then(common.mustCall());
791cb0ef41Sopenharmony_ci}
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci{
821cb0ef41Sopenharmony_ci  const signal = AbortSignal.abort('boom'); // Abort in advance
831cb0ef41Sopenharmony_ci  const promise = execPromisifed(waitCommand, { signal });
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci  assert.rejects(promise, { name: 'AbortError', cause: 'boom' })
861cb0ef41Sopenharmony_ci        .then(common.mustCall());
871cb0ef41Sopenharmony_ci}
88