summaryrefslogtreecommitdiff
path: root/jstests/core/txns/create_collection_parallel.js
blob: 401606ef2538804b579901bbd540ef969264eb5a (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
/* Tests parallel transactions with createCollections.
 *
 * @tags: [uses_transactions,
 *         # Creating collections inside multi-document transactions is supported only in v4.4
 *         # onwards.
 *         requires_fcv_44]
 */
(function() {
"use strict";

load("jstests/libs/create_collection_txn_helpers.js");

const dbName = "test";
const collName = "create_new_collection";
const distinctCollName = collName + "_second";
const session = db.getMongo().getDB(dbName).getMongo().startSession({causalConsistency: false});
const secondSession =
    db.getMongo().getDB(dbName).getMongo().startSession({causalConsistency: false});

let sessionDB = session.getDatabase("test");
let secondSessionDB = secondSession.getDatabase("test");
let sessionColl = sessionDB[collName];
sessionColl.drop({writeConcern: {w: "majority"}});
let distinctSessionColl = sessionDB[distinctCollName];
distinctSessionColl.drop({writeConcern: {w: "majority"}});

jsTest.log("Testing duplicate createCollections, second createCollection fails");

session.startTransaction({writeConcern: {w: "majority"}});        // txn 1
secondSession.startTransaction({writeConcern: {w: "majority"}});  // txn 2

createCollAndCRUDInTxn(sessionDB, collName);
jsTest.log("Committing transaction 1");
session.commitTransaction();
assert.eq(sessionColl.find({}).itcount(), 1);

assert.commandFailedWithCode(secondSessionDB.runCommand({create: collName}),
                             ErrorCodes.NamespaceExists);

assert.commandFailedWithCode(secondSession.abortTransaction_forTesting(),
                             ErrorCodes.NoSuchTransaction);

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

jsTest.log(
    "Testing duplicate createCollections in parallel, both attempt to commit, second to commit fails");

secondSession.startTransaction({writeConcern: {w: "majority"}});  // txn 2
createCollAndCRUDInTxn(secondSession.getDatabase("test"), collName);

session.startTransaction({writeConcern: {w: "majority"}});  // txn 1
createCollAndCRUDInTxn(sessionDB, collName);

jsTest.log("Committing transaction 2");
secondSession.commitTransaction();
jsTest.log("Committing transaction 1 (SHOULD FAIL)");
assert.commandFailedWithCode(session.commitTransaction_forTesting(), ErrorCodes.WriteConflict);
assert.eq(sessionColl.find({}).itcount(), 1);

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

jsTest.log("Testing distinct createCollections in parallel, both successfully commit.");
session.startTransaction({writeConcern: {w: "majority"}});  // txn 1
createCollAndCRUDInTxn(sessionDB, collName);

secondSession.startTransaction({writeConcern: {w: "majority"}});  // txn 2
createCollAndCRUDInTxn(secondSessionDB, distinctCollName);

session.commitTransaction();
secondSession.commitTransaction();

secondSession.endSession();
session.endSession();
}());