summaryrefslogtreecommitdiff
path: root/jstests/replsets/aggregation_write_concern.js
blob: 807da4bf244f5ca99d96ed993695d530e866f0bb (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
/**
 * Confirms that the aggregate command accepts writeConcern and that a read-only aggregation will
 * not wait for the writeConcern specified to be satisfied.
 */
(function() {
"use strict";

load("jstests/libs/write_concern_util.js");  // For stopReplicationOnSecondaries,
                                             // restartReplicationOnSecondaries
const name = "aggregation_write_concern";

const replTest = new ReplSetTest({nodes: [{}, {rsConfig: {priority: 0}}]});

replTest.startSet();
replTest.initiate();

const testDB = replTest.getPrimary().getDB(name);
const collectionName = "test";

// Stop replication and perform a w: 1 write. This will block subsequent 'writeConcern:
// majority' reads if the read command waits on writeConcern.

stopReplicationOnSecondaries(replTest);
assert.commandWorked(
    testDB.runCommand({insert: collectionName, documents: [{_id: 1}], writeConcern: {w: 1}}));

// A read-only aggregation accepts the writeConcern option but does not wait for it.
let res = assert.commandWorked(testDB.runCommand({
    aggregate: collectionName,
    pipeline: [{$match: {_id: 1}}],
    cursor: {},
    writeConcern: {w: "majority"}
}));
assert(res.cursor.firstBatch.length);
assert.eq(res.cursor.firstBatch[0], {_id: 1});

// An aggregation pipeline that writes will block on writeConcern.
assert.commandFailedWithCode(testDB.runCommand({
    aggregate: collectionName,
    pipeline: [{$match: {_id: 1}}, {$out: collectionName + "_out"}],
    cursor: {},
    writeConcern: {w: "majority", wtimeout: 1000}
}),
                             ErrorCodes.WriteConcernFailed);

restartReplicationOnSecondaries(replTest);
replTest.awaitLastOpCommitted();
replTest.stopSet();
})();