/
test.js
539 lines (474 loc) 路 11.9 KB
/
test.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
const test = require('tape');
const concat = require('.');
test('it concats 1 async finite listenable source', t => {
t.plan(14);
const downwardsExpectedType = [
[0, 'function'],
[1, 'number'],
[1, 'number'],
[1, 'number'],
[2, 'undefined'],
];
const downwardsExpected = [1, 2, 3];
function sourceA(type, data) {
if (type === 0) {
const sink = data;
let i = 0;
const id = setInterval(() => {
i++;
sink(1, i);
if (i === 3) {
clearInterval(id);
sink(2);
}
}, 100);
sink(0, sourceA);
}
}
function sink(type, data) {
const et = downwardsExpectedType.shift();
t.equals(type, et[0], 'downwards type is expected: ' + et[0]);
t.equals(typeof data, et[1], 'downwards data type is expected: ' + et[1]);
if (type === 1) {
const e = downwardsExpected.shift();
t.deepEquals(data, e, 'downwards data is expected: ' + JSON.stringify(e));
}
}
const source = concat(sourceA);
source(0, sink);
setTimeout(() => {
t.pass('nothing else happens');
t.end();
}, 700);
});
test('it concats 2 async finite listenable sources', t => {
t.plan(20);
const downwardsExpectedType = [
[0, 'function'],
[1, 'number'],
[1, 'number'],
[1, 'number'],
[1, 'string'],
[1, 'string'],
[2, 'undefined'],
];
const downwardsExpected = [1,2,3,'a','b'];
function sourceA(type, data) {
if (type === 0) {
const sink = data;
let i = 0;
const id = setInterval(() => {
i++;
sink(1, i);
if (i === 3) {
clearInterval(id);
sink(2);
}
}, 100);
sink(0, sourceA);
}
}
function sourceB(type, data) {
if (type === 0) {
const sink = data;
setTimeout(() => {
sink(1, 'a');
}, 230);
setTimeout(() => {
sink(1, 'b');
}, 460);
setTimeout(() => {
sink(2);
}, 550);
sink(0, sourceB);
}
}
function sink(type, data) {
const et = downwardsExpectedType.shift();
t.equals(type, et[0], 'downwards type is expected: ' + et[0]);
t.equals(typeof data, et[1], 'downwards data type is expected: ' + et[1]);
if (type === 1) {
const e = downwardsExpected.shift();
t.deepEquals(data, e, 'downwards data is expected: ' + JSON.stringify(e));
}
}
const source = concat(sourceA, sourceB);
source(0, sink);
setTimeout(() => {
t.pass('nothing else happens');
t.end();
}, 1200);
});
test('it concats 2 sync finite pullable sources', t => {
t.plan(30);
const upwardsExpectedA = [1, 1,1,1];
const upwardsExpectedB = [1,1,1];
const downwardsExpectedType = [
[0, 'function'],
[1, 'number'],
[1, 'number'],
[1, 'number'],
[1, 'string'],
[1, 'string'],
[2, 'undefined'],
];
const downwardsExpected = [10,20,30,'a','b'];
let sentA = 0;
let sinkA;
function sourceA(type, data) {
if (type === 0) {
sinkA = data;
sinkA(0, sourceA);
return;
}
if (sentA === 3) {
sinkA(2);
return;
}
t.true(upwardsExpectedA.length > 0, 'source can be pulled');
const e = upwardsExpectedA.shift();
t.equals(type, e, 'upwards A type is expected: ' + e);
if (sentA === 0) {
sentA++;
sinkA(1, 10);
return;
}
if (sentA === 1) {
sentA++;
sinkA(1, 20);
return;
}
if (sentA === 2) {
sentA++;
sinkA(1, 30);
return;
}
};
let sentB = 0;
let sinkB;
function sourceB(type, data) {
if (type === 0) {
sinkB = data;
data(0, sourceB);
return;
}
if (sentB === 2) {
sinkB(2);
return;
}
t.true(upwardsExpectedB.length > 0, 'source can be pulled');
const e = upwardsExpectedB.shift();
t.equals(type, e, 'upwards B type is expected: ' + e);
if (sentB === 0) {
sentB++;
sinkB(1, 'a');
return;
}
if (sentB === 1) {
sentB++;
sinkB(1, 'b');
return;
}
};
let talkback;
function sink(type, data) {
const et = downwardsExpectedType.shift();
t.equals(type, et[0], 'downwards type is expected: ' + et[0]);
t.equals(typeof data, et[1], 'downwards data type is expected: ' + et[1]);
if (type === 0) {
talkback = data;
talkback(1);
return;
}
if (type === 1) {
const e = downwardsExpected.shift();
t.deepEquals(data, e, 'downwards data is expected: ' + JSON.stringify(e));
talkback(1);
}
}
const source = concat(sourceA, sourceB);
source(0, sink);
setTimeout(() => {
t.pass('nothing else happens');
t.end();
}, 200);
});
test('it returns a source that disposes upon upwards END', t => {
t.plan(16);
const upwardsExpected = [[0, 'function'], [2, 'undefined']];
const downwardsExpectedType = [
[0, 'function'],
[1, 'number'],
[1, 'number'],
[1, 'number'],
];
const downwardsExpected = [10, 20, 30];
function makeSource() {
let sent = 0;
let id;
const source = (type, data) => {
const e = upwardsExpected.shift();
t.equals(type, e[0], 'upwards type is expected: ' + e[0]);
t.equals(typeof data, e[1], 'upwards data is expected: ' + e[1]);
if (type === 0) {
const sink = data;
id = setInterval(() => {
sink(1, ++sent * 10);
}, 100);
sink(0, source);
} else if (type === 2) {
clearInterval(id);
}
};
return source;
}
function makeSink(type, data) {
let talkback;
return (type, data) => {
const et = downwardsExpectedType.shift();
t.equals(type, et[0], 'downwards type is expected: ' + et[0]);
t.equals(typeof data, et[1], 'downwards data type is expected: ' + et[1]);
if (type === 0) {
talkback = data;
}
if (type === 1) {
const e = downwardsExpected.shift();
t.deepEquals(
data,
e,
'downwards data is expected: ' + JSON.stringify(e)
);
}
if (downwardsExpected.length === 0) {
talkback(2);
}
};
}
const source = concat(makeSource());
const sink = makeSink();
source(0, sink);
setTimeout(() => {
t.pass('nothing else happens');
t.end();
}, 700);
});
test('it passes unknown types up & down', t => {
t.plan(14);
const upwardsExpected = [
[0, 'function'],
[1, 'number'],
[11, 'string'],
[2, 'undefined'],
];
const downwardsExpected = [
[0, 'function'],
[1, 'number'],
[11, 'string'],
];
function makeSource() {
const source = (type, data) => {
const e = upwardsExpected.shift();
t.equals(type, e[0], 'upwards type is expected: ' + e[0]);
t.equals(typeof data, e[1], 'upwards data is expected: ' + e[1]);
if (type === 0) {
const sink = data;
sink(0, source);
sink(1, 42)
sink(11, 'unknown')
}
};
return source;
}
function makeSink(type, data) {
let talkback;
return (type, data) => {
const et = downwardsExpected.shift();
t.equals(type, et[0], 'downwards type is expected: ' + et[0]);
t.equals(typeof data, et[1], 'downwards data type is expected: ' + et[1]);
if (type === 0) {
talkback = data;
talkback(1, 57);
talkback(11, 'unknown');
talkback(2);
}
};
}
const source = concat(makeSource());
const sink = makeSink();
source(0, sink);
t.end();
});
test('it propagates source error to sink & doesn\'t subscribe to next source', t => {
t.plan(9);
const downwardsExpected = [
[0, 'function'],
[1, 'number'],
[1, 'number'],
[2, 'string'],
];
function makeSource() {
let limit = 2;
let value = 42;
const source = (type, data) => {
if (type !== 0) return;
const sink = data;
const id = setInterval(() => {
sink(1, value++);
if (--limit === 0) {
clearInterval(id);
sink(2, 'err');
}
}, 100);
sink(0, source);
};
return source;
}
function makeSink(type, data) {
let talkback;
return (type, data) => {
const et = downwardsExpected.shift();
t.equals(type, et[0], 'downwards type is expected: ' + et[0]);
t.equals(typeof data, et[1], 'downwards data type is expected: ' + et[1]);
};
}
const source = concat(makeSource(), makeSource());
const sink = makeSink();
source(0, sink);
setTimeout(() => {
t.pass('nothing else happens');
t.end();
}, 700);
});
test('it reuses latest pull when subscribing to next source', t => {
t.plan(30);
const upwardsExpectedTypeA = [
[1, 'string'],
[1, 'string'],
[1, 'string'],
[1, 'string'],
];
const upwardsExpectedTypeB = [
[1, 'string'],
[1, 'string'],
[1, 'string'],
];
const upwardsEmits = ['foo', 'bar', 'baz', 'orange', 'banana'];
const downwardsExpectedType = [
[0, 'function'],
[1, 'string'],
[1, 'string'],
[1, 'string'],
[1, 'string'],
[1, 'string'],
[2, 'undefined'],
];
const downwardsExpected = ['foo_1', 'bar_1', 'baz_1', 'orange_2', 'banana_2'];
let sentA = 0;
let sinkA;
function sourceA(type, data) {
if (type === 0) {
sinkA = data;
sinkA(0, sourceA);
return;
}
if (sentA === 3) {
sinkA(2);
return;
}
const et = upwardsExpectedTypeA.shift();
t.equals(type, et[0], 'upwards A type is expected: ' + et[0]);
t.equals(typeof data, et[1], 'upwards A data type is expected: ' + et[1]);
sentA++;
sinkA(1, data + '_1');
};
let sentB = 0;
let sinkB;
function sourceB(type, data) {
if (type === 0) {
sinkB = data;
data(0, sourceB);
return;
}
if (sentB === 2) {
sinkB(2);
return;
}
const et = upwardsExpectedTypeB.shift();
t.equals(type, et[0], 'upwards B type is expected: ' + et[0]);
t.equals(typeof data, et[1], 'upwards B data type is expected: ' + et[1]);
sentB++;
sinkB(1, data + '_2');
};
let talkback;
function sink(type, data) {
const et = downwardsExpectedType.shift();
t.equals(type, et[0], 'downwards type is expected: ' + et[0]);
t.equals(typeof data, et[1], 'downwards data type is expected: ' + et[1]);
if (type === 0) {
talkback = data;
talkback(1, upwardsEmits.shift());
return;
}
if (type === 1) {
const e = downwardsExpected.shift();
t.deepEquals(data, e, 'downwards data is expected: ' + JSON.stringify(e));
talkback(1, upwardsEmits.shift());
}
}
const source = concat(sourceA, sourceB);
source(0, sink);
setTimeout(() => {
t.pass('nothing else happens');
t.end();
}, 200);
});
test('it reuses latest pull when subscribing to next source only if needed', t => {
t.plan(17);
const downwardsExpectedType = [
[0, 'function'],
[1, 'string'],
[1, 'string'],
[1, 'string'],
[1, 'string'],
[2, 'undefined'],
];
const downwardsExpected = ['foo', 'bar', 'baz', 'xyz'];
function sourceA(type, data) {
if (type !== 0) {
t.fail('sourceA should not get anything beside the sink');
return;
}
const sink = data;
sink(0, sourceA);
sink(1, 'foo');
sink(1, 'bar');
sink(2);
};
function sourceB(type, data) {
if (type !== 0) {
t.fail('sourceB should not get anything beside the sink');
return;
}
const sink = data;
sink(0, sourceB);
sink(1, 'baz');
sink(1, 'xyz');
sink(2);
};
function sink(type, data) {
const et = downwardsExpectedType.shift();
t.equals(type, et[0], 'downwards type is expected: ' + et[0]);
t.equals(typeof data, et[1], 'downwards data type is expected: ' + et[1]);
if (type !== 1) {
return;
}
const e = downwardsExpected.shift();
t.deepEquals(data, e, 'downwards data is expected: ' + JSON.stringify(e));
}
const source = concat(sourceA, sourceB);
source(0, sink);
setTimeout(() => {
t.pass('nothing else happens');
t.end();
}, 200);
});