description: getMore-retried-backpressure
schemaVersion: "1.3"
runOnRequirements:
  - minServerVersion: '4.4' # failCommand
createEntities:
  - client:
      id: &client client0
      useMultipleMongoses: false
      observeEvents:
        - commandStartedEvent
        - commandFailedEvent
        - commandSucceededEvent
  - client:
      id: &failPointClient failPointClient
      useMultipleMongoses: false
  - database:
      id: db
      client: *client
      databaseName: &dbName default
  - collection:
      id: &collection coll
      database: db
      collectionName: &collectionName default
initialData:
  - databaseName: *dbName
    collectionName: *collectionName
    documents:
      - { a: 1 }
      - { a: 2 }
      - { a: 3 }

tests:
  - description: "getMores are retried"
    operations:
      - name: failPoint
        object: testRunner
        arguments:
          client: *failPointClient
          failPoint:
            configureFailPoint: failCommand
            mode: { times: 2 }
            data:
              failCommands: [getMore]
              errorLabels: [RetryableError, SystemOverloadedError]
              errorCode: 2

      - name: find
        object: *collection
        arguments:
          # batch size of 2 with 3 docs in the collection ensures exactly one find + one getMore exhaust the cursor
          batchSize: 2
          filter: {}
          # ensure stable ordering of result documents
          sort: { a: 1 }
        expectResult:
          - { a: 1 }
          - { a: 2 }
          - { a: 3 }

    expectEvents:
      - client: *client
        events:
          - commandStartedEvent:
              commandName: find
          - commandSucceededEvent:
              commandName: find
          # first attempt
          - commandStartedEvent:
              commandName: getMore
          - commandFailedEvent:
              commandName: getMore
          # second attempt
          - commandStartedEvent:
              commandName: getMore
          - commandFailedEvent:
              commandName: getMore
          # success
          - commandStartedEvent:
              commandName: getMore
          - commandSucceededEvent:
              commandName: getMore

  - description: "getMores are retried maxAttempts=2 times"
    operations:
      - name: failPoint
        object: testRunner
        arguments:
          client: *failPointClient
          failPoint:
            configureFailPoint: failCommand
            mode: alwaysOn
            data:
              failCommands: [getMore]
              errorLabels: [RetryableError, SystemOverloadedError]
              errorCode: 2

      - name: find
        arguments:
          batchSize: 2
          filter: {}
        object: *collection
        expectError:
          isError: true
          isClientError: false

    expectEvents:
      - client: *client
        events:
          - commandStartedEvent:
              commandName: find
          - commandSucceededEvent:
              commandName: find
          # first attempt
          - commandStartedEvent:
              commandName: getMore
          - commandFailedEvent:
              commandName: getMore
          # second attempt
          - commandStartedEvent:
              commandName: getMore
          - commandFailedEvent:
              commandName: getMore
          # third attempt
          - commandStartedEvent:
              commandName: getMore
          - commandFailedEvent:
              commandName: getMore
          - commandStartedEvent:
              commandName: killCursors
          - commandSucceededEvent:
              commandName: killCursors
