Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cursor): bubble up resumeTokenChanged event from change streams #13736

Merged
merged 1 commit into from Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/cursor/ChangeStream.js
Expand Up @@ -6,6 +6,12 @@

const EventEmitter = require('events').EventEmitter;

/*!
* ignore
*/

const driverChangeStreamEvents = ['close', 'change', 'end', 'error', 'resumeTokenChanged'];

/*!
* ignore
*/
Expand Down Expand Up @@ -52,7 +58,7 @@ class ChangeStream extends EventEmitter {
this.closed = true;
});

['close', 'change', 'end', 'error'].forEach(ev => {
driverChangeStreamEvents.forEach(ev => {
this.driverChangeStream.on(ev, data => {
// Sometimes Node driver still polls after close, so
// avoid any uncaught exceptions due to closed change streams
Expand All @@ -75,7 +81,7 @@ class ChangeStream extends EventEmitter {
this.closed = true;
});

['close', 'change', 'end', 'error'].forEach(ev => {
driverChangeStreamEvents.forEach(ev => {
this.driverChangeStream.on(ev, data => {
// Sometimes Node driver still polls after close, so
// avoid any uncaught exceptions due to closed change streams
Expand Down
16 changes: 15 additions & 1 deletion test/model.test.js
Expand Up @@ -3404,7 +3404,7 @@ describe('Model', function() {
let listener;

before(function() {
if (!process.env.REPLICA_SET) {
if (!process.env.REPLICA_SET && !process.env.START_REPLICA_SET) {
this.skip();
}
});
Expand Down Expand Up @@ -3436,6 +3436,20 @@ describe('Model', function() {
doc._id.toHexString());
});

it('bubbles up resumeTokenChanged events (gh-13607)', async function() {
const MyModel = db.model('Test', new Schema({ name: String }));

const resumeTokenChangedEvent = new Promise(resolve => {
changeStream = MyModel.watch();
listener = data => resolve(data);
changeStream.once('resumeTokenChanged', listener);
});

await MyModel.create({ name: 'test' });
const { _data } = await resumeTokenChangedEvent;
assert.ok(_data);
});

it('using next() and hasNext() (gh-11527)', async function() {
const MyModel = db.model('Test', new Schema({ name: String }));

Expand Down