description: "timeoutMS behaves correctly for tailable non-awaitData cursors"

schemaVersion: "1.9"

runOnRequirements:
  - minServerVersion: "4.4"

createEntities:
  - client:
      id: &failPointClient failPointClient
      useMultipleMongoses: false
  - client:
      id: &client client
      uriOptions:
        timeoutMS: 200
      useMultipleMongoses: false
      observeEvents:
        - commandStartedEvent
  - database:
      id: &database database
      client: *client
      databaseName: &databaseName test
  - collection:
      id: &collection collection
      database: *database
      collectionName: &collectionName coll

initialData:
  - collectionName: *collectionName
    databaseName: *databaseName
    createOptions:
      capped: true
      size: 500
    documents:
      - { _id: 0 }
      - { _id: 1 }

tests:
  - description: "error if timeoutMode is cursor_lifetime"
    operations:
      - name: find
        object: *collection
        arguments:
          filter: {}
          timeoutMode: cursorLifetime
          cursorType: tailable
        expectError:
          isClientError: true

  - description: "timeoutMS applied to find"
    operations:
      - name: failPoint
        object: testRunner
        arguments:
          client: *failPointClient
          failPoint:
            configureFailPoint: failCommand
            mode: { times: 1 }
            data:
              failCommands: ["find"]
              blockConnection: true
              blockTimeMS: 250
      - name: find
        object: *collection
        arguments:
          filter: {}
          cursorType: tailable
        expectError:
          isTimeoutError: true
    expectEvents:
      - client: *client
        events:
          # Due to SERVER-51153, the find command should not contain a maxTimeMS field for tailable non-awaitData
          # cursors because that would cap the lifetime of the created cursor.
          - commandStartedEvent:
              commandName: find
              databaseName: *databaseName
              command:
                find: *collectionName
                tailable: true
                awaitData: { $$exists: false }
                maxTimeMS: { $$exists: false }

  # The timeoutMS option should apply separately to the initial "find" and each getMore. This is a success test. The
  # find is executed with timeoutMS=20 and both find and getMore commands are configured to block for 15ms each. Neither
  # exceeds the timeout so the operation succeeds.
  - description: "timeoutMS is refreshed for getMore - success"
    operations:
      - name: failPoint
        object: testRunner
        arguments:
          client: *failPointClient
          failPoint:
            configureFailPoint: failCommand
            mode: { times: 2 }
            data:
              failCommands: ["find", "getMore"]
              blockConnection: true
              blockTimeMS: 150
      - name: createFindCursor
        object: *collection
        arguments:
          filter: {}
          cursorType: tailable
          timeoutMS: 200
          batchSize: 1
        saveResultAsEntity: &tailableCursor tailableCursor
      # Iterate the cursor twice: the first iteration will return the document from the batch in the find and the
      # second will do a getMore.
      - name: iterateUntilDocumentOrError
        object: *tailableCursor
      - name: iterateUntilDocumentOrError
        object: *tailableCursor
    expectEvents:
      - client: *client
        events:
          - commandStartedEvent:
              commandName: find
              databaseName: *databaseName
              command:
                find: *collectionName
                tailable: true
                awaitData: { $$exists: false }
                maxTimeMS: { $$exists: false }
          - commandStartedEvent:
              commandName: getMore
              databaseName: *databaseName
              command:
                getMore: { $$type: ["int", "long"] }
                collection: *collectionName
                maxTimeMS: { $$exists: false }

  # The timeoutMS option should apply separately to the initial "find" and each getMore. This is a failure test. The
  # find inherits timeoutMS=200 from the collection and the getMore command blocks for 250ms, causing iteration to fail
  # with a timeout error.
  - description: "timeoutMS is refreshed for getMore - failure"
    operations:
      - name: failPoint
        object: testRunner
        arguments:
          client: *failPointClient
          failPoint:
            configureFailPoint: failCommand
            mode: { times: 1 }
            data:
              failCommands: ["getMore"]
              blockConnection: true
              blockTimeMS: 250
      - name: createFindCursor
        object: *collection
        arguments:
          filter: {}
          cursorType: tailable
          batchSize: 1
        saveResultAsEntity: &tailableCursor tailableCursor
      # Iterate the cursor twice: the first iteration will return the document from the batch in the find and the
      # second will do a getMore.
      - name: iterateUntilDocumentOrError
        object: *tailableCursor
      - name: iterateUntilDocumentOrError
        object: *tailableCursor
        expectError:
          isTimeoutError: true
    expectEvents:
      - client: *client
        events:
          - commandStartedEvent:
              commandName: find
              databaseName: *databaseName
              command:
                find: *collectionName
                tailable: true
                awaitData: { $$exists: false }
                maxTimeMS: { $$exists: false }
          - commandStartedEvent:
              commandName: getMore
              databaseName: *databaseName
              command:
                getMore: { $$type: ["int", "long"] }
                collection: *collectionName
                maxTimeMS: { $$exists: false }
