summaryrefslogtreecommitdiff
path: root/jstests/core/txns/read_concerns.js
blob: 7d70ea4c8dcb97cb654eba5f4538ff26195306a8 (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
58
59
60
61
62
// Verifies which read concern levels transactions support, with and without afterClusterTime.
//
// @tags: [uses_transactions]
(function() {
    "use strict";

    const dbName = "test";
    const collName = "supported_read_concern_levels";

    function runTest(level, sessionOptions, supported) {
        jsTestLog("Testing transactions with read concern level: " + level +
                  " and sessionOptions: " + tojson(sessionOptions));

        db.getSiblingDB(dbName).runCommand({drop: collName, writeConcern: {w: "majority"}});

        const session = db.getMongo().startSession(sessionOptions);
        const sessionDB = session.getDatabase(dbName);
        const sessionColl = sessionDB[collName];

        // Set up the collection.
        assert.writeOK(sessionColl.insert({_id: 0}, {writeConcern: {w: "majority"}}));

        if (level) {
            session.startTransaction({readConcern: {level: level}});
        } else {
            session.startTransaction();
        }

        const res = sessionDB.runCommand({find: collName});
        if (supported) {
            assert.commandWorked(res,
                                 "expected success, read concern level: " + level +
                                     ", sessionOptions: " + tojson(sessionOptions));
            assert.commandWorked(session.commitTransaction_forTesting());
        } else {
            assert.commandFailedWithCode(res,
                                         ErrorCodes.InvalidOptions,
                                         "expected failure, read concern level: " + level +
                                             ", sessionOptions: " + tojson(sessionOptions));
            assert.commandFailedWithCode(session.abortTransaction_forTesting(),
                                         ErrorCodes.NoSuchTransaction);
        }

        session.endSession();
    }

    // Starting a txn with no read concern level is allowed.
    runTest(undefined, {causalConsistency: false}, true /*supported*/);
    runTest(undefined, {causalConsistency: true}, true /*supported*/);

    const kSupportedLevels = ["local", "majority", "snapshot"];
    for (let level of kSupportedLevels) {
        runTest(level, {causalConsistency: false}, true /*supported*/);
        runTest(level, {causalConsistency: true}, true /*supported*/);
    }

    const kUnsupportedLevels = ["available", "linearizable"];
    for (let level of kUnsupportedLevels) {
        runTest(level, {causalConsistency: false}, false /*supported*/);
        runTest(level, {causalConsistency: true}, false /*supported*/);
    }
}());