11cb0ef41Sopenharmony_ci// Flags: --expose-gc
21cb0ef41Sopenharmony_ci'use strict';
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciconst common = require('../common');
51cb0ef41Sopenharmony_ciconst { aborted } = require('util');
61cb0ef41Sopenharmony_ciconst assert = require('assert');
71cb0ef41Sopenharmony_ciconst { getEventListeners } = require('events');
81cb0ef41Sopenharmony_ciconst { spawn } = require('child_process');
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci{
111cb0ef41Sopenharmony_ci  // Test aborted works when provided a resource
121cb0ef41Sopenharmony_ci  const ac = new AbortController();
131cb0ef41Sopenharmony_ci  aborted(ac.signal, {}).then(common.mustCall());
141cb0ef41Sopenharmony_ci  ac.abort();
151cb0ef41Sopenharmony_ci  assert.strictEqual(ac.signal.aborted, true);
161cb0ef41Sopenharmony_ci  assert.strictEqual(getEventListeners(ac.signal, 'abort').length, 0);
171cb0ef41Sopenharmony_ci}
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci{
201cb0ef41Sopenharmony_ci  // Test aborted with gc cleanup
211cb0ef41Sopenharmony_ci  const ac = new AbortController();
221cb0ef41Sopenharmony_ci  aborted(ac.signal, {}).then(common.mustNotCall());
231cb0ef41Sopenharmony_ci  setImmediate(() => {
241cb0ef41Sopenharmony_ci    global.gc();
251cb0ef41Sopenharmony_ci    ac.abort();
261cb0ef41Sopenharmony_ci    assert.strictEqual(ac.signal.aborted, true);
271cb0ef41Sopenharmony_ci    assert.strictEqual(getEventListeners(ac.signal, 'abort').length, 0);
281cb0ef41Sopenharmony_ci  });
291cb0ef41Sopenharmony_ci}
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci{
321cb0ef41Sopenharmony_ci  // Fails with error if not provided abort signal
331cb0ef41Sopenharmony_ci  Promise.all([{}, null, undefined, Symbol(), [], 1, 0, 1n, true, false, 'a', () => {}].map((sig) =>
341cb0ef41Sopenharmony_ci    assert.rejects(aborted(sig, {}), {
351cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE',
361cb0ef41Sopenharmony_ci    })
371cb0ef41Sopenharmony_ci  )).then(common.mustCall());
381cb0ef41Sopenharmony_ci}
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci{
411cb0ef41Sopenharmony_ci  // Fails if not provided a resource
421cb0ef41Sopenharmony_ci  const ac = new AbortController();
431cb0ef41Sopenharmony_ci  Promise.all([null, undefined, 0, 1, 0n, 1n, Symbol(), '', 'a'].map((resource) =>
441cb0ef41Sopenharmony_ci    assert.rejects(aborted(ac.signal, resource), {
451cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_ARG_TYPE',
461cb0ef41Sopenharmony_ci    })
471cb0ef41Sopenharmony_ci  )).then(common.mustCall());
481cb0ef41Sopenharmony_ci}
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci{
511cb0ef41Sopenharmony_ci  const childProcess = spawn(process.execPath, ['--input-type=module']);
521cb0ef41Sopenharmony_ci  childProcess.on('exit', common.mustCall((code) => {
531cb0ef41Sopenharmony_ci    assert.strictEqual(code, 13);
541cb0ef41Sopenharmony_ci  }));
551cb0ef41Sopenharmony_ci  childProcess.stdin.end(`
561cb0ef41Sopenharmony_ci    import { aborted } from 'node:util';
571cb0ef41Sopenharmony_ci    await aborted(new AbortController().signal, {});
581cb0ef41Sopenharmony_ci  `);
591cb0ef41Sopenharmony_ci}
60