diff options
author | Judah Schvimer <judah@mongodb.com> | 2018-07-20 13:11:12 -0400 |
---|---|---|
committer | Judah Schvimer <judah@mongodb.com> | 2018-07-20 13:17:19 -0400 |
commit | 5b6fbcf0dc4065d725b23c6fd3911a24e078e34d (patch) | |
tree | f5da305a6d3dfb072146fa75f592c67099a03bd9 /jstests/core/txns/abort_prepared_transaction.js | |
parent | 4cdaee88d7122f3ccba152ae37d3b5b69b3b398f (diff) | |
download | mongo-5b6fbcf0dc4065d725b23c6fd3911a24e078e34d.tar.gz |
SERVER-35597 SERVER-35598 Ensure prepared transactions can be committed
Diffstat (limited to 'jstests/core/txns/abort_prepared_transaction.js')
-rw-r--r-- | jstests/core/txns/abort_prepared_transaction.js | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/jstests/core/txns/abort_prepared_transaction.js b/jstests/core/txns/abort_prepared_transaction.js new file mode 100644 index 00000000000..f5b5b98d2fe --- /dev/null +++ b/jstests/core/txns/abort_prepared_transaction.js @@ -0,0 +1,82 @@ +/** + * Tests prepared transaction abort support. + * + * @tags: [uses_transactions] + */ +(function() { + "use strict"; + load("jstests/core/txns/libs/prepare_helpers.js"); + + const dbName = "test"; + const collName = "abort_prepared_transaction"; + const testDB = db.getSiblingDB(dbName); + const testColl = testDB.getCollection(collName); + + testColl.drop(); + assert.commandWorked(testDB.runCommand({create: collName, writeConcern: {w: "majority"}})); + + const session = db.getMongo().startSession({causalConsistency: false}); + const sessionDB = session.getDatabase(dbName); + const sessionColl = sessionDB.getCollection(collName); + + const doc1 = {_id: 1, x: 1}; + + // ---- Test 1. Insert a single document and run prepare. ---- + + session.startTransaction(); + assert.commandWorked(sessionColl.insert(doc1)); + + // Insert should not be visible outside the session. + assert.eq(null, testColl.findOne(doc1)); + + // Insert should be visible in this session. + assert.eq(doc1, sessionColl.findOne(doc1)); + + PrepareHelpers.prepareTransaction(session); + session.abortTransaction(); + + // After abort the insert is rolled back. + assert.eq(null, testColl.findOne(doc1)); + + // ---- Test 2. Update a document and run prepare. ---- + + // Insert a document to update. + assert.commandWorked(sessionColl.insert(doc1, {writeConcern: {w: "majority"}})); + + session.startTransaction(); + assert.commandWorked(sessionColl.update(doc1, {$inc: {x: 1}})); + + const doc2 = {_id: 1, x: 2}; + + // Update should not be visible outside the session. + assert.eq(null, testColl.findOne(doc2)); + + // Update should be visible in this session. + assert.eq(doc2, sessionColl.findOne(doc2)); + + PrepareHelpers.prepareTransaction(session); + session.abortTransaction(); + + // After abort the update is rolled back. + assert.eq(doc1, testColl.findOne({_id: 1})); + + // ---- Test 3. Delete a document and run prepare. ---- + + // Update the document. + assert.commandWorked(sessionColl.update(doc1, {$inc: {x: 1}}, {writeConcern: {w: "majority"}})); + + session.startTransaction(); + assert.commandWorked(sessionColl.remove(doc2, {justOne: true})); + + // Delete should not be visible outside the session, so the document should be. + assert.eq(doc2, testColl.findOne(doc2)); + + // Document should not be visible in this session, since the delete should be visible. + assert.eq(null, sessionColl.findOne(doc2)); + + PrepareHelpers.prepareTransaction(session); + session.abortTransaction(); + + // After abort the delete is rolled back. + assert.eq(doc2, testColl.findOne(doc2)); +}()); |