description: "timeoutMS behaves correctly for GridFS find operations"

schemaVersion: "1.9"

runOnRequirements:
  - minServerVersion: "4.4"
    serverless: forbid  # GridFS ops can be slow on serverless.

createEntities:
  - client:
      id: &failPointClient failPointClient
      useMultipleMongoses: false
  - client:
      id: &client client
      uriOptions:
        timeoutMS: 75
      useMultipleMongoses: false
      observeEvents:
        - commandStartedEvent
  - database:
      id: &database database
      client: *client
      databaseName: &databaseName test
  - bucket:
      id: &bucket bucket
      database: *database
  - collection:
      id: &filesCollection filesCollection
      database: *database
      collectionName: &filesCollectionName fs.files
  - collection:
      id: &chunksCollection chunksCollection
      database: *database
      collectionName: &chunksCollectionName fs.chunks

initialData:
  - collectionName: *filesCollectionName
    databaseName: *databaseName
    documents: []
  - collectionName: *chunksCollectionName
    databaseName: *databaseName
    documents: []

tests:
  - description: "timeoutMS can be overridden for a find"
    operations:
      - name: failPoint
        object: testRunner
        arguments:
          client: *failPointClient
          failPoint:
            configureFailPoint: failCommand
            mode: { times: 1 }
            data:
              failCommands: ["find"]
              blockConnection: true
              blockTimeMS: 100
      - name: find
        object: *bucket
        arguments:
          filter: {}
          timeoutMS: 1000 # The client timeoutMS is 75ms and the operation blocks for 100ms, so 1000ms should let it succeed.
    expectEvents:
      - client: *client
        events:
          - commandStartedEvent:
              commandName: find
              databaseName: *databaseName
              command:
                find: *filesCollectionName
                maxTimeMS: { $$type: ["int", "long"] }

  - description: "timeoutMS applied to find command"
    operations:
      - name: failPoint
        object: testRunner
        arguments:
          client: *failPointClient
          failPoint:
            configureFailPoint: failCommand
            mode: { times: 1 }
            data:
              failCommands: ["find"]
              blockConnection: true
              blockTimeMS: 100
      - name: find
        object: *bucket
        arguments:
          filter: {}
        expectError:
          isTimeoutError: true
    expectEvents:
      - client: *client
        events:
          - commandStartedEvent:
              commandName: find
              databaseName: *databaseName
              command:
                find: *filesCollectionName
                maxTimeMS: { $$type: ["int", "long"] }
