Skip to content

Commit a078789

Browse files
committed
test: add test for closing change stream
1 parent 78e8cfb commit a078789

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

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

+40-3
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ describe('Change Streams', function () {
5353
let changeStream: ChangeStream;
5454
let db: Db;
5555

56+
const is4_2Server = (serverVersion: string) =>
57+
gte(serverVersion, '4.2.0') && lt(serverVersion, '4.3.0');
58+
5659
beforeEach(async function () {
5760
const configuration = this.configuration;
5861
client = configuration.newClient();
@@ -999,6 +1002,41 @@ describe('Change Streams', function () {
9991002
changeStream.close();
10001003
}
10011004
);
1005+
1006+
context('when an error is thrown', function () {
1007+
it(
1008+
'should close the change stream',
1009+
{ requires: { topology: '!single', mongodb: '>=4.2' } },
1010+
async function () {
1011+
changeStream = collection.watch([]);
1012+
await initIteratorMode(changeStream);
1013+
const changeStreamIterator = changeStream[Symbol.asyncIterator]();
1014+
1015+
const unresumableErrorCode = 1000;
1016+
await client.db('admin').command({
1017+
configureFailPoint: is4_2Server(this.configuration.version)
1018+
? 'failCommand'
1019+
: 'failGetMoreAfterCursorCheckout',
1020+
mode: { times: 1 },
1021+
data: {
1022+
failCommands: ['getMore'],
1023+
errorCode: unresumableErrorCode
1024+
}
1025+
} as FailPoint);
1026+
1027+
await collection.insertOne({ city: 'New York City' });
1028+
try {
1029+
const change = await changeStreamIterator.next();
1030+
expect.fail(
1031+
'Change stream did not throw unresumable error and did not produce any events'
1032+
);
1033+
} catch (error) {
1034+
expect(changeStream.closed).to.be.true;
1035+
expect(changeStream.cursor.closed).to.be.true;
1036+
}
1037+
}
1038+
);
1039+
});
10021040
});
10031041
});
10041042

@@ -2271,6 +2309,8 @@ describe('ChangeStream resumability', function () {
22712309
{ requires: { topology: '!single', mongodb: '>=4.2' } },
22722310
async function () {
22732311
changeStream = collection.watch([]);
2312+
await initIteratorMode(changeStream);
2313+
const changeStreamIterator = changeStream[Symbol.asyncIterator]();
22742314

22752315
const unresumableErrorCode = 1000;
22762316
await client.db('admin').command({
@@ -2284,9 +2324,6 @@ describe('ChangeStream resumability', function () {
22842324
}
22852325
} as FailPoint);
22862326

2287-
await initIteratorMode(changeStream);
2288-
const changeStreamIterator = changeStream[Symbol.asyncIterator]();
2289-
22902327
await collection.insertOne({ city: 'New York City' });
22912328
try {
22922329
const change = await changeStreamIterator.next();

0 commit comments

Comments
 (0)