summaryrefslogtreecommitdiff
path: root/jstests/core/txns/transaction_error_handling.js
blob: 74852bd58f87c0cc9179d2d9209e101265032866 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Test basic transaction error handling.
// @tags: [uses_transactions]
(function() {
"use strict";

const dbName = "test";
const collName = "transaction_error_handling";
const testDB = db.getSiblingDB(dbName);

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

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

const sessionOptions = {
    causalConsistency: false
};
const session = testDB.getMongo().startSession(sessionOptions);
const sessionDb = session.getDatabase(dbName);
const sessionColl = sessionDb.getCollection(collName);

jsTestLog("Test that we cannot abort or commit a nonexistant transaction.");
// Cannot abort or commit a nonexistant transaction.
try {
    assert.commandWorked(session.commitTransaction_forTesting());
} catch (e) {
    assert.eq(e.message, "There is no active transaction to commit on this session.");
}

try {
    assert.commandFailedWithCode(session.abortTransaction_forTesting(),
                                 ErrorCodes.NoSuchTransaction);
} catch (e) {
    assert.eq(e.message, "There is no active transaction to abort on this session.");
}

// Try to start a transaction when the state is 'active'.
jsTestLog("Test that we cannot start a transaction with one already started or in progress.");
session.startTransaction();
try {
    session.startTransaction();
} catch (e) {
    assert.eq(e.message, "Transaction already in progress on this session.");
}

// Try starting a transaction after inserting something.
assert.commandWorked(sessionColl.insert({_id: "insert-1"}));
// Try to start a transaction when the state is 'active'.
try {
    session.startTransaction();
} catch (e) {
    assert.eq(e.message, "Transaction already in progress on this session.");
}

// At this point, the transaction is still 'active'. We will commit this transaction and test
// that calling commitTransaction again should work while calling abortTransaction should not.
assert.commandWorked(session.commitTransaction_forTesting());

jsTestLog("Test that we can commit a transaction more than once.");
// The transaction state is 'committed'. We can call commitTransaction again in this state.
assert.commandWorked(session.commitTransaction_forTesting());

jsTestLog("Test that we cannot abort a transaction that has already been committed");
// We cannot call abortTransaction on a transaction that has already been committed.
try {
    assert.commandFailedWithCode(session.abortTransaction_forTesting(),
                                 ErrorCodes.NoSuchTransaction);
} catch (e) {
    assert.eq(e.message, "Cannot call abortTransaction after calling commitTransaction.");
}

// Start a new transaction that will be aborted. Test that we cannot call commit or
// abortTransaction on a transaction that is in the 'aborted' state.
session.startTransaction();
assert.commandWorked(sessionColl.insert({_id: "insert-2"}));
assert.commandWorked(session.abortTransaction_forTesting());

jsTestLog("Test that we cannot commit a transaction that has already been aborted.");
// We cannot call commitTransaction on a transaction that has already been aborted.
try {
    assert.commandWorked(session.commitTransaction_forTesting());
} catch (e) {
    assert.eq(e.message, "Cannot call commitTransaction after calling abortTransaction.");
}

jsTestLog("Test that we cannot abort a transaction that has already been aborted.");
// We also cannot call abortTransaction on a transaction that has already been aborted.
try {
    assert.commandFailedWithCode(session.abortTransaction_forTesting(),
                                 ErrorCodes.NoSuchTransaction);
} catch (e) {
    assert.eq(e.message, "Cannot call abortTransaction twice.");
}

jsTestLog(
    "Test that a normal operation after committing a transaction changes the state to inactive.");
session.startTransaction();
assert.commandWorked(sessionColl.insert({_id: "insert-3"}));
// The transaction state should be changed to 'committed'.
assert.commandWorked(session.commitTransaction_forTesting());
// The transaction state should be changed to 'inactive'.
assert.commandWorked(sessionColl.insert({_id: "normal-insert"}));
try {
    assert.commandWorked(session.commitTransaction_forTesting());
} catch (e) {
    assert.eq(e.message, "There is no active transaction to commit on this session.");
}

jsTestLog(
    "Test that a normal operation after aborting a transaction changes the state to inactive.");
session.startTransaction();
assert.commandWorked(sessionColl.insert({_id: "insert-4"}));
// The transaction state should be changed to 'aborted'.
assert.commandWorked(session.abortTransaction_forTesting());
// The transaction state should be changed to 'inactive'.
assert.commandWorked(sessionColl.insert({_id: "normal-insert-2"}));
try {
    assert.commandFailedWithCode(session.abortTransaction_forTesting(),
                                 ErrorCodes.NoSuchTransaction);
} catch (e) {
    assert.eq(e.message, "There is no active transaction to abort on this session.");
}

session.endSession();
}());