-
Notifications
You must be signed in to change notification settings - Fork 28.2k
/
test-stream-toArray.js
93 lines (87 loc) · 2.28 KB
/
test-stream-toArray.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
'use strict';
const common = require('../common');
const {
Readable,
} = require('stream');
const assert = require('assert');
{
// Works on a synchronous stream
(async () => {
const tests = [
[],
[1],
[1, 2, 3],
Array(100).fill().map((_, i) => i),
];
for (const test of tests) {
const stream = Readable.from(test);
const result = await stream.toArray();
assert.deepStrictEqual(result, test);
}
})().then(common.mustCall());
}
{
// Works on a non-object-mode stream
(async () => {
const firstBuffer = Buffer.from([1, 2, 3]);
const secondBuffer = Buffer.from([4, 5, 6]);
const stream = Readable.from(
[firstBuffer, secondBuffer],
{ objectMode: false });
const result = await stream.toArray();
assert.strictEqual(Array.isArray(result), true);
assert.deepStrictEqual(result, [firstBuffer, secondBuffer]);
})().then(common.mustCall());
}
{
// Works on an asynchronous stream
(async () => {
const tests = [
[],
[1],
[1, 2, 3],
Array(100).fill().map((_, i) => i),
];
for (const test of tests) {
const stream = Readable.from(test).map((x) => Promise.resolve(x));
const result = await stream.toArray();
assert.deepStrictEqual(result, test);
}
})().then(common.mustCall());
}
{
// Support for AbortSignal
const ac = new AbortController();
let stream;
assert.rejects(async () => {
stream = Readable.from([1, 2, 3]).map(async (x) => {
if (x === 3) {
await new Promise(() => {}); // Explicitly do not pass signal here
}
return Promise.resolve(x);
});
await stream.toArray({ signal: ac.signal });
}, {
name: 'AbortError',
}).then(common.mustCall(() => {
// Only stops toArray, does not destroy the stream
assert(stream.destroyed, false);
}));
ac.abort();
}
{
// Test result is a Promise
const result = Readable.from([1, 2, 3, 4, 5]).toArray();
assert.strictEqual(result instanceof Promise, true);
}
{
// Error cases
assert.rejects(async () => {
await Readable.from([1]).toArray(1);
}, /ERR_INVALID_ARG_TYPE/).then(common.mustCall());
assert.rejects(async () => {
await Readable.from([1]).toArray({
signal: true
});
}, /ERR_INVALID_ARG_TYPE/).then(common.mustCall());
}