diff options
author | William Schultz <william.schultz@mongodb.com> | 2018-04-23 16:41:49 -0400 |
---|---|---|
committer | William Schultz <william.schultz@mongodb.com> | 2018-04-23 16:42:29 -0400 |
commit | 62119ef6bd1282289a0f27af792df2aca828c1b9 (patch) | |
tree | 6a7d4135967e4967b8e8ce5f9cb35ae8e38fcb7c | |
parent | 75ed77988ea3a9151caf602e39109b3c9533adb4 (diff) | |
download | mongo-62119ef6bd1282289a0f27af792df2aca828c1b9.tar.gz |
SERVER-34259 Convert no_implicit_collection_creation_in_txn.js to use transactions
shell API
-rw-r--r-- | jstests/core/txns/no_implicit_collection_creation_in_txn.js | 155 |
1 files changed, 50 insertions, 105 deletions
diff --git a/jstests/core/txns/no_implicit_collection_creation_in_txn.js b/jstests/core/txns/no_implicit_collection_creation_in_txn.js index 088fe431643..fe93a68c2e9 100644 --- a/jstests/core/txns/no_implicit_collection_creation_in_txn.js +++ b/jstests/core/txns/no_implicit_collection_creation_in_txn.js @@ -11,46 +11,31 @@ testDB.runCommand({drop: collName, writeConcern: {w: "majority"}}); - let txnNumber = 0; - const sessionOptions = {causalConsistency: false}; const session = db.getMongo().startSession(sessionOptions); const sessionDb = session.getDatabase(dbName); + const sessionColl = sessionDb[collName]; jsTest.log("Cannot implicitly create a collection in a transaction using insert."); // Insert succeeds when the collection exists. assert.commandWorked( testDB.createCollection(testColl.getName(), {writeConcern: {w: "majority"}})); - assert.commandWorked(sessionDb.runCommand({ - insert: collName, - documents: [{_id: "doc"}], - readConcern: {level: "snapshot"}, - txnNumber: NumberLong(++txnNumber), - startTransaction: true, - autocommit: false - })); - // commitTransaction can only be called on the admin database. - assert.commandWorked(sessionDb.adminCommand( - {commitTransaction: 1, txnNumber: NumberLong(txnNumber), autocommit: false})); + + session.startTransaction({writeConcern: {w: "majority"}}); + sessionColl.insert({_id: "doc"}); + session.commitTransaction(); assert.eq({_id: "doc"}, testColl.findOne({_id: "doc"})); // Insert fails when the collection does not exist. assert.commandWorked(testDB.runCommand({drop: collName, writeConcern: {w: "majority"}})); - assert.commandFailedWithCode(sessionDb.runCommand({ - insert: collName, - documents: [{_id: "doc"}], - readConcern: {level: "snapshot"}, - txnNumber: NumberLong(++txnNumber), - startTransaction: true, - autocommit: false - }), - ErrorCodes.NamespaceNotFound); - // commitTransaction can only be called on the admin database. - assert.commandFailedWithCode( - sessionDb.adminCommand( - {commitTransaction: 1, txnNumber: NumberLong(txnNumber), autocommit: false}), - ErrorCodes.NoSuchTransaction); + + session.startTransaction({writeConcern: {w: "majority"}}); + assert.commandFailedWithCode(sessionColl.insert({_id: "doc"}), ErrorCodes.NamespaceNotFound); + + // Committing the transaction should fail, since it should never have been started. + assert.commandFailedWithCode(session.commitTransaction_forTesting(), + ErrorCodes.NoSuchTransaction); assert.eq(null, testColl.findOne({_id: "doc"})); jsTest.log("Cannot implicitly create a collection in a transaction using update."); @@ -58,49 +43,30 @@ // Update with upsert=true succeeds when the collection exists. assert.commandWorked( testDB.createCollection(testColl.getName(), {writeConcern: {w: "majority"}})); - assert.commandWorked(sessionDb.runCommand({ - update: collName, - updates: [{q: {_id: "doc"}, u: {$set: {updated: true}}, upsert: true}], - readConcern: {level: "snapshot"}, - txnNumber: NumberLong(++txnNumber), - startTransaction: true, - autocommit: false - })); - // commitTransaction can only be called on the admin database. - assert.commandWorked(sessionDb.adminCommand( - {commitTransaction: 1, txnNumber: NumberLong(txnNumber), autocommit: false})); + + session.startTransaction({writeConcern: {w: "majority"}}); + sessionColl.update({_id: "doc"}, {$set: {updated: true}}, {upsert: true}); + session.commitTransaction(); assert.eq({_id: "doc", updated: true}, testColl.findOne({_id: "doc"})); // Update with upsert=true fails when the collection does not exist. assert.commandWorked(testDB.runCommand({drop: collName, writeConcern: {w: "majority"}})); - assert.commandFailedWithCode(sessionDb.runCommand({ - update: collName, - updates: [{q: {_id: "doc"}, u: {$set: {updated: true}}, upsert: true}], - readConcern: {level: "snapshot"}, - txnNumber: NumberLong(++txnNumber), - startTransaction: true, - autocommit: false - }), - ErrorCodes.NamespaceNotFound); - // commitTransaction can only be called on the admin database. + + session.startTransaction({writeConcern: {w: "majority"}}); assert.commandFailedWithCode( - sessionDb.adminCommand( - {commitTransaction: 1, txnNumber: NumberLong(txnNumber), autocommit: false}), - ErrorCodes.NoSuchTransaction); + sessionColl.update({_id: "doc"}, {$set: {updated: true}}, {upsert: true}), + ErrorCodes.NamespaceNotFound); + + // Committing the transaction should fail, since it should never have been started. + assert.commandFailedWithCode(session.commitTransaction_forTesting(), + ErrorCodes.NoSuchTransaction); assert.eq(null, testColl.findOne({_id: "doc"})); - // Update without upsert=true succeeds when the collection does not exist. - assert.commandWorked(sessionDb.runCommand({ - update: collName, - updates: [{q: {_id: "doc"}, u: {$set: {updated: true}}}], - readConcern: {level: "snapshot"}, - txnNumber: NumberLong(++txnNumber), - startTransaction: true, - autocommit: false - })); - // commitTransaction can only be called on the admin database. - assert.commandWorked(sessionDb.adminCommand( - {commitTransaction: 1, txnNumber: NumberLong(txnNumber), autocommit: false})); + // Update with upsert=false succeeds when the collection does not exist. + session.startTransaction({writeConcern: {w: "majority"}}); + assert.commandWorked( + sessionColl.update({_id: "doc"}, {$set: {updated: true}}, {upsert: false})); + session.commitTransaction(); assert.eq(null, testColl.findOne({_id: "doc"})); jsTest.log("Cannot implicitly create a collection in a transaction using findAndModify."); @@ -108,54 +74,33 @@ // findAndModify with upsert=true succeeds when the collection exists. assert.commandWorked( testDB.createCollection(testColl.getName(), {writeConcern: {w: "majority"}})); - assert.commandWorked(sessionDb.runCommand({ - findAndModify: collName, - query: {_id: "doc"}, - update: {$set: {updated: true}}, - upsert: true, - readConcern: {level: "snapshot"}, - txnNumber: NumberLong(++txnNumber), - startTransaction: true, - autocommit: false - })); - // commitTransaction can only be called on the admin database. - assert.commandWorked(sessionDb.adminCommand( - {commitTransaction: 1, txnNumber: NumberLong(txnNumber), autocommit: false})); + + session.startTransaction({writeConcern: {w: "majority"}}); + let res = sessionColl.findAndModify( + {query: {_id: "doc"}, update: {$set: {updated: true}}, upsert: true}); + assert.eq(null, res); + session.commitTransaction(); assert.eq({_id: "doc", updated: true}, testColl.findOne({_id: "doc"})); // findAndModify with upsert=true fails when the collection does not exist. assert.commandWorked(testDB.runCommand({drop: collName, writeConcern: {w: "majority"}})); - assert.commandFailedWithCode(sessionDb.runCommand({ - findAndModify: collName, - query: {_id: "doc"}, - update: {$set: {updated: true}}, - upsert: true, - readConcern: {level: "snapshot"}, - txnNumber: NumberLong(++txnNumber), - startTransaction: true, - autocommit: false - }), - ErrorCodes.NamespaceNotFound); - // commitTransaction can only be called on the admin database. - assert.commandFailedWithCode( - sessionDb.adminCommand( - {commitTransaction: 1, txnNumber: NumberLong(txnNumber), autocommit: false}), - ErrorCodes.NoSuchTransaction); + + session.startTransaction({writeConcern: {w: "majority"}}); + res = assert.throws(() => sessionColl.findAndModify( + {query: {_id: "doc"}, update: {$set: {updated: true}}, upsert: true})); + assert.commandFailedWithCode(res, ErrorCodes.NamespaceNotFound); + + // Committing the transaction should fail, since it should never have been started. + assert.commandFailedWithCode(session.commitTransaction_forTesting(), + ErrorCodes.NoSuchTransaction); assert.eq(null, testColl.findOne({_id: "doc"})); - // findAndModify without upsert=true succeeds when the collection does not exist. - assert.commandWorked(sessionDb.runCommand({ - findAndModify: collName, - query: {_id: "doc"}, - update: {$set: {updated: true}}, - readConcern: {level: "snapshot"}, - txnNumber: NumberLong(++txnNumber), - startTransaction: true, - autocommit: false - })); - // commitTransaction can only be called on the admin database. - assert.commandWorked(sessionDb.adminCommand( - {commitTransaction: 1, txnNumber: NumberLong(txnNumber), autocommit: false})); + // findAndModify with upsert=false succeeds when the collection does not exist. + session.startTransaction({writeConcern: {w: "majority"}}); + res = sessionColl.findAndModify( + {query: {_id: "doc"}, update: {$set: {updated: true}}, upsert: false}); + assert.eq(null, res); + session.commitTransaction(); assert.eq(null, testColl.findOne({_id: "doc"})); session.endSession(); |