summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/shell_uses_correct_read_concern.js
blob: f3005c0f346199064183c7e1a65e0c8960196687 (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
50
51
52
53
54
55
56
57
/**
 * Tests that reads through the shell uses the correct read concern.
 * @tags: [requires_replication, uses_transactions, requires_majority_read_concern]
 */
(function() {
"use strict";
const rst = new ReplSetTest({nodes: 2, nodeOptions: {enableMajorityReadConcern: ""}});
rst.startSet();
rst.initiate();

const collName = "shell_uses_transaction_read_concern";
const primary = rst.getPrimary();
const db = primary.getDB("test");
var coll = db.getCollection(collName);
const testDoc = {
    "test": "doc",
    "_id": 0
};
assert.commandWorked(coll.insertOne(testDoc));
rst.awaitReplication();

const getMajorityRCCount = () =>
    db.runCommand({serverStatus: 1}).readConcernCounters.nonTransactionOps.majority;
const getSnapshotRCCount = () =>
    db.runCommand({serverStatus: 1}).readConcernCounters.transactionOps.snapshot.withoutClusterTime;

// Command-level
assert.eq(coll.runCommand({"find": coll.getName(), readConcern: {level: "majority"}})
              .cursor.firstBatch.length,
          1);
assert.eq(getMajorityRCCount(), 1);

const session = primary.startSession({readConcern: {level: "majority"}});
coll = session.getDatabase("test").getCollection(collName);

// Session-level
assert.eq(coll.find({"_id": 0}).itcount(), 1);
assert.eq(coll.runCommand({"find": coll.getName()}).cursor.firstBatch.length, 1);
assert.eq(getMajorityRCCount(), 3);

// Check that the session read concern doesn't break explain.
assert.commandWorked(coll.runCommand(
    {explain: {count: collName, query: {"_id": 0}, readConcern: {level: "available"}}}));
assert.commandWorked(coll.runCommand({explain: {count: collName, query: {"_id": 0}}}));

// Transaction-level
session.startTransaction({readConcern: {level: "snapshot"}});
assert.eq(coll.runCommand({"find": coll.getName()}).cursor.firstBatch.length, 1);
assert.eq(coll.runCommand({"find": coll.getName()}).cursor.firstBatch.length, 1);
assert.eq(coll.find({"_id": 0}).itcount(), 1);
assert.docEq(coll.findOne({"_id": 0}), testDoc);

assert.commandWorked(session.commitTransaction_forTesting());
assert.eq(getSnapshotRCCount(), 4);

rst.stopSet();
})();