MCPcopy Create free account
hub / github.com/nodejs/node / testDestroyAsync

Function testDestroyAsync

test/parallel/test-stream-iter-to-readable.js:142–171  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

140// =============================================================================
141
142async function testDestroyAsync() {
143 let returnCalled = false;
144 async function* gen() {
145 try {
146 for (let i = 0; ; i++) {
147 yield [Buffer.from(`chunk${i}`)];
148 }
149 } finally {
150 returnCalled = true;
151 }
152 }
153
154 const readable = toReadable(gen());
155
156 // Read a couple chunks then destroy
157 const chunks = [];
158 await new Promise((resolve, reject) => {
159 readable.on('data', (chunk) => {
160 chunks.push(chunk);
161 if (chunks.length >= 3) {
162 readable.destroy();
163 }
164 });
165 readable.on('close', resolve);
166 readable.on('error', reject);
167 });
168
169 assert.ok(chunks.length >= 3);
170 assert.ok(returnCalled, 'iterator.return() should have been called');
171}
172
173// =============================================================================
174// fromStreamIter: destroy while pump is waiting on backpressure

Callers 1

Calls 6

toReadableFunction · 0.85
genFunction · 0.70
onMethod · 0.45
pushMethod · 0.45
destroyMethod · 0.45
okMethod · 0.45

Tested by

no test coverage detected