description: "timeoutMS behaves correctly for bulkWrite operations"

schemaVersion: "1.9"

runOnRequirements:
  - minServerVersion: "4.4"

createEntities:
  - client:
      id: &failPointClient failPointClient
      useMultipleMongoses: false
  - client:
      id: &client client
      useMultipleMongoses: false
      observeEvents:
        - commandStartedEvent
      uriOptions:
        # Used to speed up the test
        w: 1
  - database:
      id: &database database
      client: *client
      databaseName: &databaseName test
  - collection:
      id: &collection collection
      database: *database
      collectionName: &collectionName coll 

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

tests:
  # Test that drivers do not refresh timeoutMS between commands. This is done by running a bulkWrite that will require
  # two commands with timeoutMS=200 and blocking each command for 120ms. The server should take over 200ms total, so the
  # bulkWrite should fail with a timeout error.
  - description: "timeoutMS applied to entire bulkWrite, not individual commands"
    operations:
      # Do an operation without a timeout to ensure the servers are discovered.
      - name: insertOne
        object: *collection
        arguments:
          document: {}
      - name: failPoint
        object: testRunner
        arguments:
          client: *failPointClient
          failPoint:
            configureFailPoint: failCommand
            mode: { times: 2 }
            data:
              failCommands: ["insert", "update"]
              blockConnection: true
              blockTimeMS: 120
      - name: bulkWrite
        object: *collection
        arguments:
          requests:
            - insertOne:
                document: { _id: 1 }
            - replaceOne:
                filter: { _id: 1 }
                replacement: { x: 1 }
          timeoutMS: 200
        expectError:
          isTimeoutError: true
    expectEvents:
      - client: *client
        events:
          - commandStartedEvent:
              commandName: insert
              databaseName: *databaseName
              command:
                insert: *collectionName
          - commandStartedEvent:
              commandName: insert
              databaseName: *databaseName
              command:
                insert: *collectionName
                maxTimeMS: { $$type: ["int", "long"] }
          - commandStartedEvent:
              commandName: update
              databaseName: *databaseName
              command:
                update: *collectionName
                maxTimeMS: { $$type: ["int", "long"] }
