forked from nodejs/readable-stream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-stream2-readable-wrap.js
108 lines (105 loc) · 2.55 KB
/
test-stream2-readable-wrap.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
'use strict'
/* replacement start */
const { Buffer } = require('buffer')
/* replacement end */
const { EventEmitter: EE } = require('events')
const { Readable, Writable } = require('../../lib/ours/index')
const { kReadableStreamSuiteName, kReadableStreamSuiteHasMultipleTests } = require('./symbols')
let run = 0
module.exports = function (test) {
function runTest(highWaterMark, objectMode, produce) {
test('run #' + ++run, (t) => {
t.plan(4)
const old = new EE()
const r = new Readable({
highWaterMark,
objectMode
})
t.equal(r, r.wrap(old))
let ended = false
r.on('end', function () {
ended = true
})
old.pause = function () {
// console.error('old.pause()');
old.emit('pause')
flowing = false
}
old.resume = function () {
// console.error('old.resume()');
old.emit('resume')
flow()
}
let flowing
let chunks = 10
let oldEnded = false
const expected = []
function flow() {
flowing = true
// eslint-disable-next-line no-unmodified-loop-condition
while (flowing && chunks-- > 0) {
const item = produce()
expected.push(item)
// console.log('old.emit', chunks, flowing);
old.emit('data', item)
// console.log('after emit', chunks, flowing);
}
if (chunks <= 0) {
oldEnded = true
// console.log('old end', chunks, flowing);
old.emit('end')
}
}
const w = new Writable({
highWaterMark: highWaterMark * 2,
objectMode
})
const written = []
w._write = function (chunk, encoding, cb) {
// console.log('_write', chunk);
written.push(chunk)
setTimeout(cb)
}
w.on('finish', function () {
performAsserts()
})
r.pipe(w)
flow()
function performAsserts() {
t.ok(ended)
t.ok(oldEnded)
t.deepEqual(written, expected)
}
})
}
runTest(100, false, function () {
return Buffer.alloc(100)
})
runTest(10, false, function () {
return Buffer.from('xxxxxxxxxx')
})
runTest(1, true, function () {
return {
foo: 'bar'
}
})
const objectChunks = [
5,
'a',
false,
0,
'',
'xyz',
{
x: 4
},
7,
[],
555
]
runTest(1, true, function () {
return objectChunks.shift()
})
}
module.exports[kReadableStreamSuiteName] = 'stream2-readable-wrap'
module.exports[kReadableStreamSuiteHasMultipleTests] = true