-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathshare.test.ts
More file actions
493 lines (391 loc) · 14.7 KB
/
Copy pathshare.test.ts
File metadata and controls
493 lines (391 loc) · 14.7 KB
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
/**
* Tests for Share - Pull-model multi-consumer streaming
*
* Requirements covered: See https://github.com/jasnell/new-streams/blob/main/docs/REQUIREMENTS.md Section 7 (SHARE-xxx)
*/
import { describe, it } from 'node:test';
import * as assert from 'node:assert';
import { share, shareSync, Share, SyncShare } from './share.js';
import { from, fromSync } from './from.js';
import { bytes, bytesSync } from './consumers.js';
// Helper to decode Uint8Array to string
function decode(data: Uint8Array): string {
return new TextDecoder().decode(data);
}
// Helper to collect chunks from an async iterable
async function collect(source: AsyncIterable<Uint8Array[]>): Promise<Uint8Array[]> {
const chunks: Uint8Array[] = [];
for await (const batch of source) {
chunks.push(...batch);
}
return chunks;
}
// Helper to collect chunks from a sync iterable
function collectSync(source: Iterable<Uint8Array[]>): Uint8Array[] {
const chunks: Uint8Array[] = [];
for (const batch of source) {
chunks.push(...batch);
}
return chunks;
}
describe('share()', () => {
describe('basic usage', () => {
it('should create a share instance [SHARE-001]', () => {
const source = from('test');
const shared = share(source);
assert.strictEqual(shared.consumerCount, 0);
assert.strictEqual(shared.bufferSize, 0);
});
it('should allow single consumer to pull data [SHARE-002]', async () => {
const source = from(['chunk1', 'chunk2']);
const shared = share(source);
const consumer = shared.pull();
const chunks = await collect(consumer);
assert.strictEqual(chunks.length, 2);
assert.strictEqual(decode(chunks[0]), 'chunk1');
assert.strictEqual(decode(chunks[1]), 'chunk2');
});
it('should allow multiple consumers to share data [SHARE-003]', async () => {
const source = from(['chunk1', 'chunk2']);
const shared = share(source, { highWaterMark: 100 });
const consumer1 = shared.pull();
const consumer2 = shared.pull();
assert.strictEqual(shared.consumerCount, 2);
// Both should receive all data
const [result1, result2] = await Promise.all([
collect(consumer1),
collect(consumer2),
]);
assert.strictEqual(result1.length, 2);
assert.strictEqual(result2.length, 2);
assert.strictEqual(decode(result1[0]), 'chunk1');
assert.strictEqual(decode(result2[0]), 'chunk1');
});
it('should handle sync source [SHARE-004]', async () => {
const source = fromSync(['sync1', 'sync2']);
const shared = share(source);
const consumer = shared.pull();
const chunks = await collect(consumer);
assert.strictEqual(chunks.length, 2);
assert.strictEqual(decode(chunks[0]), 'sync1');
});
});
describe('buffer management', () => {
it('should buffer data for slow consumers [SHARE-010]', async () => {
const source = from(['chunk1', 'chunk2', 'chunk3']);
const shared = share(source, { highWaterMark: 100 });
const consumer1 = shared.pull();
const consumer2 = shared.pull();
// Consumer1 reads all
const iter1 = consumer1[Symbol.asyncIterator]();
await iter1.next(); // chunk1
await iter1.next(); // chunk2
await iter1.next(); // chunk3
// Buffer should still have data for consumer2
// (at least until consumer2 catches up)
});
it('should respect buffer limit with strict policy [SHARE-011]', async () => {
// Create a source that yields many chunks
async function* manyChunks() {
for (let i = 0; i < 100; i++) {
yield [new TextEncoder().encode(`chunk${i}`)];
}
}
const shared = share(manyChunks(), { highWaterMark: 5, backpressure: 'strict' });
// Create fast consumer
const consumer = shared.pull();
const iter = consumer[Symbol.asyncIterator]();
// Pull a few chunks - should work
await iter.next();
await iter.next();
await iter.next();
// Cleanup
await iter.return?.();
shared.cancel();
});
it('should drop oldest with drop-oldest policy [SHARE-012]', async () => {
async function* slowSource() {
for (let i = 0; i < 5; i++) {
yield [new TextEncoder().encode(`chunk${i}`)];
await new Promise(resolve => setTimeout(resolve, 10));
}
}
const shared = share(slowSource(), {
highWaterMark: 2,
backpressure: 'drop-oldest',
});
const consumer1 = shared.pull();
const consumer2 = shared.pull();
// Fast consumer1 reads all
const chunks1: Uint8Array[] = [];
for await (const batch of consumer1) {
chunks1.push(...batch);
}
// Consumer2 may miss some due to drop-oldest
// (Not testing exact behavior as it depends on timing)
});
});
describe('cancel()', () => {
it('should cancel all consumers without error [SHARE-020]', async () => {
const source = from(['chunk1', 'chunk2']);
const shared = share(source);
const consumer = shared.pull();
shared.cancel();
const chunks = await collect(consumer);
assert.strictEqual(chunks.length, 0);
});
it('should cancel all consumers with error [SHARE-021]', async () => {
const source = from(['chunk1', 'chunk2']);
const shared = share(source);
const consumer = shared.pull();
shared.cancel(new Error('Cancelled'));
await assert.rejects(async () => {
await collect(consumer);
}, /Cancelled/);
});
it('should close source iterator [SHARE-022]', async () => {
let sourceClosed = false;
async function* trackableSource() {
try {
yield [new TextEncoder().encode('chunk1')];
yield [new TextEncoder().encode('chunk2')];
} finally {
sourceClosed = true;
}
}
const shared = share(trackableSource());
const consumer = shared.pull();
// Start iteration
const iter = consumer[Symbol.asyncIterator]();
await iter.next();
// Cancel
shared.cancel();
// Source should be closed
// (Note: depends on implementation details)
});
});
describe('Symbol.dispose', () => {
it('should cancel on dispose [SHARE-030]', async () => {
const source = from(['chunk1']);
const shared = share(source);
const consumer = shared.pull();
shared[Symbol.dispose]();
const chunks = await collect(consumer);
assert.strictEqual(chunks.length, 0);
});
});
describe('AbortSignal', () => {
it('should respect already-aborted signal [SHARE-040]', async () => {
const controller = new AbortController();
controller.abort();
const source = from(['chunk1']);
const shared = share(source, { signal: controller.signal });
const consumer = shared.pull();
const chunks = await collect(consumer);
assert.strictEqual(chunks.length, 0);
});
});
describe('source errors', () => {
it('should propagate source errors to all consumers [SHARE-050]', async () => {
async function* errorSource() {
yield [new TextEncoder().encode('chunk1')];
throw new Error('Source error');
}
const shared = share(errorSource());
const consumer1 = shared.pull();
const consumer2 = shared.pull();
await assert.rejects(async () => {
await collect(consumer1);
}, /Source error/);
// consumer2 should also see the error
});
});
});
describe('shareSync()', () => {
describe('basic usage', () => {
it('should create a sync share instance [SHARE-005]', () => {
const source = fromSync('test');
const shared = shareSync(source);
assert.strictEqual(shared.consumerCount, 0);
assert.strictEqual(shared.bufferSize, 0);
});
it('should allow single consumer to pull data [SHARE-006]', () => {
const source = fromSync(['chunk1', 'chunk2']);
const shared = shareSync(source);
const consumer = shared.pull();
const chunks = collectSync(consumer);
assert.strictEqual(chunks.length, 2);
assert.strictEqual(decode(chunks[0]), 'chunk1');
assert.strictEqual(decode(chunks[1]), 'chunk2');
});
it('should allow interleaved iteration of multiple consumers [SHARE-007]', () => {
const source = fromSync(['chunk1', 'chunk2', 'chunk3']);
const shared = shareSync(source, { highWaterMark: 100 });
const consumer1 = shared.pull();
const consumer2 = shared.pull();
const iter1 = consumer1[Symbol.iterator]();
const iter2 = consumer2[Symbol.iterator]();
// Interleave reads
const c1_1 = iter1.next();
const c2_1 = iter2.next();
const c1_2 = iter1.next();
const c2_2 = iter2.next();
assert.strictEqual(c1_1.done, false);
assert.strictEqual(c2_1.done, false);
assert.strictEqual(decode(c1_1.value[0]), 'chunk1');
assert.strictEqual(decode(c2_1.value[0]), 'chunk1');
});
});
describe('buffer limits', () => {
it('should throw on buffer overflow with strict policy [SHARE-013]', () => {
function* manyChunks() {
for (let i = 0; i < 100; i++) {
yield [new TextEncoder().encode(`chunk${i}`)];
}
}
const shared = shareSync(manyChunks(), {
highWaterMark: 5,
backpressure: 'strict',
});
// Create two consumers
const consumer1 = shared.pull();
const consumer2 = shared.pull();
// Consumer1 reads all - should throw when buffer exceeds limit
// because consumer2 hasn't read anything
assert.throws(() => {
collectSync(consumer1);
}, /Share buffer limit/);
});
it('should drop oldest with drop-oldest policy [SHARE-014]', () => {
function* source() {
for (let i = 0; i < 10; i++) {
yield [new TextEncoder().encode(`chunk${i}`)];
}
}
const shared = shareSync(source(), {
highWaterMark: 3,
backpressure: 'drop-oldest',
});
const consumer1 = shared.pull();
const consumer2 = shared.pull();
// Consumer1 reads all
const chunks1 = collectSync(consumer1);
assert.strictEqual(chunks1.length, 10);
// Consumer2 may have missed some
const chunks2 = collectSync(consumer2);
// With drop-oldest, slow consumer catches up but misses data
});
});
describe('cancel()', () => {
it('should cancel all consumers [SHARE-023]', () => {
const source = fromSync(['chunk1', 'chunk2']);
const shared = shareSync(source);
const consumer = shared.pull();
shared.cancel();
const chunks = collectSync(consumer);
assert.strictEqual(chunks.length, 0);
});
});
});
describe('Share.from()', () => {
it('should create share from async iterable [SHARE-060]', async () => {
const source = from(['chunk1', 'chunk2']);
const shared = Share.from(source);
const consumer = shared.pull();
const chunks = await collect(consumer);
assert.strictEqual(chunks.length, 2);
});
it('should create share from sync iterable [SHARE-061]', async () => {
const source = fromSync(['chunk1', 'chunk2']);
const shared = Share.from(source);
const consumer = shared.pull();
const chunks = await collect(consumer);
assert.strictEqual(chunks.length, 2);
});
});
describe('SyncShare.fromSync()', () => {
it('should create sync share from sync iterable [SHARE-062]', () => {
const source = fromSync(['chunk1', 'chunk2']);
const shared = SyncShare.fromSync(source);
const consumer = shared.pull();
const chunks = collectSync(consumer);
assert.strictEqual(chunks.length, 2);
});
});
describe('share() with transforms', () => {
// Simple transform that uppercases text
const uppercase = (chunks: Uint8Array[] | null) => {
if (chunks === null) return null;
return chunks.map(chunk => {
const text = new TextDecoder().decode(chunk).toUpperCase();
return new TextEncoder().encode(text);
});
};
// Transform that adds prefix
const prefix = (chunks: Uint8Array[] | null) => {
if (chunks === null) return null;
return chunks.map(chunk => {
const text = new TextDecoder().decode(chunk);
return new TextEncoder().encode('PREFIX:' + text);
});
};
it('should apply single transform to consumer [SHARE-070]', async () => {
const source = from(['hello', 'world']);
const shared = share(source, { highWaterMark: 100 });
const consumer = shared.pull(uppercase);
const chunks = await collect(consumer);
assert.strictEqual(chunks.length, 2);
assert.strictEqual(decode(chunks[0]), 'HELLO');
assert.strictEqual(decode(chunks[1]), 'WORLD');
});
it('should apply multiple transforms in order [SHARE-071]', async () => {
const source = from(['hello']);
const shared = share(source, { highWaterMark: 100 });
const consumer = shared.pull(uppercase, prefix);
const chunks = await collect(consumer);
assert.strictEqual(chunks.length, 1);
assert.strictEqual(decode(chunks[0]), 'PREFIX:HELLO');
});
it('should allow different transforms per consumer [SHARE-072]', async () => {
const source = from(['hello', 'world']);
const shared = share(source, { highWaterMark: 100 });
const consumer1 = shared.pull(uppercase);
const consumer2 = shared.pull(prefix);
const consumer3 = shared.pull(); // no transform
const [result1, result2, result3] = await Promise.all([
collect(consumer1),
collect(consumer2),
collect(consumer3),
]);
assert.strictEqual(decode(result1[0]), 'HELLO');
assert.strictEqual(decode(result2[0]), 'PREFIX:hello');
assert.strictEqual(decode(result3[0]), 'hello');
});
it('should support transforms with options [SHARE-073]', async () => {
const source = from(['hello']);
const shared = share(source);
const controller = new AbortController();
const consumer = shared.pull(uppercase, { signal: controller.signal });
const chunks = await collect(consumer);
assert.strictEqual(decode(chunks[0]), 'HELLO');
});
});
describe('shareSync() with transforms', () => {
// Simple sync transform that uppercases text
const uppercase = (chunks: Uint8Array[] | null) => {
if (chunks === null) return null;
return chunks.map(chunk => {
const text = new TextDecoder().decode(chunk).toUpperCase();
return new TextEncoder().encode(text);
});
};
it('should apply single transform to sync consumer [SHARE-074]', () => {
const source = fromSync(['hello', 'world']);
const shared = shareSync(source, { highWaterMark: 100 });
const consumer = shared.pull(uppercase);
const chunks = collectSync(consumer);
assert.strictEqual(chunks.length, 2);
assert.strictEqual(decode(chunks[0]), 'HELLO');
assert.strictEqual(decode(chunks[1]), 'WORLD');
});
});