Skip to content

Commit

Permalink
test: fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andymina committed Oct 27, 2022
1 parent 232eaaa commit d9206e8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
6 changes: 4 additions & 2 deletions src/change_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -737,13 +737,15 @@ export class ChangeStream<
}, callback);
}

async *[Symbol.asyncIterator](): AsyncGenerator<TChange, void, never> {
async *[Symbol.asyncIterator](): AsyncGenerator<TChange, void, void> {
if (this.closed) {
return;
}

try {
while (await this.hasNext()) {
// Change streams run indefinitely as long as errors are resumable
// So the only loop breaking condition is if `next()` throws
while (true) {
yield await this.next();
}
} finally {
Expand Down
22 changes: 7 additions & 15 deletions test/integration/change-streams/change_stream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,7 @@ describe('Change Streams', function () {
const docs = [{ city: 'New York City' }, { city: 'Seattle' }, { city: 'Boston' }];
await collection.insertMany(docs);

await changeStreamIterator.next();
await changeStreamIterator.return();
expect(changeStream.closed).to.be.true;
expect(changeStream.cursor.closed).to.be.true;
Expand Down Expand Up @@ -1074,12 +1075,8 @@ describe('Change Streams', function () {
changeStream.on('change', sinon.stub());
const changeStreamIterator = changeStream[Symbol.asyncIterator]();

try {
await changeStreamIterator.next();
expect.fail('Async iterator was used with emitter-based iteration');
} catch (error) {
expect(error).to.be.instanceOf(MongoAPIError);
}
const error = await changeStreamIterator.next().catch(e => e);
expect(error).to.be.instanceOf(MongoAPIError);
}
);

Expand Down Expand Up @@ -2465,15 +2462,10 @@ describe('ChangeStream resumability', function () {
} as FailPoint);

await collection.insertOne({ city: 'New York City' });
try {
await changeStreamIterator.next();
expect.fail(
'Change stream did not throw unresumable error and did not produce any events'
);
} catch (error) {
expect(error).to.be.instanceOf(MongoServerError);
expect(aggregateEvents).to.have.lengthOf(1);
}

const error = await changeStreamIterator.next().catch(e => e);
expect(error).to.be.instanceOf(MongoServerError);
expect(aggregateEvents).to.have.lengthOf(1);
}
);
});
Expand Down

0 comments on commit d9206e8

Please sign in to comment.