forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stream: close iterator in Readable.from
Call iterator.return() if not all of its values are consumed. Fixes: nodejs#32842
- Loading branch information
Showing
2 changed files
with
192 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
'use strict'; | ||
|
||
const { mustCall, mustNotCall } = require('../common'); | ||
const { Readable } = require('stream'); | ||
const { strictEqual } = require('assert'); | ||
|
||
async function asyncSupport() { | ||
const finallyMustCall = mustCall(); | ||
const bodyMustCall = mustCall(); | ||
|
||
async function* infiniteGenerate() { | ||
try { | ||
while (true) yield 'a'; | ||
} finally { | ||
finallyMustCall(); | ||
} | ||
} | ||
|
||
const stream = Readable.from(infiniteGenerate()); | ||
|
||
for await (const chunk of stream) { | ||
bodyMustCall(); | ||
strictEqual(chunk, 'a'); | ||
break; | ||
} | ||
} | ||
|
||
async function syncSupport() { | ||
const finallyMustCall = mustCall(); | ||
const bodyMustCall = mustCall(); | ||
|
||
function* infiniteGenerate() { | ||
try { | ||
while (true) yield 'a'; | ||
} finally { | ||
finallyMustCall(); | ||
} | ||
} | ||
|
||
const stream = Readable.from(infiniteGenerate()); | ||
|
||
for await (const chunk of stream) { | ||
bodyMustCall(); | ||
strictEqual(chunk, 'a'); | ||
break; | ||
} | ||
} | ||
|
||
async function syncPromiseSupport() { | ||
const returnMustBeAwaited = mustCall(); | ||
const bodyMustCall = mustCall(); | ||
|
||
function* infiniteGenerate() { | ||
try { | ||
while (true) yield Promise.resolve('a'); | ||
} finally { | ||
// eslint-disable-next-line no-unsafe-finally | ||
return { then(cb) { | ||
returnMustBeAwaited(); | ||
cb(); | ||
} }; | ||
} | ||
} | ||
|
||
const stream = Readable.from(infiniteGenerate()); | ||
|
||
for await (const chunk of stream) { | ||
bodyMustCall(); | ||
strictEqual(chunk, 'a'); | ||
break; | ||
} | ||
} | ||
|
||
async function syncRejectedSupport() { | ||
const returnMustBeAwaited = mustCall(); | ||
const bodyMustNotCall = mustNotCall(); | ||
const catchMustCall = mustCall(); | ||
const secondNextMustNotCall = mustNotCall(); | ||
|
||
function* generate() { | ||
try { | ||
yield Promise.reject('a'); | ||
secondNextMustNotCall(); | ||
} finally { | ||
// eslint-disable-next-line no-unsafe-finally | ||
return { then(cb) { | ||
returnMustBeAwaited(); | ||
cb(); | ||
} }; | ||
} | ||
} | ||
|
||
const stream = Readable.from(generate()); | ||
|
||
try { | ||
for await (const chunk of stream) { | ||
bodyMustNotCall(chunk); | ||
} | ||
} catch { | ||
catchMustCall(); | ||
} | ||
} | ||
|
||
async function noReturnAfterThrow() { | ||
const returnMustNotCall = mustNotCall(); | ||
const bodyMustNotCall = mustNotCall(); | ||
const catchMustCall = mustCall(); | ||
const nextMustCall = mustCall(); | ||
|
||
const stream = Readable.from({ | ||
[Symbol.asyncIterator]() { return this; }, | ||
async next() { | ||
nextMustCall(); | ||
throw new Error('a'); | ||
}, | ||
async return() { | ||
returnMustNotCall(); | ||
return { done: true }; | ||
}, | ||
}); | ||
|
||
try { | ||
for await (const chunk of stream) { | ||
bodyMustNotCall(chunk); | ||
} | ||
} catch { | ||
catchMustCall(); | ||
} | ||
} | ||
|
||
async function closeStreamWhileNextIsPending() { | ||
const finallyMustCall = mustCall(); | ||
let resolveDestroy; | ||
const destroyed = new Promise((resolve) => { resolveDestroy = resolve; }); | ||
|
||
async function* infiniteGenerate() { | ||
try { | ||
while (true) { | ||
yield 'a'; | ||
await destroyed; | ||
} | ||
} finally { | ||
finallyMustCall(); | ||
} | ||
} | ||
|
||
const stream = Readable.from(infiniteGenerate()); | ||
|
||
stream.on('data', (data) => { | ||
strictEqual(data, 'a'); | ||
stream.destroy(); | ||
resolveDestroy(); | ||
}); | ||
} | ||
|
||
Promise.all([ | ||
asyncSupport(), | ||
syncSupport(), | ||
syncPromiseSupport(), | ||
syncRejectedSupport(), | ||
noReturnAfterThrow(), | ||
closeStreamWhileNextIsPending() | ||
]).then(mustCall()); |