description: "WaitQueueTimeoutError does not clear the pool"

schemaVersion: "1.9"

runOnRequirements:
  - minServerVersion: "4.4"
    topologies: ["single", "replicaset", "sharded"]

createEntities:
  - client:
      id: &failPointClient failPointClient
      useMultipleMongoses: false
  - client:
      id: &client client
      uriOptions:
        maxPoolSize: 1
        appname: &appname waitQueueTimeoutErrorTest
      useMultipleMongoses: false
      observeEvents:
        - commandStartedEvent
        - poolClearedEvent
  - database:
      id: &database database
      client: *client
      databaseName: &databaseName test

tests:
  - description: "WaitQueueTimeoutError does not clear the pool"
    operations:
      - name: failPoint
        object: testRunner
        arguments:
          client: *failPointClient
          failPoint:
            configureFailPoint: failCommand
            mode: { times: 1 }
            data:
              failCommands: ["ping"]
              blockConnection: true
              blockTimeMS: 500
              appName: *appname
      # Start thread.
      - name: createEntities
        object: testRunner
        arguments:
          entities:
            - thread:
                id: &thread0 thread0
      - name: runOnThread
        object: testRunner
        arguments:
          thread: *thread0
          operation:
            name: runCommand
            object: *database
            arguments:
              command: { ping: 1 }
              commandName: ping
      # Wait for the thread to checkout the only connection (maxPoolSize=1).
      - name: waitForEvent
        object: testRunner
        arguments:
          client: *client
          event:
            commandStartedEvent:
              commandName: ping
          count: 1
      # Run another command with a short timeout to make it likely to get a WaitQueueTimeoutError.
      - name: runCommand
        object: *database
        arguments:
          timeoutMS: 100
          command: { hello: 1 }
          commandName: hello
        expectError:
          isTimeoutError: true
      - name: waitForThread
        object: testRunner
        arguments:
          thread: *thread0
      # Run another command with no timeout to ensure the pool is not cleared.
      - name: runCommand
        object: *database
        arguments:
          command: { hello: 1 }
          commandName: hello

    expectEvents:
      - client: *client
        eventType: command
        events:
          - commandStartedEvent:
              commandName: ping
              databaseName: *databaseName
              command:
                ping: 1
          - commandStartedEvent:
              commandName: hello
              databaseName: *databaseName
              command:
                hello: 1
      # No poolClearedEvent.
      - client: *client
        eventType: cmap
        events: []
