summaryrefslogtreecommitdiff
path: root/jstests/core/txns/empty_prepare.js
blob: cb1b616c9fcc7f5c972827d1eed2f6f24af58852 (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
/**
 * Tests transactions that are prepared after no writes.
 *
 * @tags: [uses_transactions, uses_prepare_transaction]
 */
(function() {
    "use strict";

    const dbName = "test";
    const collName = "empty_prepare";
    const testDB = db.getSiblingDB(dbName);
    const testColl = testDB.getCollection(collName);

    testColl.drop({writeConcern: {w: "majority"}});
    assert.commandWorked(testDB.runCommand({create: collName, writeConcern: {w: "majority"}}));

    const doc = {_id: 1, a: 1, b: 1};
    assert.commandWorked(testColl.insert(doc));

    const session = db.getMongo().startSession({causalConsistency: false});
    const sessionDB = session.getDatabase(dbName);
    const sessionColl = sessionDB.getCollection(collName);

    // ---- Test 1. No operations before prepare ----

    session.startTransaction();
    assert.commandFailedWithCode(sessionDB.adminCommand({prepareTransaction: 1}),
                                 ErrorCodes.OperationNotSupportedInTransaction);
    assert.commandFailedWithCode(session.abortTransaction_forTesting(),
                                 ErrorCodes.NoSuchTransaction);

    // ---- Test 2. Only reads before prepare ----

    session.startTransaction();
    assert.eq(doc, sessionColl.findOne({a: 1}));
    let res = assert.commandWorked(sessionDB.adminCommand({prepareTransaction: 1}));
    // Makes sure prepareTransaction returns prepareTimestamp in its response.
    assert(res.hasOwnProperty("prepareTimestamp"), tojson(res));
    assert.commandWorked(session.abortTransaction_forTesting());

    // ---- Test 3. Noop writes before prepare ----

    session.startTransaction();
    res = assert.commandWorked(sessionColl.update({a: 1}, {$set: {b: 1}}));
    assert.eq(res.nMatched, 1, tojson(res));
    assert.eq(res.nModified, 0, tojson(res));
    assert.eq(res.nUpserted, 0, tojson(res));
    res = assert.commandWorked(sessionDB.adminCommand({prepareTransaction: 1}));
    // Makes sure prepareTransaction returns prepareTimestamp in its response.
    assert(res.hasOwnProperty("prepareTimestamp"), tojson(res));
    assert.commandWorked(session.abortTransaction_forTesting());

}());