-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathchange_streams.prose.test.ts
1003 lines (907 loc) · 32.4 KB
/
change_streams.prose.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { expect } from 'chai';
import { once } from 'events';
import * as sinon from 'sinon';
import { setTimeout } from 'timers';
import {
AbstractCursor,
type ChangeStream,
type CommandFailedEvent,
type CommandStartedEvent,
type CommandSucceededEvent,
type Document,
isHello,
LEGACY_HELLO_COMMAND,
Long,
MongoNetworkError,
ObjectId,
Timestamp
} from '../../mongodb';
import * as mock from '../../tools/mongodb-mock/index';
import { getSymbolFrom } from '../../tools/utils';
import { setupDatabase } from '../shared';
/**
* Triggers a fake resumable error on a change stream
* changeStream
* [delay] optional delay before triggering error
* onClose callback when cursor closed due this error
*/
function triggerResumableError(changeStream: ChangeStream, onClose?: () => void);
function triggerResumableError(changeStream: ChangeStream, delay: number, onClose?: () => void);
function triggerResumableError(
changeStream: ChangeStream,
delay: number | (() => void),
onClose?: () => void
) {
if (typeof delay === 'function') {
onClose = delay;
delay = undefined;
}
const stub = sinon.stub(changeStream.cursor, 'close');
stub.callsFake(async function () {
stub.wrappedMethod.call(this);
stub.restore();
onClose();
});
function triggerError() {
const cursorStream = changeStream.cursorStream;
if (cursorStream) {
cursorStream.emit('error', new MongoNetworkError('error triggered from test'));
return;
}
const nextStub = sinon.stub(changeStream.cursor, 'next').callsFake(async function () {
callback(new MongoNetworkError('error triggered from test'));
nextStub.restore();
});
changeStream.next(() => {
// ignore
});
}
if (typeof delay === 'number') {
setTimeout(triggerError, delay);
return;
}
triggerError();
}
const initIteratorMode = async (cs: ChangeStream) => {
const kInit = getSymbolFrom(AbstractCursor.prototype, 'kInit');
const initEvent = once(cs.cursor, 'init');
await cs.cursor[kInit]();
await initEvent;
return;
};
/** Waits for a change stream to start */
function waitForStarted(changeStream, callback) {
changeStream.cursor.once('init', () => {
callback();
});
}
// Define the pipeline processing changes
const pipeline = [
{ $addFields: { addedField: 'This is a field added using $addFields' } },
{ $project: { documentKey: false } },
{ $addFields: { comment: 'The documentKey field has been projected out of this document.' } }
];
describe('Change Stream prose tests', function () {
before(async function () {
return await setupDatabase(this.configuration, ['integration_tests']);
});
beforeEach(async function () {
const configuration = this.configuration;
const client = configuration.newClient();
const db = client.db('integration_tests');
try {
await db.createCollection('test');
} catch {
// ns already exists, don't care
} finally {
await client.close();
}
});
afterEach(async () => await mock.cleanup());
// TODO(NODE-3884): Add tests 1-4, 6-8. (#5 is removed from spec)
// Note: #3 is partially contained in change_stream.test.js > Change Stream Resume Error Tests
// 9. $changeStream stage for ChangeStream against a server >=4.0 and <4.0.7 that has not received
// any results yet MUST include a startAtOperationTime option when resuming a change stream.
it('should include a startAtOperationTime field when resuming if no changes have been received', {
metadata: { requires: { topology: 'replicaset', mongodb: '>=4.0 <4.0.7' } },
test: function (done) {
const configuration = this.configuration;
const OPERATION_TIME = new Timestamp({ i: 4, t: 1501511802 });
const makeHello = server => ({
__nodejs_mock_server__: true,
[LEGACY_HELLO_COMMAND]: true,
secondary: false,
me: server.uri(),
primary: server.uri(),
tags: { loc: 'ny' },
setName: 'rs',
setVersion: 1,
electionId: new ObjectId(0),
maxBsonObjectSize: 16777216,
maxMessageSizeBytes: 48000000,
maxWriteBatchSize: 1000,
localTime: new Date(),
maxWireVersion: 7,
minWireVersion: 0,
ok: 1,
hosts: [server.uri()],
operationTime: OPERATION_TIME,
$clusterTime: {
clusterTime: OPERATION_TIME
}
});
const AGGREGATE_RESPONSE = {
ok: 1,
cursor: {
firstBatch: [],
id: new Long('9064341847921713401'),
ns: 'test.test'
},
operationTime: OPERATION_TIME,
$clusterTime: {
clusterTime: OPERATION_TIME
}
};
const CHANGE_DOC = {
_id: {
ts: OPERATION_TIME,
ns: 'integration_tests.docsDataEvent',
_id: new ObjectId('597f407a8fd4abb616feca93')
},
operationType: 'insert',
ns: {
db: 'integration_tests',
coll: 'docsDataEvent'
},
fullDocument: {
_id: new ObjectId('597f407a8fd4abb616feca93'),
a: 1,
counter: 0
}
};
const GET_MORE_RESPONSE = {
ok: 1,
cursor: {
nextBatch: [CHANGE_DOC],
id: new Long('9064341847921713401'),
ns: 'test.test'
},
cursorId: new Long('9064341847921713401')
};
const dbName = 'integration_tests';
const collectionName = 'resumeWithStartAtOperationTime';
const connectOptions = { monitorCommands: true };
let getMoreCounter = 0;
let changeStream;
let server;
let client;
let finish = (err?: Error) => {
finish = () => {
// ignore
};
Promise.resolve()
.then(() => changeStream && changeStream.close())
.then(() => client && client.close())
.then(() => done(err));
};
function primaryServerHandler(request) {
try {
const doc = request.document;
if (isHello(doc)) {
return request.reply(makeHello(server));
} else if (doc.aggregate) {
return request.reply(AGGREGATE_RESPONSE);
} else if (doc.getMore) {
if (getMoreCounter++ === 0) {
request.reply({ ok: 0 });
return;
}
request.reply(GET_MORE_RESPONSE);
} else if (doc.endSessions) {
request.reply({ ok: 1 });
} else if (doc.killCursors) {
request.reply({ ok: 1 });
}
} catch (e) {
finish(e);
}
}
const started = [];
mock
.createServer()
.then(_server => (server = _server))
.then(() => server.setMessageHandler(primaryServerHandler))
.then(() => (client = configuration.newClient(`mongodb://${server.uri()}`, connectOptions)))
.then(() => {
client.on('commandStarted', e => {
if (e.commandName === 'aggregate') {
started.push(e);
}
});
})
.then(() => client.db(dbName))
.then(db => db.collection(collectionName))
.then(col => col.watch(pipeline))
.then(_changeStream => (changeStream = _changeStream))
.then(() => changeStream.next())
.then(() => {
const first = started[0].command;
expect(first).to.have.nested.property('pipeline[0].$changeStream');
const firstStage = first.pipeline[0].$changeStream;
expect(firstStage).to.not.have.property('resumeAfter');
expect(firstStage).to.not.have.property('startAtOperationTime');
const second = started[1].command;
expect(second).to.have.nested.property('pipeline[0].$changeStream');
const secondStage = second.pipeline[0].$changeStream;
expect(secondStage).to.not.have.property('resumeAfter');
expect(secondStage).to.have.property('startAtOperationTime');
expect(secondStage.startAtOperationTime.equals(OPERATION_TIME)).to.be.ok;
})
.then(
() => finish(),
err => finish(err)
);
}
});
// 10 removed by spec
describe('Change Stream prose 11-14', () => {
class MockServerManager {
config: any;
cmdList: Set<string>;
database: string;
collection: string;
ns: string;
_timestampCounter: number;
cursorId: Long;
commandIterators: any;
promise: Promise<any>;
server: any;
client: any;
apm: {
started: CommandStartedEvent[];
succeeded: CommandSucceededEvent[];
failed: CommandFailedEvent[];
};
changeStream: any;
resumeTokenChangedEvents: any[];
namespace: any;
constructor(config, commandIterators) {
this.config = config;
this.cmdList = new Set([
LEGACY_HELLO_COMMAND,
'hello',
'endSessions',
'aggregate',
'getMore'
]);
this.database = 'test_db';
this.collection = 'test_coll';
this.ns = `${this.database}.${this.collection}`;
this._timestampCounter = 0;
this.cursorId = new Long('9064341847921713401');
this.commandIterators = commandIterators;
this.promise = this.init();
// Handler for the legacy hello command
this[LEGACY_HELLO_COMMAND] = function () {
return this.hello();
};
}
async init() {
const server = await mock.createServer();
this.server = server;
this.server.setMessageHandler(request => {
const doc = request.document;
const opname = Object.keys(doc)[0];
let response = { ok: 0 };
if (this.cmdList.has(opname) && this[opname]) {
response = this[opname](doc);
}
request.reply(this.applyOpTime(response));
});
this.client = this.config.newClient(this.mongodbURI, {
monitorCommands: true,
serverApi: null // TODO(NODE-3807): remove resetting serverApi when the usage of mongodb mock server is removed
});
this.apm = { started: [], succeeded: [], failed: [] };
(
[
['commandStarted', this.apm.started],
['commandSucceeded', this.apm.succeeded],
['commandFailed', this.apm.failed]
] as const
).forEach(opts => {
const eventName = opts[0];
const target = opts[1];
this.client.on(eventName, e => {
if (e.commandName === 'aggregate' || e.commandName === 'getMore') {
target.push(e);
}
});
});
}
makeChangeStream(options?: Document) {
this.changeStream = this.client
.db(this.database)
.collection(this.collection)
.watch(options);
this.resumeTokenChangedEvents = [];
this.changeStream.on('resumeTokenChanged', resumeToken => {
this.resumeTokenChangedEvents.push({ resumeToken });
});
return this.changeStream;
}
teardown(e?: Error) {
let promise = Promise.resolve();
if (this.changeStream) {
promise = promise.then(() => this.changeStream.close()).catch();
}
if (this.client) {
promise = promise.then(() => this.client.close()).catch();
}
return promise.then(function () {
if (e) {
throw e;
}
});
}
ready() {
return this.promise;
}
get mongodbURI() {
return `mongodb://${this.server.uri()}`;
}
// Handlers for specific commands
hello() {
const uri = this.server.uri();
return Object.assign({}, mock.HELLO, {
[LEGACY_HELLO_COMMAND]: true,
secondary: false,
me: uri,
primary: uri,
setName: 'rs',
localTime: new Date(),
ok: 1,
hosts: [uri]
});
}
endSessions() {
return { ok: 1 };
}
aggregate() {
let cursor;
try {
cursor = this._buildCursor('aggregate', 'firstBatch');
} catch (e) {
return { ok: 0, errmsg: e.message };
}
return {
ok: 1,
cursor
};
}
getMore() {
let cursor;
try {
cursor = this._buildCursor('getMore', 'nextBatch');
} catch (e) {
return { ok: 0, errmsg: e.message };
}
return {
ok: 1,
cursor,
cursorId: this.cursorId
};
}
// Helpers
timestamp() {
return new Timestamp({ i: this._timestampCounter++, t: this._timestampCounter });
}
applyOpTime(obj) {
const operationTime = this.timestamp();
return Object.assign({}, obj, {
$clusterTime: { clusterTime: operationTime },
operationTime
});
}
_buildCursor(type, batchKey) {
const config = this.commandIterators[type].next().value;
if (!config) {
throw new Error('no more config for ' + type);
}
const batch = Array.from({ length: config.numDocuments || 0 }).map(() =>
this.changeEvent()
);
const cursor: Document = {
[batchKey]: batch,
id: this.cursorId,
ns: this.ns
};
if (config.postBatchResumeToken) {
cursor.postBatchResumeToken = this.resumeToken();
}
return cursor;
}
changeEvent(operationType?: string, fullDocument?: Document) {
fullDocument = fullDocument || {};
return {
_id: this.resumeToken(),
operationType,
ns: {
db: this.database,
coll: this.collection
},
fullDocument
};
}
resumeToken() {
return {
ts: this.timestamp(),
ns: this.namespace,
_id: new ObjectId()
};
}
}
// 11. For a ChangeStream under these conditions:
// Running against a server >=4.0.7.
// The batch is empty or has been iterated to the last document.
// Expected result:
// getResumeToken must return the postBatchResumeToken from the current command response.
describe('for emptied batch on server >= 4.0.7', function () {
it('must return the postBatchResumeToken from the current command response', function () {
const manager = new MockServerManager(this.configuration, {
aggregate: (function* () {
yield { numDocuments: 0, postBatchResumeToken: true, cursor: { firstBatch: [] } };
})(),
getMore: (function* () {
yield { numDocuments: 1, postBatchResumeToken: true, cursor: { nextBatch: [{}] } };
})()
});
return manager
.ready()
.then(() => {
return manager.makeChangeStream().next();
})
.then(
() => manager.teardown(),
err => manager.teardown(err)
)
.then(() => {
const tokens = manager.resumeTokenChangedEvents.map(e => e.resumeToken);
const successes = manager.apm.succeeded.map(e => {
try {
// @ts-expect-error: e.reply is unknown
return e.reply.cursor;
} catch (e) {
return {};
}
});
expect(successes).to.have.a.lengthOf(2);
expect(successes[0]).to.have.a.property('postBatchResumeToken');
expect(successes[1]).to.have.a.property('postBatchResumeToken');
expect(successes[1]).to.have.a.nested.property('nextBatch[0]._id');
expect(tokens).to.have.a.lengthOf(2);
expect(tokens[0]).to.deep.equal(successes[0].postBatchResumeToken);
expect(tokens[1])
.to.deep.equal(successes[1].postBatchResumeToken)
.and.to.not.deep.equal(successes[1].nextBatch[0]._id);
});
});
});
// 12. For a ChangeStream under these conditions:
// Running against a server <4.0.7.
// The batch is empty or has been iterated to the last document.
// Expected result:
// getResumeToken must return the _id of the last document returned if one exists.
// getResumeToken must return resumeAfter from the initial aggregate if the option was specified.
// If ``resumeAfter`` was not specified, the ``getResumeToken`` result must be empty.
describe('for emptied batch on server <= 4.0.7', function () {
it('must return the _id of the last document returned if one exists', function () {
const manager = new MockServerManager(this.configuration, {
aggregate: (function* () {
yield { numDocuments: 0, postBatchResumeToken: false };
})(),
getMore: (function* () {
yield { numDocuments: 1, postBatchResumeToken: false };
})()
});
return manager
.ready()
.then(() => manager.makeChangeStream().next())
.then(
() => manager.teardown(),
err => manager.teardown(err)
)
.then(() => {
const tokens = manager.resumeTokenChangedEvents.map(e => e.resumeToken);
const successes = manager.apm.succeeded.map(e => {
try {
// @ts-expect-error: e.reply is unknown
return e.reply.cursor;
} catch (e) {
return {};
}
});
expect(successes).to.have.a.lengthOf(2);
expect(successes[1]).to.have.a.nested.property('nextBatch[0]._id');
expect(tokens).to.have.a.lengthOf(1);
expect(tokens[0]).to.deep.equal(successes[1].nextBatch[0]._id);
});
});
it('must return resumeAfter from the initial aggregate if the option was specified', function () {
const manager = new MockServerManager(this.configuration, {
aggregate: (function* () {
yield { numDocuments: 0, postBatchResumeToken: false };
})(),
getMore: (function* () {
yield { numDocuments: 0, postBatchResumeToken: false };
})()
});
let token;
const resumeAfter = manager.resumeToken();
return manager
.ready()
.then(() => {
return new Promise<void>(resolve => {
const changeStream = manager.makeChangeStream({ resumeAfter });
let counter = 0;
changeStream.cursor.on('response', () => {
if (counter === 1) {
token = changeStream.resumeToken;
resolve();
}
counter += 1;
});
changeStream.next().catch(() => {
// Note: this is expected to fail
});
});
})
.then(
() => manager.teardown(),
err => manager.teardown(err)
)
.then(() => {
expect(token).to.deep.equal(resumeAfter);
});
});
it('must be empty if resumeAfter options was not specified', function () {
const manager = new MockServerManager(this.configuration, {
aggregate: (function* () {
yield { numDocuments: 0, postBatchResumeToken: false };
})(),
getMore: (function* () {
yield { numDocuments: 0, postBatchResumeToken: false };
})()
});
let token;
return manager
.ready()
.then(() => {
return new Promise<void>(resolve => {
const changeStream = manager.makeChangeStream();
let counter = 0;
changeStream.cursor.on('response', () => {
if (counter === 1) {
token = changeStream.resumeToken;
resolve();
}
counter += 1;
});
changeStream.next().catch(() => {
// Note: this is expected to fail
});
});
})
.then(
() => manager.teardown(),
err => manager.teardown(err)
)
.then(() => {
expect(token).to.not.exist;
});
});
});
// 13. For a ChangeStream under these conditions:
// The batch is not empty.
// The batch has been iterated up to but not including the last element.
// Expected result:
// getResumeToken must return the _id of the previous document returned.
describe('for non-empty batch iterated up to but not including the last element', function () {
it('must return the _id of the previous document returned', function () {
const manager = new MockServerManager(this.configuration, {
aggregate: (function* () {
yield { numDocuments: 2, postBatchResumeToken: true };
})(),
getMore: (function* () {
// fake getMore
})()
});
return manager
.ready()
.then(() => {
return manager.makeChangeStream().next();
})
.then(
() => manager.teardown(),
err => manager.teardown(err)
)
.then(() => {
const tokens = manager.resumeTokenChangedEvents.map(e => e.resumeToken);
const successes = manager.apm.succeeded.map(e => {
try {
// @ts-expect-error: e.reply is unknown
return e.reply.cursor;
} catch (e) {
return {};
}
});
expect(successes).to.have.a.lengthOf(1);
expect(successes[0]).to.have.a.nested.property('firstBatch[0]._id');
expect(successes[0]).to.have.a.property('postBatchResumeToken');
expect(tokens).to.have.a.lengthOf(1);
expect(tokens[0])
.to.deep.equal(successes[0].firstBatch[0]._id)
.and.to.not.deep.equal(successes[0].postBatchResumeToken);
});
});
});
// 14. For a ChangeStream under these conditions:
// The batch is not empty.
// The batch hasn’t been iterated at all.
// Only the initial aggregate command has been executed.
// Expected result:
// getResumeToken must return startAfter from the initial aggregate if the option was specified.
// getResumeToken must return resumeAfter from the initial aggregate if the option was specified.
// If neither the startAfter nor resumeAfter options were specified, the getResumeToken result must be empty.
describe('for non-empty non-iterated batch where only the initial aggregate command has been executed', function () {
it('must return startAfter from the initial aggregate if the option was specified', function () {
const manager = new MockServerManager(this.configuration, {
aggregate: (function* () {
yield { numDocuments: 0, postBatchResumeToken: false };
})(),
getMore: (function* () {
yield { numDocuments: 0, postBatchResumeToken: false };
})()
});
let token;
const startAfter = manager.resumeToken();
const resumeAfter = manager.resumeToken();
return manager
.ready()
.then(() => {
return new Promise<void>(resolve => {
const changeStream = manager.makeChangeStream({ startAfter, resumeAfter });
changeStream.cursor.once('response', () => {
token = changeStream.resumeToken;
resolve();
});
changeStream.next().catch(() => {
// Note: this is expected to fail
});
});
})
.then(
() => manager.teardown(),
err => manager.teardown(err)
)
.then(() => {
expect(token).to.deep.equal(startAfter).and.to.not.deep.equal(resumeAfter);
});
});
it('must return resumeAfter from the initial aggregate if the option was specified', function () {
const manager = new MockServerManager(this.configuration, {
aggregate: (function* () {
yield { numDocuments: 0, postBatchResumeToken: false };
})(),
getMore: (function* () {
yield { numDocuments: 0, postBatchResumeToken: false };
})()
});
let token;
const resumeAfter = manager.resumeToken();
return manager
.ready()
.then(() => {
return new Promise<void>(resolve => {
const changeStream = manager.makeChangeStream({ resumeAfter });
changeStream.cursor.once('response', () => {
token = changeStream.resumeToken;
resolve();
});
changeStream.next().catch(() => {
// Note: this is expected to fail
});
});
})
.then(
() => manager.teardown(),
err => manager.teardown(err)
)
.then(() => {
expect(token).to.deep.equal(resumeAfter);
});
});
it('must be empty if neither the startAfter nor resumeAfter options were specified', function () {
const manager = new MockServerManager(this.configuration, {
aggregate: (function* () {
yield { numDocuments: 0, postBatchResumeToken: false };
})(),
getMore: (function* () {
yield { numDocuments: 0, postBatchResumeToken: false };
})()
});
let token;
return manager
.ready()
.then(() => {
return new Promise<void>(resolve => {
const changeStream = manager.makeChangeStream();
changeStream.cursor.once('response', () => {
token = changeStream.resumeToken;
resolve();
});
changeStream.next().catch(() => {
// Note: this is expected to fail
});
});
})
.then(
() => manager.teardown(),
err => manager.teardown(err)
)
.then(() => {
expect(token).to.not.exist;
});
});
});
});
// 15 - 16 removed by spec
describe('Change Stream prose 17-18', function () {
let client;
let coll;
let startAfter;
function recordEvent(events, e) {
if (e.commandName !== 'aggregate') return;
events.push({ $changeStream: e.command.pipeline[0].$changeStream });
}
beforeEach(function (done) {
const configuration = this.configuration;
client = configuration.newClient({ monitorCommands: true });
client.connect(err => {
expect(err).to.not.exist;
coll = client.db('integration_tests').collection('setupAfterTest');
const changeStream = coll.watch();
waitForStarted(changeStream, () => {
coll.insertOne({ x: 1 }, { writeConcern: { w: 'majority', j: true } }, err => {
expect(err).to.not.exist;
coll.drop(err => {
expect(err).to.not.exist;
});
});
});
changeStream.on('change', change => {
if (change.operationType === 'invalidate') {
startAfter = change._id;
changeStream.close(done);
}
});
});
});
afterEach(function (done) {
client.close(done);
});
// 17. $changeStream stage for ChangeStream started with startAfter against a server >=4.1.1
// that has not received any results yet
// - MUST include a startAfter option
// - MUST NOT include a resumeAfter option
// when resuming a change stream.
it('$changeStream without results must include startAfter and not resumeAfter', {
metadata: { requires: { topology: 'replicaset', mongodb: '>=4.1.1' } },
test: function (done) {
const events = [];
client.on('commandStarted', e => recordEvent(events, e));
const changeStream = coll.watch([], { startAfter });
this.defer(() => changeStream.close());
changeStream.once('change', change => {
expect(change).to.containSubset({
operationType: 'insert',
fullDocument: { x: 2 }
});
expect(events).to.be.an('array').with.lengthOf(3);
expect(events[0]).nested.property('$changeStream.startAfter').to.exist;
expect(events[1]).to.equal('error');
expect(events[2]).nested.property('$changeStream.startAfter').to.exist;
done();
});
waitForStarted(changeStream, () => {
triggerResumableError(changeStream, () => {
events.push('error');
coll.insertOne({ x: 2 }, { writeConcern: { w: 'majority', j: true } });
});
});
}
});
// 18. $changeStream stage for ChangeStream started with startAfter against a server >=4.1.1
// that has received at least one result
// - MUST include a resumeAfter option
// - MUST NOT include a startAfter option
// when resuming a change stream.
it('$changeStream with results must include resumeAfter and not startAfter', {
metadata: { requires: { topology: 'replicaset', mongodb: '>=4.1.1' } },
test: function (done) {
let events = [];
client.on('commandStarted', e => recordEvent(events, e));
const changeStream = coll.watch([], { startAfter });
this.defer(() => changeStream.close());
changeStream.on('change', change => {
events.push({ change: { insert: { x: change.fullDocument.x } } });
switch (change.fullDocument.x) {
case 2:
// only events after this point are relevant to this test
events = [];
triggerResumableError(changeStream, () => events.push('error'));
break;
case 3:
expect(events).to.be.an('array').with.lengthOf(3);
expect(events[0]).to.equal('error');
expect(events[1]).nested.property('$changeStream.resumeAfter').to.exist;
expect(events[2]).to.eql({ change: { insert: { x: 3 } } });
done();
break;
}
});
waitForStarted(changeStream, () =>
this.defer(
coll
.insertOne({ x: 2 }, { writeConcern: { w: 'majority', j: true } })
.then(() => coll.insertOne({ x: 3 }, { writeConcern: { w: 'majority', j: true } }))
)
);
}
});
});
describe('19. Validate that large ChangeStream events are split when using $changeStreamSplitLargeEvent', function () {
let client;
let db;
let collection;
let changeStream;
beforeEach(async function () {
const configuration = this.configuration;
client = configuration.newClient();
db = client.db('test');
// Create a new collection _C_ with changeStreamPreAndPostImages enabled.
await db.createCollection('changeStreamSplitTests', {
changeStreamPreAndPostImages: { enabled: true }
});
collection = db.collection('changeStreamSplitTests');
});
afterEach(async function () {
await changeStream.close();
await collection.drop();
await client.close();
});
it('splits the event into multiple fragments', {
metadata: { requires: { topology: '!single', mongodb: '>=7.0.0' } },
test: async function () {
// Insert into _C_ a document at least 10mb in size, e.g. { "value": "q"*10*1024*1024 }
await collection.insertOne({ value: 'q'.repeat(10 * 1024 * 1024) });
// Create a change stream _S_ by calling watch on _C_ with pipeline
// [{ "$changeStreamSplitLargeEvent": {} }] and fullDocumentBeforeChange=required.
changeStream = collection.watch([{ $changeStreamSplitLargeEvent: {} }], {
fullDocumentBeforeChange: 'required'
});
await initIteratorMode(changeStream);
// Call updateOne on _C_ with an empty query and an update setting the field to a new
// large value, e.g. { "$set": { "value": "z"*10*1024*1024 } }.
await collection.updateOne({}, { $set: { value: 'z'.repeat(10 * 1024 * 1024) } });
// Collect two events from _S_.
const eventOne = await changeStream.next();
const eventTwo = await changeStream.next();
// Assert that the events collected have splitEvent fields { "fragment": 1, "of": 2 }
// and { "fragment": 2, "of": 2 }, in that order.
expect(eventOne.splitEvent).to.deep.equal({ fragment: 1, of: 2 });
expect(eventTwo.splitEvent).to.deep.equal({ fragment: 2, of: 2 });
}