|
| 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 | + // Filter works on synchronous streams with a synchronous predicate |
| 12 | + const stream = Readable.from([1, 2, 3, 4, 5]).filter((x) => x < 3); |
| 13 | + const result = [1, 2]; |
| 14 | + (async () => { |
| 15 | + for await (const item of stream) { |
| 16 | + assert.strictEqual(item, result.shift()); |
| 17 | + } |
| 18 | + })().then(common.mustCall()); |
| 19 | +} |
| 20 | + |
| 21 | +{ |
| 22 | + // Filter works on synchronous streams with an asynchronous predicate |
| 23 | + const stream = Readable.from([1, 2, 3, 4, 5]).filter(async (x) => { |
| 24 | + await Promise.resolve(); |
| 25 | + return x > 3; |
| 26 | + }); |
| 27 | + const result = [4, 5]; |
| 28 | + (async () => { |
| 29 | + for await (const item of stream) { |
| 30 | + assert.strictEqual(item, result.shift()); |
| 31 | + } |
| 32 | + })().then(common.mustCall()); |
| 33 | +} |
| 34 | + |
| 35 | +{ |
| 36 | + // Map works on asynchronous streams with a asynchronous mapper |
| 37 | + const stream = Readable.from([1, 2, 3, 4, 5]).map(async (x) => { |
| 38 | + await Promise.resolve(); |
| 39 | + return x + x; |
| 40 | + }).filter((x) => x > 5); |
| 41 | + const result = [6, 8, 10]; |
| 42 | + (async () => { |
| 43 | + for await (const item of stream) { |
| 44 | + assert.strictEqual(item, result.shift()); |
| 45 | + } |
| 46 | + })().then(common.mustCall()); |
| 47 | +} |
| 48 | + |
| 49 | +{ |
| 50 | + // Concurrency + AbortSignal |
| 51 | + const ac = new AbortController(); |
| 52 | + let calls = 0; |
| 53 | + const stream = Readable.from([1, 2, 3, 4]).filter(async (_, { signal }) => { |
| 54 | + calls++; |
| 55 | + await setTimeout(100, { signal }); |
| 56 | + }, { signal: ac.signal, concurrency: 2 }); |
| 57 | + // pump |
| 58 | + assert.rejects(async () => { |
| 59 | + for await (const item of stream) { |
| 60 | + // nope |
| 61 | + console.log(item); |
| 62 | + } |
| 63 | + }, { |
| 64 | + name: 'AbortError', |
| 65 | + }).then(common.mustCall()); |
| 66 | + |
| 67 | + setImmediate(() => { |
| 68 | + ac.abort(); |
| 69 | + assert.strictEqual(calls, 2); |
| 70 | + }); |
| 71 | +} |
| 72 | + |
| 73 | +{ |
| 74 | + // Concurrency result order |
| 75 | + const stream = Readable.from([1, 2]).filter(async (item, { signal }) => { |
| 76 | + await setTimeout(10 - item, { signal }); |
| 77 | + return true; |
| 78 | + }, { concurrency: 2 }); |
| 79 | + |
| 80 | + (async () => { |
| 81 | + const expected = [1, 2]; |
| 82 | + for await (const item of stream) { |
| 83 | + assert.strictEqual(item, expected.shift()); |
| 84 | + } |
| 85 | + })().then(common.mustCall()); |
| 86 | +} |
| 87 | + |
| 88 | +{ |
| 89 | + // Error cases |
| 90 | + assert.rejects(async () => { |
| 91 | + // eslint-disable-next-line no-unused-vars |
| 92 | + for await (const unused of Readable.from([1]).filter(1)); |
| 93 | + }, /ERR_INVALID_ARG_TYPE/).then(common.mustCall()); |
| 94 | + assert.rejects(async () => { |
| 95 | + // eslint-disable-next-line no-unused-vars |
| 96 | + for await (const _ of Readable.from([1]).filter((x) => x, { |
| 97 | + concurrency: 'Foo' |
| 98 | + })); |
| 99 | + }, /ERR_OUT_OF_RANGE/).then(common.mustCall()); |
| 100 | + assert.rejects(async () => { |
| 101 | + // eslint-disable-next-line no-unused-vars |
| 102 | + for await (const _ of Readable.from([1]).filter((x) => x, 1)); |
| 103 | + }, /ERR_INVALID_ARG_TYPE/).then(common.mustCall()); |
| 104 | +} |
| 105 | +{ |
| 106 | + // Test result is a Readable |
| 107 | + const stream = Readable.from([1, 2, 3, 4, 5]).filter((x) => true); |
| 108 | + assert.strictEqual(stream.readable, true); |
| 109 | +} |
0 commit comments