Skip to content

Commit 232eaaa

Browse files
committed
test: add test for ignoring errors in close
1 parent 9690249 commit 232eaaa

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

test/integration/change-streams/change_stream.test.ts

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ describe('Change Streams', function () {
7373
});
7474

7575
afterEach(async () => {
76+
sinon.restore();
7677
await changeStream.close();
7778
await client.close();
7879
await mock.cleanup();
@@ -952,7 +953,7 @@ describe('Change Streams', function () {
952953
'This test only worked because of timing, changeStream.close does not remove the change listener';
953954
});
954955

955-
context('iterator api', function () {
956+
describe('iterator api', function () {
956957
describe('#tryNext()', function () {
957958
it('should return null on single iteration of empty cursor', {
958959
metadata: { requires: { topology: 'replicaset' } },
@@ -998,8 +999,6 @@ describe('Change Streams', function () {
998999
const { fullDocument } = change.value;
9991000
expect(fullDocument.city).to.equal(doc.city);
10001001
}
1001-
1002-
changeStream.close();
10031002
}
10041003
);
10051004

@@ -1009,18 +1008,12 @@ describe('Change Streams', function () {
10091008
async function () {
10101009
changeStream = collection.watch([]);
10111010
await initIteratorMode(changeStream);
1011+
const changeStreamIterator = changeStream[Symbol.asyncIterator]();
10121012

10131013
const docs = [{ city: 'New York City' }, { city: 'Seattle' }, { city: 'Boston' }];
10141014
await collection.insertMany(docs);
10151015

1016-
const changeStreamAsyncIteratorHelper = async function (changeStream: ChangeStream) {
1017-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
1018-
for await (const change of changeStream) {
1019-
return;
1020-
}
1021-
};
1022-
1023-
await changeStreamAsyncIteratorHelper(changeStream);
1016+
await changeStreamIterator.return();
10241017
expect(changeStream.closed).to.be.true;
10251018
expect(changeStream.cursor.closed).to.be.true;
10261019
}
@@ -1114,6 +1107,24 @@ describe('Change Streams', function () {
11141107
}
11151108
}
11161109
);
1110+
1111+
it(
1112+
'ignores errors thrown from close',
1113+
{ requires: { topology: '!single' } },
1114+
async function () {
1115+
changeStream = collection.watch([]);
1116+
await initIteratorMode(changeStream);
1117+
const changeStreamIterator = changeStream[Symbol.asyncIterator]();
1118+
1119+
sinon.stub(changeStream.cursor, 'close').throws(new MongoAPIError('testing'));
1120+
1121+
try {
1122+
await changeStreamIterator.return();
1123+
} catch (error) {
1124+
expect.fail('Async iterator threw an error on close');
1125+
}
1126+
}
1127+
);
11171128
});
11181129
});
11191130

@@ -2362,8 +2373,6 @@ describe('ChangeStream resumability', function () {
23622373
await changeStreamIterator.next();
23632374

23642375
expect(aggregateEvents).to.have.lengthOf(2);
2365-
2366-
changeStream.close();
23672376
}
23682377
);
23692378
}
@@ -2397,8 +2406,6 @@ describe('ChangeStream resumability', function () {
23972406
await changeStreamIterator.next();
23982407

23992408
expect(aggregateEvents).to.have.lengthOf(2);
2400-
2401-
changeStream.close();
24022409
}
24032410
);
24042411
}
@@ -2467,8 +2474,6 @@ describe('ChangeStream resumability', function () {
24672474
expect(error).to.be.instanceOf(MongoServerError);
24682475
expect(aggregateEvents).to.have.lengthOf(1);
24692476
}
2470-
2471-
changeStream.close();
24722477
}
24732478
);
24742479
});

0 commit comments

Comments
 (0)