|
5 | 5 | Readable,
|
6 | 6 | } = require('stream');
|
7 | 7 | const assert = require('assert');
|
8 |
| -const { setTimeout } = require('timers/promises'); |
| 8 | +const { once } = require('events'); |
9 | 9 |
|
10 | 10 | {
|
11 | 11 | // forEach works on synchronous streams with a synchronous predicate
|
@@ -43,14 +43,59 @@ const { setTimeout } = require('timers/promises');
|
43 | 43 | })().then(common.mustCall());
|
44 | 44 | }
|
45 | 45 |
|
| 46 | +{ |
| 47 | + // forEach works on an infinite stream |
| 48 | + const ac = new AbortController(); |
| 49 | + const { signal } = ac; |
| 50 | + const stream = Readable.from(async function* () { |
| 51 | + while (true) yield 1; |
| 52 | + }(), { signal }); |
| 53 | + let i = 0; |
| 54 | + assert.rejects(stream.forEach(common.mustCall((x) => { |
| 55 | + i++; |
| 56 | + if (i === 10) ac.abort(); |
| 57 | + assert.strictEqual(x, 1); |
| 58 | + }, 10)), { name: 'AbortError' }).then(common.mustCall()); |
| 59 | +} |
| 60 | + |
| 61 | +{ |
| 62 | + // Emitting an error during `forEach` |
| 63 | + const stream = Readable.from([1, 2, 3, 4, 5]); |
| 64 | + assert.rejects(stream.forEach(async (x) => { |
| 65 | + if (x === 3) { |
| 66 | + stream.emit('error', new Error('boom')); |
| 67 | + } |
| 68 | + }), /boom/).then(common.mustCall()); |
| 69 | +} |
| 70 | + |
| 71 | +{ |
| 72 | + // Throwing an error during `forEach` (sync) |
| 73 | + const stream = Readable.from([1, 2, 3, 4, 5]); |
| 74 | + assert.rejects(stream.forEach((x) => { |
| 75 | + if (x === 3) { |
| 76 | + throw new Error('boom'); |
| 77 | + } |
| 78 | + }), /boom/).then(common.mustCall()); |
| 79 | +} |
| 80 | + |
| 81 | +{ |
| 82 | + // Throwing an error during `forEach` (async) |
| 83 | + const stream = Readable.from([1, 2, 3, 4, 5]); |
| 84 | + assert.rejects(stream.forEach(async (x) => { |
| 85 | + if (x === 3) { |
| 86 | + return Promise.reject(new Error('boom')); |
| 87 | + } |
| 88 | + }), /boom/).then(common.mustCall()); |
| 89 | +} |
| 90 | + |
46 | 91 | {
|
47 | 92 | // Concurrency + AbortSignal
|
48 | 93 | const ac = new AbortController();
|
49 | 94 | let calls = 0;
|
50 | 95 | const forEachPromise =
|
51 | 96 | Readable.from([1, 2, 3, 4]).forEach(async (_, { signal }) => {
|
52 | 97 | calls++;
|
53 |
| - await setTimeout(100, { signal }); |
| 98 | + await once(signal, 'abort'); |
54 | 99 | }, { signal: ac.signal, concurrency: 2 });
|
55 | 100 | // pump
|
56 | 101 | assert.rejects(async () => {
|
|
0 commit comments