|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +const { |
| 5 | + Readable, |
| 6 | +} = require('stream'); |
| 7 | +const assert = require('assert'); |
| 8 | +const { setTimeout } = require('timers/promises'); |
| 9 | + |
| 10 | +{ |
| 11 | + // forEach works on synchronous streams with a synchronous predicate |
| 12 | + const stream = Readable.from([1, 2, 3]); |
| 13 | + const result = [1, 2, 3]; |
| 14 | + (async () => { |
| 15 | + await stream.forEach((value) => assert.strictEqual(value, result.shift())); |
| 16 | + })().then(common.mustCall()); |
| 17 | +} |
| 18 | + |
| 19 | +{ |
| 20 | + // forEach works an asynchronous streams |
| 21 | + const stream = Readable.from([1, 2, 3]).filter(async (x) => { |
| 22 | + await Promise.resolve(); |
| 23 | + return true; |
| 24 | + }); |
| 25 | + const result = [1, 2, 3]; |
| 26 | + (async () => { |
| 27 | + await stream.forEach((value) => assert.strictEqual(value, result.shift())); |
| 28 | + })().then(common.mustCall()); |
| 29 | +} |
| 30 | + |
| 31 | +{ |
| 32 | + // forEach works on asynchronous streams with a asynchronous forEach fn |
| 33 | + const stream = Readable.from([1, 2, 3]).filter(async (x) => { |
| 34 | + await Promise.resolve(); |
| 35 | + return true; |
| 36 | + }); |
| 37 | + const result = [1, 2, 3]; |
| 38 | + (async () => { |
| 39 | + await stream.forEach(async (value) => { |
| 40 | + await Promise.resolve(); |
| 41 | + assert.strictEqual(value, result.shift()); |
| 42 | + }); |
| 43 | + })().then(common.mustCall()); |
| 44 | +} |
| 45 | + |
| 46 | +{ |
| 47 | + // Concurrency + AbortSignal |
| 48 | + const ac = new AbortController(); |
| 49 | + let calls = 0; |
| 50 | + const forEachPromise = |
| 51 | + Readable.from([1, 2, 3, 4]).forEach(async (_, { signal }) => { |
| 52 | + calls++; |
| 53 | + await setTimeout(100, { signal }); |
| 54 | + }, { signal: ac.signal, concurrency: 2 }); |
| 55 | + // pump |
| 56 | + assert.rejects(async () => { |
| 57 | + await forEachPromise; |
| 58 | + }, { |
| 59 | + name: 'AbortError', |
| 60 | + }).then(common.mustCall()); |
| 61 | + |
| 62 | + setImmediate(() => { |
| 63 | + ac.abort(); |
| 64 | + assert.strictEqual(calls, 2); |
| 65 | + }); |
| 66 | +} |
| 67 | + |
| 68 | +{ |
| 69 | + // Error cases |
| 70 | + assert.rejects(async () => { |
| 71 | + Readable.from([1]).forEach(1); |
| 72 | + }, /ERR_INVALID_ARG_TYPE/).then(common.mustCall()); |
| 73 | + assert.rejects(async () => { |
| 74 | + Readable.from([1]).forEach((x) => x, { |
| 75 | + concurrency: 'Foo' |
| 76 | + }); |
| 77 | + }, /ERR_OUT_OF_RANGE/).then(common.mustCall()); |
| 78 | + assert.rejects(async () => { |
| 79 | + Readable.from([1]).forEach((x) => x, 1); |
| 80 | + }, /ERR_INVALID_ARG_TYPE/).then(common.mustCall()); |
| 81 | +} |
| 82 | +{ |
| 83 | + // Test result is a Promise |
| 84 | + const stream = Readable.from([1, 2, 3, 4, 5]).forEach((_) => true); |
| 85 | + assert.strictEqual(typeof stream.then, 'function'); |
| 86 | +} |
0 commit comments