@@ -65,14 +65,14 @@ describe("Consumer management", () => {
65
65
66
66
test ( "Automaticly add consumer on pipe" , ( ) => {
67
67
expect ( testTopic . consumers . size ) . toBe ( 0 ) ;
68
- testTopic . pipe ( testConsumer . stream ( ) ) ;
68
+ testTopic . pipe ( testConsumer ) ;
69
69
expect ( testTopic . consumers . size ) . toBe ( 1 ) ;
70
70
} )
71
71
72
72
test ( "Add only unique streams on pipe" , ( ) => {
73
- testTopic . pipe ( testConsumer . stream ( ) ) ;
74
- testTopic . pipe ( testConsumer . stream ( ) ) ;
75
- testTopic . pipe ( testConsumer . stream ( ) ) ;
73
+ testTopic . pipe ( testConsumer ) ;
74
+ testTopic . pipe ( testConsumer ) ;
75
+ testTopic . pipe ( testConsumer ) ;
76
76
expect ( testTopic . consumers . size ) . toBe ( 1 ) ;
77
77
} )
78
78
@@ -81,9 +81,9 @@ describe("Consumer management", () => {
81
81
const testConsumer2 = StreamWrapper . create ( new Writable ( { write : ( ) => { } } ) , "testWriteStream2" , StreamType . Instance , { } ) ;
82
82
const testConsumer3 = StreamWrapper . create ( new Writable ( { write : ( ) => { } } ) , "testWriteStream3" , StreamType . Instance , { } ) ;
83
83
84
- testTopic . pipe ( testConsumer1 . stream ( ) ) ;
85
- testTopic . pipe ( testConsumer2 . stream ( ) ) ;
86
- testTopic . pipe ( testConsumer3 . stream ( ) ) ;
84
+ testTopic . pipe ( testConsumer1 ) ;
85
+ testTopic . pipe ( testConsumer2 ) ;
86
+ testTopic . pipe ( testConsumer3 ) ;
87
87
88
88
expect ( testTopic . consumers . size ) . toBe ( 3 ) ;
89
89
} )
@@ -106,17 +106,16 @@ describe("Consumer management", () => {
106
106
const testConsumer2 = StreamWrapper . create ( new Writable ( { write : ( ) => { } } ) , "testWriteStream2" , StreamType . Instance , { } ) ;
107
107
const testConsumer3 = StreamWrapper . create ( new Writable ( { write : ( ) => { } } ) , "testWriteStream3" , StreamType . Instance , { } ) ;
108
108
109
- testTopic . pipe ( testConsumer1 . stream ( ) ) ;
110
- testTopic . pipe ( testConsumer2 . stream ( ) ) ;
111
- testTopic . pipe ( testConsumer3 . stream ( ) ) ;
109
+ testTopic . pipe ( testConsumer1 ) ;
110
+ testTopic . pipe ( testConsumer2 ) ;
111
+ testTopic . pipe ( testConsumer3 ) ;
112
112
113
113
expect ( testTopic . consumers . size ) . toBe ( 3 ) ;
114
114
testTopic . unpipe ( ) ;
115
115
expect ( testTopic . consumers . size ) . toBe ( 0 ) ;
116
116
} )
117
117
} )
118
118
119
-
120
119
describe ( "Event flow" , ( ) => {
121
120
const waitForEvent = ( eventName : string , source : Stream ) => {
122
121
return new Promise < boolean > ( ( resolve , reject ) => {
@@ -182,7 +181,7 @@ describe("Event flow", () => {
182
181
testProvider . stream ( ) . pipe ( testTopic )
183
182
expect ( testTopic . state ( ) ) . toBe ( WorkState . Waiting ) ;
184
183
const eventPromise = waitForEvent ( TopicEvent . StateChanged , testTopic ) ;
185
- testTopic . pipe ( testConsumer . stream ( ) ) ;
184
+ testTopic . pipe ( testConsumer ) ;
186
185
await eventPromise
187
186
expect ( testTopic . state ( ) ) . toBe ( WorkState . Flowing ) ;
188
187
@@ -208,7 +207,7 @@ describe("Event flow", () => {
208
207
209
208
test ( "State flowing after resume()" , async ( ) => {
210
209
let eventPromise = waitForEvent ( TopicEvent . StateChanged , testTopic ) ;
211
- testProvider . stream ( ) . pipe ( testTopic ) . pipe ( testConsumer . stream ( ) ) ;
210
+ testProvider . stream ( ) . pipe ( testTopic ) . pipe ( testConsumer ) ;
212
211
await eventPromise ;
213
212
expect ( testTopic . state ( ) ) . toBe ( WorkState . Flowing ) ;
214
213
@@ -238,16 +237,16 @@ describe("Event flow", () => {
238
237
} )
239
238
test ( "ConsumersChanged on add" , async ( ) => {
240
239
const eventPromise = waitForEvent ( TopicEvent . ConsumersChanged , testTopic ) ;
241
- testTopic . pipe ( testConsumer . stream ( ) ) ;
240
+ testTopic . pipe ( testConsumer ) ;
242
241
await eventPromise ;
243
242
expect ( testTopic . consumers . size ) . toBe ( 1 ) ;
244
243
} )
245
244
test ( "ConsumersChanged on remove" , async ( ) => {
246
245
let eventPromise = waitForEvent ( TopicEvent . ConsumersChanged , testTopic ) ;
247
- testTopic . pipe ( testConsumer . stream ( ) ) ;
246
+ testTopic . pipe ( testConsumer ) ;
248
247
await eventPromise ;
249
248
eventPromise = waitForEvent ( TopicEvent . ConsumersChanged , testTopic ) ;
250
- testTopic . unpipe ( testConsumer . stream ( ) ) ;
249
+ testTopic . unpipe ( testConsumer ) ;
251
250
expect ( testTopic . consumers . size ) . toBe ( 0 ) ;
252
251
} )
253
252
} )
@@ -256,6 +255,13 @@ describe("Event flow", () => {
256
255
describe ( "Data flow" , ( ) => {
257
256
const testText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit." ;
258
257
258
+ const createWaitingPromise = ( ) : [ Promise < void > , ( ) => void , ( _ : any ) => void ] => {
259
+ let res = ( ) => { }
260
+ let rej = ( reason : any ) => { } ;
261
+ const promise = new Promise < void > ( ( resolve , reject ) => { res = resolve , rej = reject } )
262
+ return [ promise , res , rej ]
263
+ }
264
+
259
265
test ( "Basic flow" , async ( ) => {
260
266
const topicFinished = new Promise ( resolve => testTopic . on ( "readable" , ( ) => {
261
267
resolve ( testTopic . read ( ) ) ;
@@ -269,7 +275,7 @@ describe("Data flow", () => {
269
275
const testProvider = StreamWrapper . create ( new PassThrough ( { encoding : "ascii" } ) , "testReadStream" , StreamType . Instance , { } ) ;
270
276
const testConsumer = StreamWrapper . create ( new PassThrough ( { encoding : "ascii" } ) , "testWriteStream" , StreamType . Instance , { } ) ;
271
277
272
- testProvider . stream ( ) . pipe ( testTopic ) . pipe ( testConsumer . stream ( ) ) ;
278
+ testProvider . stream ( ) . pipe ( testTopic ) . pipe ( testConsumer ) ;
273
279
274
280
const readPromise = new Promise ( resolve => testConsumer . stream ( ) . on ( "readable" , ( ) => {
275
281
resolve ( testConsumer . stream ( ) . read ( ) )
@@ -279,10 +285,73 @@ describe("Data flow", () => {
279
285
280
286
expect ( readValue ) . toBe ( testText ) ;
281
287
} ) ;
282
- test ( "Many Providers writing" , ( ) => {
283
-
288
+ test ( "Many Providers writing" , async ( ) => {
289
+ const [ startGeneratingPromise , startGenerating ] = createWaitingPromise ( ) ;
290
+ async function * generator ( from : number , to : number ) {
291
+ let i = from ;
292
+ while ( i <= to ) {
293
+ await startGeneratingPromise ;
294
+ yield Number ( i ++ ) . toString ( )
295
+ }
296
+ }
297
+
298
+ const createStreamProvider = ( name : string , from : number , to : number ) : [ StreamWrapper < Readable > , Promise < void > ] => {
299
+ const gen = generator ( from , to ) ;
300
+ const provider = StreamWrapper . create ( Readable . from ( gen ) . setEncoding ( "ascii" ) , name , StreamType . Instance , { } ) ;
301
+ const [ streamEndPromise , streamEnd , streamError ] = createWaitingPromise ( ) ;
302
+ provider . stream ( ) . on ( "close" , streamEnd ) . on ( "error" , streamError )
303
+ return [ provider , streamEndPromise ] ;
304
+ }
305
+
306
+ const [ provider1 , provider1End ] = createStreamProvider ( "TestReadStream1" , 1 , 10 ) ;
307
+ const [ provider2 , provider2End ] = createStreamProvider ( "TestReadStream2" , 11 , 20 ) ;
308
+ const [ provider3 , provider3End ] = createStreamProvider ( "TestReadStream3" , 21 , 30 ) ;
309
+
310
+ provider1 . stream ( ) . pipe ( testTopic , { end : false } ) ;
311
+ provider2 . stream ( ) . pipe ( testTopic , { end : false } ) ;
312
+ provider3 . stream ( ) . pipe ( testTopic , { end : false } ) ;
313
+
314
+ const result : number [ ] = [ ] ;
315
+ testTopic . on ( "readable" , ( ) => { result . push ( Number ( testTopic . read ( ) ) ) } )
316
+
317
+ startGenerating ( ) ;
318
+ await Promise . all ( [ provider1End , provider2End , provider3End ] ) ;
319
+ result . sort ( ( a : number , b : number ) => a - b )
320
+ const expectedResult = [ ...Array ( 30 ) . keys ( ) ] . map ( val => val + 1 ) ;
321
+ const match = result . length === expectedResult . length && ! expectedResult . some ( ( value , index ) => result [ index ] !== value )
322
+ expect ( match ) . toBe ( true ) ;
284
323
} )
285
- test ( "Many Consumers reading" , ( ) => {
324
+ test ( "Many Consumers reading" , async ( ) => {
325
+ const consumer1 = StreamWrapper . create ( new PassThrough ( { encoding : "ascii" } ) , "TestWriteStream1" , StreamType . Instance , { } ) ;
326
+ const consumer2 = StreamWrapper . create ( new PassThrough ( { encoding : "ascii" } ) , "TestWriteStream1" , StreamType . Instance , { } ) ;
327
+ const consumer3 = StreamWrapper . create ( new PassThrough ( { encoding : "ascii" } ) , "TestWriteStream1" , StreamType . Instance , { } ) ;
328
+
329
+ let result = [ "" , "" , "" ] ;
330
+ const [ readed1Promise , readed1 ] = createWaitingPromise ( ) ;
331
+ const [ readed2Promise , readed2 ] = createWaitingPromise ( ) ;
332
+ const [ readed3Promise , readed3 ] = createWaitingPromise ( ) ;
333
+
334
+ consumer1 . stream ( ) . on ( "readable" , ( ) => {
335
+ result [ 0 ] = consumer1 . stream ( ) . read ( ) ;
336
+ readed1 ( ) ;
337
+ } )
338
+ consumer2 . stream ( ) . on ( "readable" , ( ) => {
339
+ result [ 1 ] = consumer2 . stream ( ) . read ( ) ;
340
+ readed2 ( ) ;
341
+ } )
342
+ consumer3 . stream ( ) . on ( "readable" , ( ) => {
343
+ result [ 2 ] = consumer3 . stream ( ) . read ( ) ;
344
+ readed3 ( ) ;
345
+ } )
286
346
347
+ testTopic . pipe ( consumer1 ) ;
348
+ testTopic . pipe ( consumer2 ) ;
349
+ testTopic . pipe ( consumer3 ) ;
350
+ testTopic . write ( testText ) ;
351
+
352
+ await Promise . all ( [ readed1Promise , readed2Promise , readed3Promise ] ) ;
353
+ expect ( result [ 0 ] ) . toBe ( testText ) ;
354
+ expect ( result [ 1 ] ) . toBe ( testText ) ;
355
+ expect ( result [ 2 ] ) . toBe ( testText ) ;
287
356
} )
288
357
} )
0 commit comments