diff options
author | Lingzhi Deng <lingzhi.deng@mongodb.com> | 2019-04-25 16:56:58 -0400 |
---|---|---|
committer | Lingzhi Deng <lingzhi.deng@mongodb.com> | 2019-04-29 16:33:55 -0400 |
commit | a7b6ea4c7c51069eb85b789d078210ec2852e5e0 (patch) | |
tree | 11eb2226872ed441f7789f54364e18ab17917b7f /jstests | |
parent | e3796fef68ec17ef475e669cd04193aac506bf58 (diff) | |
download | mongo-a7b6ea4c7c51069eb85b789d078210ec2852e5e0.tar.gz |
SERVER-40839: Add tests for empty unprepared transactions
Diffstat (limited to 'jstests')
-rw-r--r-- | jstests/core/txns/empty_commit_abort.js | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/jstests/core/txns/empty_commit_abort.js b/jstests/core/txns/empty_commit_abort.js new file mode 100644 index 00000000000..0b65ffab94f --- /dev/null +++ b/jstests/core/txns/empty_commit_abort.js @@ -0,0 +1,64 @@ +/** + * Tests transactions that commit/abort after no writes. + * + * @tags: [uses_transactions] + */ +(function() { + "use strict"; + + const dbName = "test"; + const collName = "empty_commit_abort"; + 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(); + const sessionDB = session.getDatabase(dbName); + const sessionColl = sessionDB.getCollection(collName); + + // ---- Test 1. No operations before commit ---- + session.startTransaction(); + assert.commandFailedWithCode(sessionDB.adminCommand({commitTransaction: 1}), + ErrorCodes.OperationNotSupportedInTransaction); + assert.commandFailedWithCode(session.abortTransaction_forTesting(), + ErrorCodes.NoSuchTransaction); + + // ---- Test 2. No operations before abort ---- + session.startTransaction(); + assert.commandFailedWithCode(sessionDB.adminCommand({abortTransaction: 1}), + ErrorCodes.OperationNotSupportedInTransaction); + assert.commandFailedWithCode(session.abortTransaction_forTesting(), + ErrorCodes.NoSuchTransaction); + + // ---- Test 3. Only reads before commit ---- + session.startTransaction(); + assert.eq(doc, sessionColl.findOne({a: 1})); + assert.commandWorked(session.commitTransaction_forTesting()); + + // ---- Test 4. Only reads before abort ---- + session.startTransaction(); + assert.eq(doc, sessionColl.findOne({a: 1})); + assert.commandWorked(session.abortTransaction_forTesting()); + + // ---- Test 5. Noop writes before commit ---- + session.startTransaction(); + let 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)); + assert.commandWorked(session.commitTransaction_forTesting()); + + // ---- Test 6. Noop writes before abort ---- + 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)); + assert.commandWorked(session.abortTransaction_forTesting()); + +}()); |