Skip to content

Commit 30b7e10

Browse files
committed
test: add test for ignoring errors in close
1 parent cada541 commit 30b7e10

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

@@ -2303,8 +2314,6 @@ describe('ChangeStream resumability', function () {
23032314
await changeStreamIterator.next();
23042315

23052316
expect(aggregateEvents).to.have.lengthOf(2);
2306-
2307-
changeStream.close();
23082317
}
23092318
);
23102319
}
@@ -2338,8 +2347,6 @@ describe('ChangeStream resumability', function () {
23382347
await changeStreamIterator.next();
23392348

23402349
expect(aggregateEvents).to.have.lengthOf(2);
2341-
2342-
changeStream.close();
23432350
}
23442351
);
23452352
}
@@ -2408,8 +2415,6 @@ describe('ChangeStream resumability', function () {
24082415
expect(error).to.be.instanceOf(MongoServerError);
24092416
expect(aggregateEvents).to.have.lengthOf(1);
24102417
}
2411-
2412-
changeStream.close();
24132418
}
24142419
);
24152420
});

0 commit comments

Comments
 (0)