summaryrefslogtreecommitdiff
path: root/jstests/core/txns/shell_prompt_in_transaction.js
blob: 019ea5595de7d8d3adc793585d1336eb91f92111 (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
// Test shell prompt doesn't run in the session of the global 'db'.
// @tags: [uses_transactions]

(function() {
    "use strict";

    const collName = "shell_prompt_in_transaction";

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

    // Override the global "db".
    const session = db.getMongo().startSession();
    db = session.getDatabase(db.getName());
    const coll = db.getCollection(collName);

    function simulatePrompt() {
        __promptWrapper__(defaultPrompt);
    }

    // Start a transaction, so the session will attach txn info to the commands running on it.
    session.startTransaction();
    jsTestLog("Run shell prompt to simulate a user hitting enter.");
    simulatePrompt();
    const doc = {_id: "shell-write"};
    assert.commandWorked(coll.insert(doc));
    assert.docEq(doc, coll.findOne());
    simulatePrompt();
    assert.commandWorked(session.abortTransaction_forTesting());
    assert.docEq(null, coll.findOne());

    // Start a transaction, so the session has a running transaction now.
    simulatePrompt();
    session.startTransaction();
    jsTestLog("Run shell prompt to simulate a user hitting enter.");
    simulatePrompt();
    assert.commandWorked(coll.insert(doc));
    simulatePrompt();
    assert.commandWorked(session.commitTransaction_forTesting());
    assert.docEq(doc, coll.findOne());

    coll.drop({writeConcern: {w: "majority"}});
})();