Debian Codegraph @

merge2/test/index.js

raw symbols SCIP
1 'use strict'
2
3 const tman = require('tman')
4 const assert = require('assert')
5 const Stream = require('stream')
6 const thunk = require('thunks').thunk
7 const through = require('through2')
8 const toThrough = require('to-through')
9
10 test(require('..'))
11
12 function test (merge2) {
13 tman.suite('merge2', function () {
14 tman.it('merge2(read1, read2, through3)', function (done) {
15 const options = { objectMode: true }
16 const result = []
17 const read1 = fakeReadStream(options)
18 const read2 = fakeReadStream(options)
19 const through3 = through.obj()
20
21 const mergeStream = merge2(read1, read2, through3)
22
23 read1.push(1)
24 thunk.delay(100)(function () {
25 read1.push(2)
26 read1.push(null)
27 })
28 read2.push(3)
29 thunk.delay(10)(function () {
30 read2.push(4)
31 read2.push(null)
32 })
33 through3.push(5)
34 thunk.delay(200)(function () {
35 through3.push(6)
36 through3.end()
37 })
38
39 mergeStream
40 .on('data', function (chunk) {
41 result.push(chunk)
42 })
43 .on('error', done)
44 .on('end', function () {
45 assert.deepStrictEqual(result, [1, 2, 3, 4, 5, 6])
46 done()
47 })
48 })
49
50 tman.it('merge2 - error handling', function (done) {
51 const ts = through.obj()
52
53 const mergeStream = merge2(toThrough(ts), { pipeError: true })
54
55 const expectedError = new Error('error')
56 thunk.delay(100)(function () {
57 ts.destroy(expectedError)
58 })
59
60 mergeStream
61 .on('error', function (error) {
62 assert.strictEqual(error, expectedError)
63 done()
64 })
65 .on('end', function () {
66 throw Error('error expected')
67 })
68 })
69
70 tman.it('merge2(TransformStream)', function (done) {
71 const result = []
72 const ts = through.obj()
73
74 const mergeStream = merge2(toThrough(ts))
75
76 ts.push(1)
77 thunk.delay(100)(function () {
78 ts.push(2)
79 ts.push(null)
80 })
81
82 mergeStream
83 .on('data', function (chunk) {
84 result.push(chunk)
85 })
86 .on('error', done)
87 .on('end', function () {
88 assert.deepStrictEqual(result, [1, 2])
89 done()
90 })
91 })
92
93 tman.it('merge2(read1, [read2, through3], through4, [through5, read6])', function (done) {
94 const options = { objectMode: true }
95 const result = []
96 const read1 = fakeReadStream(options)
97 const read2 = fakeReadStream(options)
98 const through3 = through.obj()
99 const through4 = through.obj()
100 const through5 = through.obj()
101 const read6 = fakeReadStream(options)
102
103 read1.push(1)
104 read1.push(null)
105 thunk.delay(100)(function () {
106 read2.push(2)
107 read2.push(null)
108 })
109 through3.push(3)
110 through3.end()
111 through4.push(4)
112 through4.push(null)
113 through5.push(5)
114 through5.push(null)
115 thunk.delay(200)(function () {
116 read6.push(6)
117 read6.push(null)
118 })
119
120 const mergeStream = merge2(read1, [read2, through3], through4, [through5, read6])
121
122 mergeStream
123 .on('data', function (chunk) {
124 result.push(chunk)
125 })
126 .on('error', done)
127 .on('end', function () {
128 assert.deepStrictEqual(result, [1, 3, 2, 4, 5, 6])
129 done()
130 })
131 })
132
133 tman.it('merge2().add(read1, [read2, through3], through4, [through5, read6])', function (done) {
134 const options = { objectMode: true }
135 const result = []
136 const read1 = fakeReadStream(options)
137 const read2 = fakeReadStream(options)
138 const through3 = through.obj()
139 const through4 = through.obj()
140 const through5 = through.obj()
141 const read6 = fakeReadStream(options)
142 const mergeStream = merge2()
143
144 read1.push(1)
145 read1.push(null)
146 thunk.delay(100)(function () {
147 read2.push(2)
148 read2.push(null)
149 })
150 through3.push(3)
151 through3.end()
152 through4.push(4)
153 through4.push(null)
154 through5.push(5)
155 through5.push(null)
156 thunk.delay(200)(function () {
157 read6.push(6)
158 read6.push(null)
159 })
160
161 mergeStream
162 .add(read1, [read2, through3], through4)
163 .on('data', function (chunk) {
164 result.push(chunk)
165 })
166 .add([through5, read6])
167 .on('error', done)
168 .on('end', function () {
169 assert.deepStrictEqual(result, [1, 3, 2, 4, 5, 6])
170 done()
171 })
172 })
173
174 tman.it('merge2(read1, read2, through3, {objectMode: false})', function (done) {
175 const options = { objectMode: false }
176 let result = ''
177 const read1 = fakeReadStream(options)
178 const read2 = fakeReadStream(options)
179 const through3 = through(options)
180
181 const mergeStream = merge2(read1, read2, through3, options)
182
183 read1.push('1')
184 thunk.delay(100)(function () {
185 read1.push('2')
186 read1.push(null)
187 })
188 read2.push('3')
189 thunk.delay(10)(function () {
190 read2.push('4')
191 read2.push(null)
192 })
193 through3.push('5')
194 thunk.delay(200)(function () {
195 through3.push('6')
196 through3.end()
197 })
198
199 mergeStream
200 .on('data', function (chunk) {
201 result += chunk.toString()
202 })
203 .on('error', done)
204 .on('end', function () {
205 assert.strictEqual(result, '123456')
206 done()
207 })
208 })
209
210 tman.it('merge2([read1, read2]) with classic style streams', function (done) {
211 const result = []
212 const read1 = fakeReadClassicStream()
213 const read2 = fakeReadClassicStream()
214
215 const mergeStream = merge2([read1, read2])
216
217 read1.push(1)
218 read1.push(null)
219 thunk.delay(100)(function () {
220 read2.push(2)
221 read2.push(null)
222 })
223
224 mergeStream
225 .on('data', function (chunk) {
226 result.push(chunk)
227 })
228 .on('error', done)
229 .on('end', function () {
230 assert.deepStrictEqual(result, [1, 2])
231 done()
232 })
233 })
234
235 tman.it('merge2(read1, read2, {end: false})', function (done) {
236 const options = { objectMode: true }
237 const result = []
238 const read1 = fakeReadStream(options)
239 const read2 = fakeReadStream(options)
240 const through3 = through.obj()
241
242 const mergeStream = merge2(read1, read2, { end: false })
243
244 read1.push(1)
245 read1.push(2)
246 read1.push(null)
247 read2.push(3)
248 read2.push(4)
249 read2.push(null)
250 through3.push(5)
251 through3.push(6)
252 through3.end()
253
254 thunk.delay(500)(function () {
255 assert.deepStrictEqual(result, [1, 2, 3, 4])
256 mergeStream.add(through3)
257 return thunk.delay(100)
258 })(function () {
259 mergeStream.end()
260 })
261
262 mergeStream
263 .on('data', function (chunk) {
264 result.push(chunk)
265 })
266 .on('error', done)
267 .on('end', function () {
268 assert.deepStrictEqual(result, [1, 2, 3, 4, 5, 6])
269 done()
270 })
271 })
272
273 tman.it('merge2(merge2(through4, [through5, read6]), read1, [read2, through3])', function (done) {
274 const options = { objectMode: true }
275 const result1 = []
276 const result2 = []
277 const read1 = fakeReadStream(options)
278 const read2 = fakeReadStream(options)
279 const through3 = through.obj()
280 const through4 = through.obj()
281 const through5 = through.obj()
282 const read6 = fakeReadStream(options)
283
284 read1.push(1)
285 read1.push(null)
286 thunk.delay(100)(function () {
287 read2.push(2)
288 read2.push(null)
289 })
290 through3.push(3)
291 through3.end()
292 through4.push(4)
293 through4.push(null)
294 through5.push(5)
295 through5.push(null)
296 thunk.delay(10)(function () {
297 read6.push(6)
298 read6.push(null)
299 })
300
301 const mergeStream1 = merge2(through4, [through5, read6])
302
303 mergeStream1.on('data', function (chunk) {
304 result1.push(chunk)
305 })
306
307 const mergeStream = merge2(mergeStream1, read1, [read2, through3])
308
309 mergeStream
310 .on('data', function (chunk) {
311 result2.push(chunk)
312 if (result2.length <= 3) assert.deepStrictEqual(result1, result2)
313 else assert.deepStrictEqual(result1, [4, 5, 6])
314 })
315 .on('error', done)
316 .on('end', function () {
317 assert.deepStrictEqual(result2, [4, 5, 6, 1, 3, 2])
318 done()
319 })
320 })
321 })
322 }
323
324 function fakeReadStream (options) {
325 const readStream = new Stream.Readable(options)
326 readStream._read = function () {}
327 return readStream
328 }
329
330 function fakeReadClassicStream () {
331 const readStream = new Stream()
332 readStream.readable = true
333 readStream.push = function (data) {
334 if (data === null) {
335 this.emit('end')
336 readStream.readable = false
337 }
338 this.emit('data', data)
339 }
340 return readStream
341 }