jstests/core/txns/libs/write_conflicts.js
/**
     * Runs a specific write conflict test case.
     *
     * See the documentation above for further explanation of these test cases. After running the
     * test case, this function checks the final expected state of the collection, and then removes
     * all documents from the test collection.
     *
     * @param coll - a collection object that represents which collection to execute the test
     * operations against.
     * @param txn1Op - the command object to execute on transaction 1.
     * @param txn2Op - the command object to execute on transaction 2.
     * @param expectedDocs - an array of documents that is the expected state of the test collection
     * after both transactions are committed/aborted.
     * @param writeConflictTestCase - the write conflict test case to execute. it should be one of
     * the test case functions defined above in 'WriteConflictsTestCases'.
     * @param initOp (optional) - an operation to execute against the test collection before
     * starting either transaction.
     */
Runs a specific write conflict test case.
See the documentation above for further explanation of these test cases. After running the
test case, this function checks the final expected state of the collection, and then removes
all documents from the test collection.

@param coll - a collection object that represents which collection to execute the test
operations against.
@param txn1Op - the command object to execute on transaction 1.
@param txn2Op - the command object to execute on transaction 2.
@param expectedDocs - an array of documents that is the expected state of the test collection
after both transactions are committed/aborted.
@param writeConflictTestCase - the write conflict test case to execute. it should be one of
the test case functions defined above in 'WriteConflictsTestCases'.
@param initOp (optional) - an operation to execute against the test collection before
starting either transaction.
Runs a specific write conflict test case . See the documentation above for further explanation of these test cases . After running the test case this function checks the final expected state of the collection and then removes all documents from the test collection . @param coll - a collection object that represents which collection to execute the test operations against . @param txn1Op - the command object to execute on transaction 1 . @param txn2Op - the command object to execute on transaction 2 . @param expectedDocs - an array of documents that is the expected state of the test collection after both transactions are committed / aborted . @param writeConflictTestCase - the write conflict test case to execute . it should be one of the test case functions defined above in ' WriteConflictsTestCases ' . @param initOp ( optional ) - an operation to execute against the test collection before starting either transaction .
function writeConflictTest(coll, txn1Op, txn2Op, expectedDocs, writeConflictTestCase, initOp) {
        assert.commandWorked(coll.remove({}, {writeConcern: {w: "majority"}}));
        const testDB = coll.getDB();
        const conn = testDB.getMongo();
        const sessionOptions = {causalConsistency: false};
        const session1 = conn.startSession(sessionOptions);
        const session2 = conn.startSession(sessionOptions);
        jsTestLog("Executing write conflict test, case '" + writeConflictTestCase.name +
                  "'. \n transaction 1 op: " + tojson(txn1Op) +
                  "\n transaction 2 op: " + tojson(txn2Op));
        try {
            writeConflictTestCase(coll, session1, session2, txn1Op, txn2Op, initOp);
        } catch (e) {
            jsTestLog("Write conflict test case '" + writeConflictTestCase.name + "' failed.");
            let lsid1 = session1.getSessionId();
            let lsid2 = session2.getSessionId();
            print("Killing session with sessionID: " + tojson(lsid1));
            assert.commandWorked(testDB.runCommand({killSessions: [lsid1]}));
            print("Killing session with sessionID: " + tojson(lsid2));
            assert.commandWorked(testDB.runCommand({killSessions: [lsid2]}));
            throw e;
        }
        assert.sameMembers(expectedDocs, coll.find().toArray());
        assert.commandWorked(coll.remove({}, {writeConcern: {w: "majority"}}));
    }
function writeConflictTest ( coll , txn1Op , txn2Op , expectedDocs , writeConflictTestCase , initOp ) { assert . commandWorked ( coll . remove ( { } , { writeConcern : { w : "majority" } } ) ) ; const testDB = coll . getDB ( ) ; const conn = testDB . getMongo ( ) ; const sessionOptions = { causalConsistency : false } ; const session1 = conn . startSession ( sessionOptions ) ; const session2 = conn . startSession ( sessionOptions ) ; jsTestLog ( "Executing write conflict test, case '" + writeConflictTestCase . name + "'. \n transaction 1 op: " + tojson ( txn1Op ) + "\n transaction 2 op: " + tojson ( txn2Op ) ) ; try { writeConflictTestCase ( coll , session1 , session2 , txn1Op , txn2Op , initOp ) ; } catch ( e ) { jsTestLog ( "Write conflict test case '" + writeConflictTestCase . name + "' failed." ) ; let lsid1 = session1 . getSessionId ( ) ; let lsid2 = session2 . getSessionId ( ) ; print ( "Killing session with sessionID: " + tojson ( lsid1 ) ) ; assert . commandWorked ( testDB . runCommand ( { killSessions : [ lsid1 ] } ) ) ; print ( "Killing session with sessionID: " + tojson ( lsid2 ) ) ; assert . commandWorked ( testDB . runCommand ( { killSessions : [ lsid2 ] } ) ) ; throw e ; } assert . sameMembers ( expectedDocs , coll . find ( ) . toArray ( ) ) ; assert . commandWorked ( coll . remove ( { } , { writeConcern : { w : "majority" } } ) ) ; }
Runs a specific write conflict test case.
Runs a specific write conflict test case .
// Make sure the collection is empty. // Initiate two sessions. // Run the specified write conflict test. // We make sure to abort any idle transactions. // Check the final state of the collection. // Clean up the collection.