diff options
author | Nitzan Uziely <nitzan@testim.io> | 2021-02-13 15:05:04 +0300 |
---|---|---|
committer | Michaƫl Zasso <targos@protonmail.com> | 2021-02-28 16:46:20 +0300 |
commit | 3d3df0c00522e26c81a71cf0911ecbed55c31b4d (patch) | |
tree | 153c7dba414ab922e6fa6649f312caa62cc566c4 /test | |
parent | 617819e4fba83217d6a1d65c383e4448a481bd1e (diff) |
stream: add AbortSignal support to finished
PR-URL: https://github.com/nodejs/node/pull/37354
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/parallel/test-stream-finished.js | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/test/parallel/test-stream-finished.js b/test/parallel/test-stream-finished.js index a495bbe2861..d1652396dae 100644 --- a/test/parallel/test-stream-finished.js +++ b/test/parallel/test-stream-finished.js @@ -93,6 +93,83 @@ const http = require('http'); } { + // Check pre-cancelled + const signal = new EventTarget(); + signal.aborted = true; + + const rs = Readable.from((function* () {})()); + finished(rs, { signal }, common.mustCall((err) => { + assert.strictEqual(err.name, 'AbortError'); + })); +} + +{ + // Check cancelled before the stream ends sync. + const ac = new AbortController(); + const { signal } = ac; + + const rs = Readable.from((function* () {})()); + finished(rs, { signal }, common.mustCall((err) => { + assert.strictEqual(err.name, 'AbortError'); + })); + + ac.abort(); +} + +{ + // Check cancelled before the stream ends async. + const ac = new AbortController(); + const { signal } = ac; + + const rs = Readable.from((function* () {})()); + setTimeout(() => ac.abort(), 1); + finished(rs, { signal }, common.mustCall((err) => { + assert.strictEqual(err.name, 'AbortError'); + })); +} + +{ + // Check cancelled after doesn't throw. + const ac = new AbortController(); + const { signal } = ac; + + const rs = Readable.from((function* () { + yield 5; + setImmediate(() => ac.abort()); + })()); + rs.resume(); + finished(rs, { signal }, common.mustSucceed()); +} + +{ + // Promisified abort works + const finishedPromise = promisify(finished); + async function run() { + const ac = new AbortController(); + const { signal } = ac; + const rs = Readable.from((function* () {})()); + setImmediate(() => ac.abort()); + await finishedPromise(rs, { signal }); + } + + assert.rejects(run, { name: 'AbortError' }).then(common.mustCall()); +} + +{ + // Promisified pre-aborted works + const finishedPromise = promisify(finished); + async function run() { + const signal = new EventTarget(); + signal.aborted = true; + const rs = Readable.from((function* () {})()); + await finishedPromise(rs, { signal }); + } + + assert.rejects(run, { name: 'AbortError' }).then(common.mustCall()); +} + + +{ const rs = fs.createReadStream('file-does-not-exist'); finished(rs, common.expectsError({ |