| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,59 +1,94 @@ | |||
| 1 | 1 | // Flags: --expose-gc | |
| 2 | 2 | 'use strict'; | |
| 3 | 3 | ||
| 4 | - const common = require('../common'); | ||
| 4 | + require('../common'); | ||
| 5 | 5 | const { aborted } = require('util'); | |
| 6 | - const assert = require('assert'); | ||
| 6 | + const { | ||
| 7 | + match, | ||
| 8 | + rejects, | ||
| 9 | + strictEqual, | ||
| 10 | + } = require('assert'); | ||
| 7 | 11 | const { getEventListeners } = require('events'); | |
| 8 | - const { spawn } = require('child_process'); | ||
| 12 | + const { inspect } = require('util'); | ||
| 9 | 13 | ||
| 10 | - { | ||
| 11 | - // Test aborted works when provided a resource | ||
| 14 | + const { | ||
| 15 | + test, | ||
| 16 | + } = require('node:test'); | ||
| 17 | + | ||
| 18 | + test('Aborted works when provided a resource', async () => { | ||
| 12 | 19 | const ac = new AbortController(); | |
| 13 | - aborted(ac.signal, {}).then(common.mustCall()); | ||
| 20 | + const promise = aborted(ac.signal, {}); | ||
| 14 | 21 | ac.abort(); | |
| 15 | - assert.strictEqual(ac.signal.aborted, true); | ||
| 16 | - assert.strictEqual(getEventListeners(ac.signal, 'abort').length, 0); | ||
| 17 | - } | ||
| 22 | + await promise; | ||
| 23 | + strictEqual(ac.signal.aborted, true); | ||
| 24 | + strictEqual(getEventListeners(ac.signal, 'abort').length, 0); | ||
| 25 | + }); | ||
| 18 | 26 | ||
| 19 | - { | ||
| 27 | + test('Aborted with gc cleanup', async () => { | ||
| 20 | 28 | // Test aborted with gc cleanup | |
| 21 | 29 | const ac = new AbortController(); | |
| 22 | - aborted(ac.signal, {}).then(common.mustNotCall()); | ||
| 30 | + | ||
| 31 | + const abortedPromise = aborted(ac.signal, {}); | ||
| 32 | + const { promise, resolve } = Promise.withResolvers(); | ||
| 33 | + | ||
| 23 | 34 | setImmediate(() => { | |
| 24 | 35 | global.gc(); | |
| 25 | 36 | ac.abort(); | |
| 26 | - assert.strictEqual(ac.signal.aborted, true); | ||
| 27 | - assert.strictEqual(getEventListeners(ac.signal, 'abort').length, 0); | ||
| 37 | + strictEqual(ac.signal.aborted, true); | ||
| 38 | + strictEqual(getEventListeners(ac.signal, 'abort').length, 0); | ||
| 39 | + resolve(); | ||
| 28 | 40 | }); | |
| 29 | - } | ||
| 30 | 41 | ||
| 31 | - { | ||
| 32 | - // Fails with error if not provided abort signal | ||
| 33 | - Promise.all([{}, null, undefined, Symbol(), [], 1, 0, 1n, true, false, 'a', () => {}].map((sig) => | ||
| 34 | - assert.rejects(aborted(sig, {}), { | ||
| 42 | + await promise; | ||
| 43 | + | ||
| 44 | + // Ensure that the promise is still pending | ||
| 45 | + match(inspect(abortedPromise), /<pending>/); | ||
| 46 | + }); | ||
| 47 | + | ||
| 48 | + test('Fails with error if not provided AbortSignal', async () => { | ||
| 49 | + await Promise.all([{}, null, undefined, Symbol(), [], 1, 0, 1n, true, false, 'a', () => {}].map((sig) => | ||
| 50 | + rejects(aborted(sig, {}), { | ||
| 35 | 51 | code: 'ERR_INVALID_ARG_TYPE', | |
| 36 | 52 | }) | |
| 37 | - )).then(common.mustCall()); | ||
| 38 | - } | ||
| 53 | + )); | ||
| 54 | + }); | ||
| 39 | 55 | ||
| 40 | - { | ||
| 56 | + test('Fails if not provided a resource', async () => { | ||
| 41 | 57 | // Fails if not provided a resource | |
| 42 | 58 | const ac = new AbortController(); | |
| 43 | - Promise.all([null, undefined, 0, 1, 0n, 1n, Symbol(), '', 'a'].map((resource) => | ||
| 44 | - assert.rejects(aborted(ac.signal, resource), { | ||
| 59 | + await Promise.all([null, undefined, 0, 1, 0n, 1n, Symbol(), '', 'a'].map((resource) => | ||
| 60 | + rejects(aborted(ac.signal, resource), { | ||
| 45 | 61 | code: 'ERR_INVALID_ARG_TYPE', | |
| 46 | 62 | }) | |
| 47 | - )).then(common.mustCall()); | ||
| 63 | + )); | ||
| 64 | + }); | ||
| 65 | + | ||
| 66 | + // To allow this case to be more flexibly tested on runtimes that do not have | ||
| 67 | + // child_process.spawn, we lazily require it and skip the test if it is not | ||
| 68 | + // present. | ||
| 69 | + | ||
| 70 | + let spawn; | ||
| 71 | + function lazySpawn() { | ||
| 72 | + if (spawn === undefined) { | ||
| 73 | + try { | ||
| 74 | + spawn = require('child_process').spawn; | ||
| 75 | + } catch { | ||
| 76 | + // Ignore if spawn does not exist. | ||
| 77 | + } | ||
| 78 | + } | ||
| 79 | + return spawn; | ||
| 48 | 80 | } | |
| 49 | 81 | ||
| 50 | - { | ||
| 82 | + test('Does not hang forever', { skip: !lazySpawn() }, async () => { | ||
| 83 | + const { promise, resolve } = Promise.withResolvers(); | ||
| 51 | 84 | const childProcess = spawn(process.execPath, ['--input-type=module']); | |
| 52 | - childProcess.on('exit', common.mustCall((code) => { | ||
| 53 | - assert.strictEqual(code, 13); | ||
| 54 | - })); | ||
| 85 | + childProcess.on('exit', (code) => { | ||
| 86 | + strictEqual(code, 13); | ||
| 87 | + resolve(); | ||
| 88 | + }); | ||
| 55 | 89 | childProcess.stdin.end(` | |
| 56 | 90 | import { aborted } from 'node:util'; | |
| 57 | 91 | await aborted(new AbortController().signal, {}); | |
| 58 | 92 | `); | |
| 59 | - } | ||
| 93 | + await promise; | ||
| 94 | + }); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments