summaryrefslogtreecommitdiff
path: root/jstests/sharding/change_stream_against_shard_mongod.js
blob: e66571caeb5ff4f905e29246b387778a385c9e98 (plain)
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
/**
 * Tests that an updateLookup change stream on a sharded collection can be successfully opened
 * and read from on a shard mongoD. Exercises the fix for SERVER-44977.
 * @tags: [
 *   uses_change_streams,
 * ]
 */
(function() {
"use strict";

// Start a new sharded cluster and obtain references to the test DB and collection.
const st = new ShardingTest({
    shards: 1,
    mongos: 1,
    rs: {nodes: 1, setParameter: {writePeriodicNoops: true, periodicNoopIntervalSecs: 1}}
});

const mongosDB = st.s.getDB(jsTestName());
const mongosColl = mongosDB.test;

const shard0 = st.rs0;
const shard0Coll = shard0.getPrimary().getCollection(mongosColl.getFullName());

// Enable sharding on the the test database and ensure that the primary is shard0.
assert.commandWorked(mongosDB.adminCommand({enableSharding: mongosDB.getName()}));
st.ensurePrimaryShard(mongosDB.getName(), shard0.getURL());

// Shard the source collection on {a: 1}. No need to split since it's single-shard.
st.shardColl(mongosColl, {a: 1}, false);

// Open an updateLookup change stream on the collection, against the shard mongoD.
const csCursor = shard0Coll.watch([], {fullDocument: "updateLookup"});

// Write one document onto shard0, then do an op-style update which will require a lookup.
assert.commandWorked(mongosColl.insert({_id: 0, a: -100}));
assert.commandWorked(mongosColl.update({a: -100}, {$set: {updated: true}}));

// Confirm that the stream opened against the shard mongoD sees both events.
const expectedEvents =
    [{op: "insert", doc: {_id: 0, a: -100}}, {op: "update", doc: {_id: 0, a: -100, updated: true}}];
for (let event of expectedEvents) {
    assert.soon(() => csCursor.hasNext());
    const nextDoc = csCursor.next();
    assert.eq(nextDoc.operationType, event.op);
    assert.docEq(event.doc, nextDoc.fullDocument);
}

st.stop();
})();