jstests/change_streams/projection_fakes_internal_event.js
// further confirm that the faked events do not cause additional cursors to be opened.
further confirm that the faked events do not cause additional cursors to be opened.
further confirm that the faked events do not cause additional cursors to be opened .
function assertChangeStreamBehaviour(projection, expectedEvents, expectedErrorCode = null) {
    assert(!(expectedEvents && expectedErrorCode));
    const commentID = `${Math.random()}`;
    var csCursor = testColl.watch([{$addFields: projection}],
                                  {startAtOperationTime: Timestamp(1, 1), comment: commentID});
    if (expectedEvents && expectedEvents.length > 0) {
        for (let expectedEvent of expectedEvents) {
            assert.soon(() => csCursor.hasNext());
            const nextEvent = csCursor.next();
            for (let fieldName in expectedEvent) {
                assert.eq(
                    expectedEvent[fieldName], nextEvent[fieldName], {expectedEvent, nextEvent});
            }
        }
    }
    if (expectedEvents && expectedEvents.length == 0) {
        const startPoint = csCursor.getResumeToken();
        assert.soon(() => {
            assert(!csCursor.hasNext(), () => tojson(csCursor.next()));
            return bsonWoCompare(csCursor.getResumeToken(), startPoint) > 0;
        });
    }
    if (expectedErrorCode) {
        assert.soon(() => {
            try {
                assert.throwsWithCode(() => csCursor.hasNext(), expectedErrorCode);
            } catch (err) {
                return false;
            }
            return true;
        });
    } else {
        const openCursors = adminDB
                                .aggregate([
                                    {$currentOp: {idleCursors: true}},
                                    {$match: {"cursor.originatingCommand.comment": commentID}},
                                    {
                                        $group: {
                                            _id: {shard: "$shard", cursorId: "$cursor.cursorId"},
                                            currentOps: {$push: "$$ROOT"}
                                        }
                                    }
                                ])
                                .toArray();
        assert.eq(openCursors.length, numShards, openCursors);
    }
    csCursor.close();
}
function assertChangeStreamBehaviour ( projection , expectedEvents , expectedErrorCode = null ) { assert ( ! ( expectedEvents && expectedErrorCode ) ) ; const commentID = ` ${ Math . random ( ) } ` ; var csCursor = testColl . watch ( [ { $addFields : projection } ] , { startAtOperationTime : Timestamp ( 1 , 1 ) , comment : commentID } ) ; if ( expectedEvents && expectedEvents . length > 0 ) { for ( let expectedEvent of expectedEvents ) { assert . soon ( ( ) => csCursor . hasNext ( ) ) ; const nextEvent = csCursor . next ( ) ; for ( let fieldName in expectedEvent ) { assert . eq ( expectedEvent [ fieldName ] , nextEvent [ fieldName ] , { expectedEvent , nextEvent } ) ; } } } if ( expectedEvents && expectedEvents . length == 0 ) { const startPoint = csCursor . getResumeToken ( ) ; assert . soon ( ( ) => { assert ( ! csCursor . hasNext ( ) , ( ) => tojson ( csCursor . next ( ) ) ) ; return bsonWoCompare ( csCursor . getResumeToken ( ) , startPoint ) > 0 ; } ) ; } if ( expectedErrorCode ) { assert . soon ( ( ) => { try { assert . throwsWithCode ( ( ) => csCursor . hasNext ( ) , expectedErrorCode ) ; } catch ( err ) { return false ; } return true ; } ) ; } else { const openCursors = adminDB . aggregate ( [ { $currentOp : { idleCursors : true } } , { $match : { "cursor.originatingCommand.comment" : commentID } } , { $group : { _id : { shard : "$shard" , cursorId : "$cursor.cursorId" } , currentOps : { $push : "$$ROOT" } } } ] ) . toArray ( ) ; assert . eq ( openCursors . length , numShards , openCursors ) ; } csCursor . close ( ) ; }
further confirm that the faked events do not cause additional cursors to be opened.
further confirm that the faked events do not cause additional cursors to be opened .
// Can't expect both to see events and to throw an exception. // Generate a random ID for this stream. // Create a change stream cursor with the specified projection. // Confirm that the observed events match the expected events, if specified. // If there are no expected events, confirm that the token advances without seeing anything. // If we expect an error code, assert that we throw it soon. // Otherwise, confirm that we still only have a single cursor on each shard. It's possible // that the same cursor will be listed as both active and inactive, so group by cursorId. // Close the change stream when we are done.