jstests/noPassthrough/awaitable_hello.js
// runTest takes in the hello command or its aliases, isMaster and ismaster.
runTest takes in the hello command or its aliases, isMaster and ismaster.
runTest takes in the hello command or its aliases isMaster and ismaster .
function runTest(db, cmd, logFailpoint) {
    const res = assert.commandWorked(db.runCommand(cmd));
    assert(res.hasOwnProperty("topologyVersion"), tojson(res));
    const topologyVersionField = res.topologyVersion;
    assert(topologyVersionField.hasOwnProperty("processId"), tojson(topologyVersionField));
    assert(topologyVersionField.hasOwnProperty("counter"), tojson(topologyVersionField));
    assert.commandWorked(
        db.runCommand({[cmd]: 1, topologyVersion: topologyVersionField, maxAwaitTimeMS: 0}));
    assert.commandWorked(db.adminCommand({clearLog: 'global'}));
    let now = new Date();
    jsTestLog(`Running slow ${cmd}`);
    const timesEnteredBeforeRunningCommand = configureFailPoint(db, logFailpoint).timesEntered;
    assert.commandWorked(
        db.runCommand({[cmd]: 1, topologyVersion: topologyVersionField, maxAwaitTimeMS: 20000}));
    let commandDuration = new Date() - now;
    assert.gte(
        commandDuration,
        10000,
        cmd + ` command should have taken at least 10000ms, but completed in ${commandDuration}ms`);
    const timesEnteredAfterRunningCommand = configureFailPoint(db, logFailpoint).timesEntered;
    assert(timesEnteredBeforeRunningCommand == timesEnteredAfterRunningCommand);
    function runHelloCommand(cmd, topologyVersionField) {
        assert.commandWorked(
            db.runCommand({[cmd]: 1, topologyVersion: topologyVersionField, maxAwaitTimeMS: 1}));
        jsTestLog(`${cmd} completed in parallel shell`);
    }
    assert.commandWorked(db.adminCommand({clearLog: 'global'}));
    const helloFailpoint = configureFailPoint(db, "waitInHello", {}, {skip: 1});
    const logFailPoint = configureFailPoint(db, logFailpoint);
    const awaitHello = startParallelShell(funWithArgs(runHelloCommand, cmd, topologyVersionField),
                                          db.getMongo().port);
    helloFailpoint.wait();
    sleep(1000);  
    helloFailpoint.off();
    awaitHello();
    logFailPoint.wait();
    now = new Date();
    assert.commandWorked(db.runCommand({
        [cmd]: 1,
        topologyVersion: {processId: ObjectId(), counter: topologyVersionField.counter},
        maxAwaitTimeMS: 2000
    }));
    commandDuration = new Date() - now;
    assert.lt(
        commandDuration,
        1000,
        cmd + ` command should have taken at most 1000ms, but completed in ${commandDuration}ms`);
    assert.commandWorked(db.runCommand({
        [cmd]: 1,
        topologyVersion:
            {processId: ObjectId(), counter: NumberLong(topologyVersionField.counter + 1)},
        maxAwaitTimeMS: 0
    }));
    assert.commandFailedWithCode(db.runCommand({
        [cmd]: 1,
        topologyVersion: {
            processId: topologyVersionField.processId,
            counter: NumberLong(topologyVersionField.counter + 1)
        },
        maxAwaitTimeMS: 0
    }),
                                 [31382, 51761, 51764]);
    assert.commandFailedWithCode(
        db.runCommand({[cmd]: 1, topologyVersion: "topology_version_string", maxAwaitTimeMS: 0}),
        ErrorCodes.TypeMismatch);
    assert.commandFailedWithCode(db.runCommand({
        [cmd]: 1,
        topologyVersion: {processId: "pid1", counter: topologyVersionField.counter},
        maxAwaitTimeMS: 0
    }),
                                 ErrorCodes.TypeMismatch);
    assert.commandFailedWithCode(db.runCommand({
        [cmd]: 1,
        topologyVersion: {processId: topologyVersionField.processId, counter: 0},
        maxAwaitTimeMS: 0
    }),
                                 ErrorCodes.TypeMismatch);
    assert.commandFailedWithCode(db.runCommand({
        [cmd]: 1,
        topologyVersion: {processId: topologyVersionField.processId},
        maxAwaitTimeMS: 0
    }),
                                 40414);
    assert.commandFailedWithCode(db.runCommand({
        [cmd]: 1,
        topologyVersion: {counter: topologyVersionField.counter},
        maxAwaitTimeMS: 0
    }),
                                 40414);
    assert.commandFailedWithCode(db.runCommand({
        [cmd]: 1,
        topologyVersion: {processId: topologyVersionField.processId, counter: NumberLong("-1")},
        maxAwaitTimeMS: 0
    }),
                                 [31372, 51758]);
    assert.commandFailedWithCode(db.runCommand({
        [cmd]: 1,
        topologyVersion: {
            processId: topologyVersionField.processId,
            counter: topologyVersionField.counter,
            randomField: "I should cause an error"
        },
        maxAwaitTimeMS: 0
    }),
                                 40415);
    assert.commandFailedWithCode(db.runCommand({[cmd]: 1, topologyVersion: topologyVersionField}),
                                 [31368, 51760]);
    assert.commandFailedWithCode(db.runCommand({[cmd]: 1, maxAwaitTimeMS: 0}), [31368, 51760]);
    assert.commandFailedWithCode(db.runCommand({
        [cmd]: 1,
        topologyVersion: topologyVersionField,
        maxAwaitTimeMS: "stringMaxAwaitTimeMS"
    }),
                                 ErrorCodes.TypeMismatch);
    assert.commandFailedWithCode(db.runCommand({
        [cmd]: 1,
        topologyVersion: topologyVersionField,
        maxAwaitTimeMS: -1,
    }),
                                 [31373, 51759, kIDLParserComparisonError]);
}
function runTest ( db , cmd , logFailpoint ) { const res = assert . commandWorked ( db . runCommand ( cmd ) ) ; assert ( res . hasOwnProperty ( "topologyVersion" ) , tojson ( res ) ) ; const topologyVersionField = res . topologyVersion ; assert ( topologyVersionField . hasOwnProperty ( "processId" ) , tojson ( topologyVersionField ) ) ; assert ( topologyVersionField . hasOwnProperty ( "counter" ) , tojson ( topologyVersionField ) ) ; assert . commandWorked ( db . runCommand ( { [ cmd ] : 1 , topologyVersion : topologyVersionField , maxAwaitTimeMS : 0 } ) ) ; assert . commandWorked ( db . adminCommand ( { clearLog : 'global' } ) ) ; let now = new Date ( ) ; jsTestLog ( ` ${ cmd } ` ) ; const timesEnteredBeforeRunningCommand = configureFailPoint ( db , logFailpoint ) . timesEntered ; assert . commandWorked ( db . runCommand ( { [ cmd ] : 1 , topologyVersion : topologyVersionField , maxAwaitTimeMS : 20000 } ) ) ; let commandDuration = new Date ( ) - now ; assert . gte ( commandDuration , 10000 , cmd + ` ${ commandDuration } ` ) ; const timesEnteredAfterRunningCommand = configureFailPoint ( db , logFailpoint ) . timesEntered ; assert ( timesEnteredBeforeRunningCommand == timesEnteredAfterRunningCommand ) ; function runHelloCommand ( cmd , topologyVersionField ) { assert . commandWorked ( db . runCommand ( { [ cmd ] : 1 , topologyVersion : topologyVersionField , maxAwaitTimeMS : 1 } ) ) ; jsTestLog ( ` ${ cmd } ` ) ; } assert . commandWorked ( db . adminCommand ( { clearLog : 'global' } ) ) ; const helloFailpoint = configureFailPoint ( db , "waitInHello" , { } , { skip : 1 } ) ; const logFailPoint = configureFailPoint ( db , logFailpoint ) ; const awaitHello = startParallelShell ( funWithArgs ( runHelloCommand , cmd , topologyVersionField ) , db . getMongo ( ) . port ) ; helloFailpoint . wait ( ) ; sleep ( 1000 ) ; helloFailpoint . off ( ) ; awaitHello ( ) ; logFailPoint . wait ( ) ; now = new Date ( ) ; assert . commandWorked ( db . runCommand ( { [ cmd ] : 1 , topologyVersion : { processId : ObjectId ( ) , counter : topologyVersionField . counter } , maxAwaitTimeMS : 2000 } ) ) ; commandDuration = new Date ( ) - now ; assert . lt ( commandDuration , 1000 , cmd + ` ${ commandDuration } ` ) ; assert . commandWorked ( db . runCommand ( { [ cmd ] : 1 , topologyVersion : { processId : ObjectId ( ) , counter : NumberLong ( topologyVersionField . counter + 1 ) } , maxAwaitTimeMS : 0 } ) ) ; assert . commandFailedWithCode ( db . runCommand ( { [ cmd ] : 1 , topologyVersion : { processId : topologyVersionField . processId , counter : NumberLong ( topologyVersionField . counter + 1 ) } , maxAwaitTimeMS : 0 } ) , [ 31382 , 51761 , 51764 ] ) ; assert . commandFailedWithCode ( db . runCommand ( { [ cmd ] : 1 , topologyVersion : "topology_version_string" , maxAwaitTimeMS : 0 } ) , ErrorCodes . TypeMismatch ) ; assert . commandFailedWithCode ( db . runCommand ( { [ cmd ] : 1 , topologyVersion : { processId : "pid1" , counter : topologyVersionField . counter } , maxAwaitTimeMS : 0 } ) , ErrorCodes . TypeMismatch ) ; assert . commandFailedWithCode ( db . runCommand ( { [ cmd ] : 1 , topologyVersion : { processId : topologyVersionField . processId , counter : 0 } , maxAwaitTimeMS : 0 } ) , ErrorCodes . TypeMismatch ) ; assert . commandFailedWithCode ( db . runCommand ( { [ cmd ] : 1 , topologyVersion : { processId : topologyVersionField . processId } , maxAwaitTimeMS : 0 } ) , 40414 ) ; assert . commandFailedWithCode ( db . runCommand ( { [ cmd ] : 1 , topologyVersion : { counter : topologyVersionField . counter } , maxAwaitTimeMS : 0 } ) , 40414 ) ; assert . commandFailedWithCode ( db . runCommand ( { [ cmd ] : 1 , topologyVersion : { processId : topologyVersionField . processId , counter : NumberLong ( "-1" ) } , maxAwaitTimeMS : 0 } ) , [ 31372 , 51758 ] ) ; assert . commandFailedWithCode ( db . runCommand ( { [ cmd ] : 1 , topologyVersion : { processId : topologyVersionField . processId , counter : topologyVersionField . counter , randomField : "I should cause an error" } , maxAwaitTimeMS : 0 } ) , 40415 ) ; assert . commandFailedWithCode ( db . runCommand ( { [ cmd ] : 1 , topologyVersion : topologyVersionField } ) , [ 31368 , 51760 ] ) ; assert . commandFailedWithCode ( db . runCommand ( { [ cmd ] : 1 , maxAwaitTimeMS : 0 } ) , [ 31368 , 51760 ] ) ; assert . commandFailedWithCode ( db . runCommand ( { [ cmd ] : 1 , topologyVersion : topologyVersionField , maxAwaitTimeMS : "stringMaxAwaitTimeMS" } ) , ErrorCodes . TypeMismatch ) ; assert . commandFailedWithCode ( db . runCommand ( { [ cmd ] : 1 , topologyVersion : topologyVersionField , maxAwaitTimeMS : - 1 , } ) , [ 31373 , 51759 , kIDLParserComparisonError ] ) ; }
runTest takes in the hello command or its aliases, isMaster and ismaster.
runTest takes in the hello command or its aliases isMaster and ismaster .
// Check the command response contains a topologyVersion even if maxAwaitTimeMS and // topologyVersion are not included in the request. // Check that the command succeeds when passed a valid topologyVersion and maxAwaitTimeMS. In // this case, use the topologyVersion from the previous command response. The topologyVersion // field is expected to be of the form {processId: <ObjectId>, counter: <long>}. // Ensure the command waits for at least maxAwaitTimeMS before returning, and doesn't appear in // slow query log even if it takes many seconds. // Get the slow query log failpoint for the command, to know the current timesEntered before // the command runs. // Allow for some clock imprecision between the server and the jstest. // Get the slow query log failpoint for the command, to make sure that it didn't get hit during // the command run by checking that timesEntered is the same as before. // Check that the command appears in the slow query log if it's unexpectedly slow. // Use a skip of 1, since the parallel shell runs hello when it starts. // Make the command hang for a second in the parallel shell. // Wait for the parallel shell to finish. // Wait for the command to be logged. // Check that when a different processId is given, the server responds immediately. // Check that when a different processId is given, a higher counter is allowed. // Check that when the same processId is given, a higher counter is not allowed. // Check that passing a topologyVersion not of type object fails. // Check that a topologyVersion with an invalid processId and valid counter fails. // Check that a topologyVersion with a valid processId and invalid counter fails. // Check that a topologyVersion with a valid processId but missing counter fails. // Check that a topologyVersion with a missing processId and valid counter fails. // Check that a topologyVersion with a valid processId and negative counter fails. // Check that the command fails if there is an extra field in its topologyVersion. // A client following the awaitable hello/isMaster protocol must include topologyVersion in // their request if and only if they include maxAwaitTimeMS. Check that the command fails if // there is a topologyVersion but no maxAwaitTimeMS field. // Check that the command fails if there is a maxAwaitTimeMS field but no topologyVersion. // Check that the command fails if there is a valid topologyVersion but invalid maxAwaitTimeMS // type. // Check that the command fails if there is a valid topologyVersion but negative maxAwaitTimeMS.