jstests/libs/write_concern_util.js
/**
 * Tests that a command properly waits for writeConcern on retry. Takes an optional
 * 'setupFunc' that sets up the database state. 'setupFunc' accepts a connection to the
 * primary.
 */
Tests that a command properly waits for writeConcern on retry. Takes an optional
'setupFunc' that sets up the database state. 'setupFunc' accepts a connection to the
primary.
Tests that a command properly waits for writeConcern on retry . Takes an optional ' setupFunc ' that sets up the database state . ' setupFunc ' accepts a connection to the primary .
function runWriteConcernRetryabilityTest(priConn, secConn, cmd, kNodes, dbName, setupFunc) {
    dbName = dbName || "test";
    jsTestLog(`Testing ${tojson(cmd)} on ${dbName}.`);
    assert.commandWorked(priConn.adminCommand(
        {setDefaultRWConcern: 1, defaultWriteConcern: {w: 1}, writeConcern: {w: "majority"}}));
    const secondPriConn = new Mongo(priConn.host);
    const testDB2 = secondPriConn.getDB(dbName);
    assert.commandWorked(testDB2.dummy.insert({x: 1}, {writeConcern: {w: kNodes}}));
    if (setupFunc) {
        setupFunc(priConn);
    }
    stopServerReplication(secConn);
    const testDB = priConn.getDB(dbName);
    checkWriteConcernTimedOut(testDB.runCommand(cmd));
    checkWriteConcernTimedOut(testDB2.runCommand(cmd));
    checkWriteConcernTimedOut(testDB.runCommand(cmd));
    assert.commandWorked(testDB.dummy.insert({x: 2}));
    assert.commandWorked(testDB2.dummy.insert({x: 3}));
    checkWriteConcernTimedOut(testDB.runCommand(cmd));
    checkWriteConcernTimedOut(testDB2.runCommand(cmd));
    restartServerReplication(secConn);
}
function runWriteConcernRetryabilityTest ( priConn , secConn , cmd , kNodes , dbName , setupFunc ) { dbName = dbName || "test" ; jsTestLog ( ` ${ tojson ( cmd ) } ${ dbName } ` ) ; assert . commandWorked ( priConn . adminCommand ( { setDefaultRWConcern : 1 , defaultWriteConcern : { w : 1 } , writeConcern : { w : "majority" } } ) ) ; const secondPriConn = new Mongo ( priConn . host ) ; const testDB2 = secondPriConn . getDB ( dbName ) ; assert . commandWorked ( testDB2 . dummy . insert ( { x : 1 } , { writeConcern : { w : kNodes } } ) ) ; if ( setupFunc ) { setupFunc ( priConn ) ; } stopServerReplication ( secConn ) ; const testDB = priConn . getDB ( dbName ) ; checkWriteConcernTimedOut ( testDB . runCommand ( cmd ) ) ; checkWriteConcernTimedOut ( testDB2 . runCommand ( cmd ) ) ; checkWriteConcernTimedOut ( testDB . runCommand ( cmd ) ) ; assert . commandWorked ( testDB . dummy . insert ( { x : 2 } ) ) ; assert . commandWorked ( testDB2 . dummy . insert ( { x : 3 } ) ) ; checkWriteConcernTimedOut ( testDB . runCommand ( cmd ) ) ; checkWriteConcernTimedOut ( testDB2 . runCommand ( cmd ) ) ; restartServerReplication ( secConn ) ; }
Tests that a command properly waits for writeConcern on retry.
Tests that a command properly waits for writeConcern on retry .
// The default WC is majority and stopServerReplication will prevent the replica set from // fulfilling any majority writes // Send a dummy write to this connection so it will have the Client object initialized. // Retry the command on the new connection whose lastOp will be less than the main connection. // Retry the command on the main connection whose lastOp will not have changed. // Bump forward the client lastOp on both connections and try again on both.