diff options
author | Nitzan Uziely <linkgoron@gmail.com> | 2021-04-17 21:16:46 +0300 |
---|---|---|
committer | Danielle Adams <adamzdanielle@gmail.com> | 2021-05-31 22:34:54 +0300 |
commit | 9054d25acc5d3b6dcb8ea53351d9b3027f9d68e1 (patch) | |
tree | 9c804ec42c5722ce22a6fe7f93414d67da28a404 /test | |
parent | e2f28c80d1a44f306ad106796696a794ac9683d9 (diff) |
stream: add a non-destroying iterator to Readable
add a non-destroying iterator to Readable
fixes: https://github.com/nodejs/node/issues/38491
PR-URL: https://github.com/nodejs/node/pull/38526
Fixes: https://github.com/nodejs/node/issues/38491
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/parallel/test-stream-readable-async-iterators.js | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/test/parallel/test-stream-readable-async-iterators.js b/test/parallel/test-stream-readable-async-iterators.js index 7c457fdc3da..a497317565f 100644 --- a/test/parallel/test-stream-readable-async-iterators.js +++ b/test/parallel/test-stream-readable-async-iterators.js @@ -693,6 +693,122 @@ async function tests() { }); } +// AsyncIterator non-destroying iterator +{ + function createReadable() { + return Readable.from((async function* () { + await Promise.resolve(); + yield 5; + await Promise.resolve(); + yield 7; + await Promise.resolve(); + })()); + } + + function createErrorReadable() { + const opts = { read() { throw new Error('inner'); } }; + return new Readable(opts); + } + + // Check default destroys on return + (async function() { + const readable = createReadable(); + for await (const chunk of readable.iterator()) { + assert.strictEqual(chunk, 5); + break; + } + + assert.ok(readable.destroyed); + })().then(common.mustCall()); + + // Check explicit destroying on return + (async function() { + const readable = createReadable(); + for await (const chunk of readable.iterator({ destroyOnReturn: true })) { + assert.strictEqual(chunk, 5); + break; + } + + assert.ok(readable.destroyed); + })().then(common.mustCall()); + + // Check default destroys on error + (async function() { + const readable = createErrorReadable(); + try { + // eslint-disable-next-line no-unused-vars + for await (const chunk of readable) { } + assert.fail('should have thrown'); + } catch (err) { + assert.strictEqual(err.message, 'inner'); + } + + assert.ok(readable.destroyed); + })().then(common.mustCall()); + + // Check explicit destroys on error + (async function() { + const readable = createErrorReadable(); + const opts = { destroyOnError: true, destroyOnReturn: false }; + try { + // eslint-disable-next-line no-unused-vars + for await (const chunk of readable.iterator(opts)) { } + assert.fail('should have thrown'); + } catch (err) { + assert.strictEqual(err.message, 'inner'); + } + + assert.ok(readable.destroyed); + })().then(common.mustCall()); + + // Check explicit non-destroy with return true + (async function() { + const readable = createErrorReadable(); + const opts = { destroyOnError: false, destroyOnReturn: true }; + try { + // eslint-disable-next-line no-unused-vars + for await (const chunk of readable.iterator(opts)) { } + assert.fail('should have thrown'); + } catch (err) { + assert.strictEqual(err.message, 'inner'); + } + + assert.ok(!readable.destroyed); + })().then(common.mustCall()); + + // Check explicit non-destroy with return true + (async function() { + const readable = createReadable(); + const opts = { destroyOnReturn: false }; + for await (const chunk of readable.iterator(opts)) { + assert.strictEqual(chunk, 5); + break; + } + + assert.ok(!readable.destroyed); + + for await (const chunk of readable.iterator(opts)) { + assert.strictEqual(chunk, 7); + } + + assert.ok(readable.destroyed); + })().then(common.mustCall()); + + // Check non-object options. + { + const readable = createReadable(); + assert.throws( + () => readable.iterator(42), + { + code: 'ERR_INVALID_ARG_TYPE', + name: 'TypeError', + message: 'The "options" argument must be of type object. Received ' + + 'type number (42)', + } + ); + } +} + { let _req; const server = http.createServer((request, response) => { |