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
|
// Basic $changeStream tests.
(function() {
"use strict";
load("jstests/libs/collection_drop_recreate.js"); // For assert[Drop|Create]Collection.
load("jstests/libs/change_stream_util.js");
load('jstests/libs/uuid_util.js');
let cst = new ChangeStreamTest(db);
jsTestLog("Testing single insert");
assertDropCollection(db, "t1");
let cursor = cst.startWatchingChanges({pipeline: [{$changeStream: {}}], collection: db.t1});
// Test that if there are no changes, we return an empty batch.
assert.eq(0, cursor.firstBatch.length, "Cursor had changes: " + tojson(cursor));
assert.writeOK(db.t1.insert({_id: 0, a: 1}));
const t1Uuid = getUUIDFromListCollections(db, db.t1.getName());
let expected = {
documentKey: {_id: 0},
fullDocument: {_id: 0, a: 1},
ns: {db: "test", coll: "t1"},
operationType: "insert",
};
cst.assertNextChangesEqual({cursor: cursor, expectedChanges: [expected]});
// Test that if there are no changes during a subsequent 'getMore', we return an empty batch.
cursor = cst.getNextBatch(cursor);
assert.eq(0, cursor.nextBatch.length, "Cursor had changes: " + tojson(cursor));
jsTestLog("Testing second insert");
cursor = cst.startWatchingChanges({pipeline: [{$changeStream: {}}], collection: db.t1});
assert.writeOK(db.t1.insert({_id: 1, a: 2}));
expected = {
documentKey: {_id: 1},
fullDocument: {_id: 1, a: 2},
ns: {db: "test", coll: "t1"},
operationType: "insert",
};
cst.assertNextChangesEqual({cursor: cursor, expectedChanges: [expected]});
jsTestLog("Testing update");
cursor = cst.startWatchingChanges({pipeline: [{$changeStream: {}}], collection: db.t1});
assert.writeOK(db.t1.update({_id: 0}, {_id: 0, a: 3}));
expected = {
documentKey: {_id: 0},
fullDocument: {_id: 0, a: 3},
ns: {db: "test", coll: "t1"},
operationType: "replace",
};
cst.assertNextChangesEqual({cursor: cursor, expectedChanges: [expected]});
jsTestLog("Testing update of another field");
cursor = cst.startWatchingChanges({pipeline: [{$changeStream: {}}], collection: db.t1});
assert.writeOK(db.t1.update({_id: 0}, {_id: 0, b: 3}));
expected = {
documentKey: {_id: 0},
fullDocument: {_id: 0, b: 3},
ns: {db: "test", coll: "t1"},
operationType: "replace",
};
cst.assertNextChangesEqual({cursor: cursor, expectedChanges: [expected]});
jsTestLog("Testing upsert");
cursor = cst.startWatchingChanges({pipeline: [{$changeStream: {}}], collection: db.t1});
assert.writeOK(db.t1.update({_id: 2}, {_id: 2, a: 4}, {upsert: true}));
expected = {
documentKey: {_id: 2},
fullDocument: {_id: 2, a: 4},
ns: {db: "test", coll: "t1"},
operationType: "insert",
};
cst.assertNextChangesEqual({cursor: cursor, expectedChanges: [expected]});
jsTestLog("Testing partial update with $inc");
assert.writeOK(db.t1.insert({_id: 3, a: 5, b: 1}));
cursor = cst.startWatchingChanges({pipeline: [{$changeStream: {}}], collection: db.t1});
assert.writeOK(db.t1.update({_id: 3}, {$inc: {b: 2}}));
expected = {
documentKey: {_id: 3},
ns: {db: "test", coll: "t1"},
operationType: "update",
updateDescription: {removedFields: [], updatedFields: {b: 3}},
};
cst.assertNextChangesEqual({cursor: cursor, expectedChanges: [expected]});
jsTestLog("Testing delete");
cursor = cst.startWatchingChanges({pipeline: [{$changeStream: {}}], collection: db.t1});
assert.writeOK(db.t1.remove({_id: 1}));
expected = {
documentKey: {_id: 1},
ns: {db: "test", coll: "t1"},
operationType: "delete",
};
cst.assertNextChangesEqual({cursor: cursor, expectedChanges: [expected]});
jsTestLog("Testing intervening write on another collection");
assertDropCollection(db, "t2");
cursor = cst.startWatchingChanges({pipeline: [{$changeStream: {}}], collection: db.t1});
let t2cursor = cst.startWatchingChanges({pipeline: [{$changeStream: {}}], collection: db.t2});
assert.writeOK(db.t2.insert({_id: 100, c: 1}));
const t2Uuid = getUUIDFromListCollections(db, db.t2.getName());
cst.assertNextChangesEqual({cursor: cursor, expectedChanges: []});
expected = {
documentKey: {_id: 100},
fullDocument: {_id: 100, c: 1},
ns: {db: "test", coll: "t2"},
operationType: "insert",
};
cst.assertNextChangesEqual({cursor: t2cursor, expectedChanges: [expected]});
jsTestLog("Testing drop of unrelated collection");
assert.writeOK(db.dropping.insert({}));
assertDropCollection(db, db.dropping.getName());
// Should still see the previous change from t2, shouldn't see anything about 'dropping'.
// Test collection renaming. Sharded collections cannot be renamed.
if (!db.t2.stats().sharded) {
jsTestLog("Testing rename");
assertDropCollection(db, "t3");
t2cursor = cst.startWatchingChanges({pipeline: [{$changeStream: {}}], collection: db.t2});
assert.writeOK(db.t2.renameCollection("t3"));
expected = {operationType: "invalidate"};
cst.assertNextChangesEqual(
{cursor: t2cursor, expectedChanges: [expected], expectInvalidate: true});
}
jsTestLog("Testing insert that looks like rename");
assertDropCollection(db, "dne1");
assertDropCollection(db, "dne2");
const dne1cursor =
cst.startWatchingChanges({pipeline: [{$changeStream: {}}], collection: db.dne1});
const dne2cursor =
cst.startWatchingChanges({pipeline: [{$changeStream: {}}], collection: db.dne2});
assert.writeOK(db.t2.insert({_id: 101, renameCollection: "test.dne1", to: "test.dne2"}));
cst.assertNextChangesEqual({cursor: dne1cursor, expectedChanges: []});
cst.assertNextChangesEqual({cursor: dne2cursor, expectedChanges: []});
const isMongos = db.runCommand({isdbgrid: 1}).isdbgrid;
if (!isMongos) {
jsTestLog("Ensuring attempt to read with legacy operations fails.");
db.getMongo().forceReadMode('legacy');
const legacyCursor = db.tailable2.aggregate([{$changeStream: {}}, cst.oplogProjection],
{cursor: {batchSize: 0}});
assert.throws(function() {
legacyCursor.next();
}, [], "Legacy getMore expected to fail on changeStream cursor.");
db.getMongo().forceReadMode('commands');
}
jsTestLog("Testing resumability");
assertDropAndRecreateCollection(db, "resume1");
// Note we do not project away 'id.ts' as it is part of the resume token.
let resumeCursor = cst.startWatchingChanges(
{pipeline: [{$changeStream: {}}], collection: db.resume1, includeToken: true});
// Insert a document and save the resulting change stream.
assert.writeOK(db.resume1.insert({_id: 1}));
const firstInsertChangeDoc = cst.getOneChange(resumeCursor);
assert.docEq(firstInsertChangeDoc.fullDocument, {_id: 1});
jsTestLog("Testing resume after one document.");
resumeCursor = cst.startWatchingChanges({
pipeline: [{$changeStream: {resumeAfter: firstInsertChangeDoc._id}}],
collection: db.resume1,
includeToken: true,
aggregateOptions: {cursor: {batchSize: 0}},
});
jsTestLog("Inserting additional documents.");
assert.writeOK(db.resume1.insert({_id: 2}));
const secondInsertChangeDoc = cst.getOneChange(resumeCursor);
assert.docEq(secondInsertChangeDoc.fullDocument, {_id: 2});
assert.writeOK(db.resume1.insert({_id: 3}));
const thirdInsertChangeDoc = cst.getOneChange(resumeCursor);
assert.docEq(thirdInsertChangeDoc.fullDocument, {_id: 3});
jsTestLog("Testing resume after first document of three.");
resumeCursor = cst.startWatchingChanges({
pipeline: [{$changeStream: {resumeAfter: firstInsertChangeDoc._id}}],
collection: db.resume1,
includeToken: true,
aggregateOptions: {cursor: {batchSize: 0}},
});
assert.docEq(cst.getOneChange(resumeCursor), secondInsertChangeDoc);
assert.docEq(cst.getOneChange(resumeCursor), thirdInsertChangeDoc);
jsTestLog("Testing resume after second document of three.");
resumeCursor = cst.startWatchingChanges({
pipeline: [{$changeStream: {resumeAfter: secondInsertChangeDoc._id}}],
collection: db.resume1,
includeToken: true,
aggregateOptions: {cursor: {batchSize: 0}},
});
assert.docEq(cst.getOneChange(resumeCursor), thirdInsertChangeDoc);
cst.cleanUp();
}());
|