Skip to content

Commit aa1c757

Browse files
committed
chore: fix failing tests
1 parent 26eb5a7 commit aa1c757

4 files changed

Lines changed: 238 additions & 245 deletions

File tree

asynciterator.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1626,7 +1626,7 @@ export class MultiTransformIterator<S, D = S> extends TransformIterator<S, D> {
16261626
export class UnionIterator<T> extends AsyncIterator<T> {
16271627
private _sources : AsyncIterator<AsyncIterator<T>>;
16281628
private buffer = new LinkedList<AsyncIterator<T>>();
1629-
private _sourceStarted: boolean;
1629+
private _sourceStarted: boolean = false;
16301630
private _destroySources: boolean;
16311631
private _maxBufferSize: number;
16321632

@@ -1640,20 +1640,15 @@ export class UnionIterator<T> extends AsyncIterator<T> {
16401640
AsyncIteratorOrArray<Promise<AsyncIterator<T>>> |
16411641
AsyncIteratorOrArray<MaybePromise<AsyncIterator<T>>> |
16421642
MaybePromise<AsyncIteratorOrArray<MaybePromise<IterableSource<T>>>>,
1643-
options: BufferedIteratorOptions & { destroySources?: boolean } = {}) {
1643+
options: { destroySources?: boolean, maxParallelIterators?: number } = {}) {
16441644
super();
1645-
this._sources = wrap(sources).map<AsyncIterator<T>>(wrap);
1645+
this._addSource(this._sources = wrap(sources).map<AsyncIterator<T>>(wrap));
16461646

16471647
// Set other options
16481648
this._destroySources = options.destroySources !== false;
1649-
this._maxBufferSize = options.maxBufferSize || Infinity;
1649+
this._maxBufferSize = options.maxParallelIterators || Infinity;
16501650

1651-
// TODO: Get rid of this when merging #45
1652-
this._sourceStarted = options.autoStart !== false;
1653-
if (this._sources.done && this._sourceStarted)
1654-
this.close();
1655-
else
1656-
this.readable = this._sources.readable;
1651+
this.readable = this._sources.readable;
16571652
}
16581653

16591654
// Adds the given source to the internal sources array
@@ -1729,8 +1724,8 @@ export class UnionIterator<T> extends AsyncIterator<T> {
17291724
source.destroy();
17301725
}
17311726
this.buffer.clear();
1732-
this._sources.destroy();
17331727
}
1728+
this._sources.destroy();
17341729
super.close();
17351730
}
17361731
}

perf/UnionIterator-perf.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@ console.timeEnd('For loop with 5x10^9 elems');
1313
console.time('UnionIterator 2x10^7 iterators');
1414
for (let i = 0; i < 5; i++) {
1515
it = new UnionIterator([range(0, 10_000_000), range(0, 10_000_000)]);
16-
await promisifyEventEmitter(it.on('data', () => {}));
16+
await promisifyEventEmitter(it.on('data', () => { /* noop */ }));
1717
}
1818
console.timeEnd('UnionIterator 2x10^7 iterators');
1919

2020
console.time('UnionIterator 1000x20_000 iterators');
2121
for (let i = 0; i < 5; i++) {
2222
it = new UnionIterator(range(0, 1000).map(() => range(0, 20_000)));
23-
await promisifyEventEmitter(it.on('data', () => {}));
23+
await promisifyEventEmitter(it.on('data', () => { /* noop */ }));
2424
}
2525
console.timeEnd('UnionIterator 1000x20_000 iterators');
2626

2727
console.time('UnionIterator 1000x20_000 iterators - maxBufferSize of 1');
2828
for (let i = 0; i < 5; i++) {
2929
it = new UnionIterator(range(0, 1000).map(() => range(0, 20_000)));
30-
await promisifyEventEmitter(it.on('data', () => {}));
30+
await promisifyEventEmitter(it.on('data', () => { /* noop */ }));
3131
}
3232
console.timeEnd('UnionIterator 1000x20_000 iterators - maxBufferSize of 1');

test/UnionIterator-test.js

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,18 @@ describe('UnionIterator', () => {
8686
iterator = new UnionIterator(sources);
8787
});
8888

89-
it('should have ended', () => {
90-
iterator.ended.should.be.true;
89+
describe('before reading', () => {
90+
it('should not have ended', () => {
91+
iterator.ended.should.be.false;
92+
});
9193
});
9294
});
9395

9496
describe('when constructed with an array of 0 sources without autoStart', () => {
9597
let iterator;
9698
before(() => {
9799
const sources = [];
98-
iterator = new UnionIterator(sources, { autoStart: false });
100+
iterator = new UnionIterator(sources);
99101
});
100102

101103
describe('before reading', () => {
@@ -147,7 +149,7 @@ describe('UnionIterator', () => {
147149
});
148150

149151
it('should have ended', () => {
150-
iterator.ended.should.be.true;
152+
iterator.ended.should.be.false;
151153
});
152154
});
153155

@@ -159,15 +161,15 @@ describe('UnionIterator', () => {
159161
});
160162

161163
it('should have ended', () => {
162-
iterator.ended.should.be.true;
164+
iterator.ended.should.be.false;
163165
});
164166
});
165167

166168
describe('when constructed with an iterator of 0 sources without autoStart', () => {
167169
let iterator;
168170
before(() => {
169171
const sources = [];
170-
iterator = new UnionIterator(new ArrayIterator(sources), { autoStart: false });
172+
iterator = new UnionIterator(new ArrayIterator(sources));
171173
});
172174

173175
describe('before reading', () => {
@@ -237,10 +239,6 @@ describe('UnionIterator', () => {
237239
});
238240

239241
describe('before reading', () => {
240-
it('should have read the sources', () => {
241-
sourceIterator.read.should.have.been.called;
242-
});
243-
244242
it('should not have ended', () => {
245243
iterator.ended.should.be.false;
246244
});
@@ -268,12 +266,12 @@ describe('UnionIterator', () => {
268266
const sources = [Promise.resolve(range(0, 2)), range(3, 6)];
269267
sourceIterator = new ArrayIterator(sources);
270268
sinon.spy(sourceIterator, 'read');
271-
iterator = new UnionIterator(sourceIterator, { autoStart: true });
269+
iterator = new UnionIterator(sourceIterator);
272270
});
273271

274272
describe('before reading', () => {
275-
it('should have read the sources', () => {
276-
sourceIterator.read.should.have.been.called;
273+
it('should not have read the sources', () => {
274+
sourceIterator.read.should.not.have.been.called;
277275
});
278276

279277
it('should not have ended', () => {
@@ -303,7 +301,7 @@ describe('UnionIterator', () => {
303301
const sources = [range(0, 2), range(3, 6)];
304302
sourceIterator = new ArrayIterator(sources);
305303
sinon.spy(sourceIterator, 'read');
306-
iterator = new UnionIterator(sourceIterator, { autoStart: false });
304+
iterator = new UnionIterator(sourceIterator);
307305
});
308306

309307
describe('before reading', () => {
@@ -342,7 +340,7 @@ describe('UnionIterator', () => {
342340
const sources = [Promise.resolve(range(0, 2)), range(3, 6)];
343341
sourceIterator = new ArrayIterator(sources);
344342
sinon.spy(sourceIterator, 'read');
345-
iterator = new UnionIterator(sourceIterator, { autoStart: false });
343+
iterator = new UnionIterator(sourceIterator);
346344
});
347345

348346
describe('before reading', () => {
@@ -387,6 +385,9 @@ describe('UnionIterator', () => {
387385
});
388386

389387
it('should emit an error when the first iterator emits an error', () => {
388+
iterator.read().should.eql(0);
389+
iterator.read().should.eql(1);
390+
390391
const error = new Error('error');
391392
const callback = sinon.spy();
392393
iterator.on('error', callback);
@@ -396,6 +397,11 @@ describe('UnionIterator', () => {
396397
});
397398

398399
it('should emit an error when the second iterator emits an error', () => {
400+
iterator.read().should.eql(0);
401+
iterator.read().should.eql(1);
402+
iterator.read().should.eql(2);
403+
iterator.read().should.eql(3);
404+
399405
const error = new Error('error');
400406
const callback = sinon.spy();
401407
iterator.on('error', callback);
@@ -408,23 +414,9 @@ describe('UnionIterator', () => {
408414
(await toArray(iterator)).should.be.instanceof(Array);
409415
});
410416

411-
it('should allow the _read method to be called multiple times', () => {
412-
iterator._read(1, noop);
413-
iterator._read(1, noop);
414-
});
415-
416417
it('should make a round-robin union of the data elements', async () => {
417418
(await toArray(iterator)).sort().should.eql([0, 1, 2, 3, 4, 5, 6]);
418419
});
419-
420-
it('should destroy the sources when closing', async () => {
421-
iterator.close();
422-
423-
await new Promise(resolve => iterator.on('end', resolve));
424-
425-
sources[0].closed.should.be.true;
426-
sources[1].closed.should.be.true;
427-
});
428420
});
429421

430422
describe('a UnionIterator with two sources without destroySources', () => {
@@ -555,9 +547,18 @@ describe('UnionIterator', () => {
555547
await new Promise(resolve => iterator.on('end', resolve));
556548

557549
sourcesIterator.closed.should.be.true;
550+
});
551+
552+
it('should close iterators that have started to be read', async () => {
553+
iterator.read();
554+
iterator.close();
555+
556+
await new Promise(resolve => iterator.on('end', resolve));
557+
558+
sourcesIterator.closed.should.be.true;
558559

559560
sources[0].closed.should.be.true;
560-
sources[1].closed.should.be.true;
561+
sources[1].closed.should.be.false;
561562
});
562563
});
563564

@@ -586,9 +587,6 @@ describe('UnionIterator', () => {
586587
await new Promise(resolve => iterator.on('end', resolve));
587588

588589
sourcesIterator.closed.should.be.true;
589-
590-
sources[0].closed.should.be.true;
591-
sources[1].closed.should.be.true;
592590
});
593591
});
594592

0 commit comments

Comments
 (0)