---
description: pool-cleared-on-min-pool-size-population-error

schemaVersion: "1.4"

runOnRequirements:
  # failCommand appName requirements
  - minServerVersion: "4.4"
    serverless: forbid
    topologies: [ single ]

createEntities:
  - client:
      id: &setupClient setupClient
      useMultipleMongoses: false

tests:
  - description: Pool is cleared on authentication error during minPoolSize population
    runOnRequirements:
      # failCommand appName requirements
      - auth: true
    operations:
      - name: failPoint
        object: testRunner
        arguments:
          client: *setupClient
          failPoint:
            configureFailPoint: failCommand
            mode:
              times: 1
            data:
              failCommands:
                - saslContinue
              appName: authErrorTest
              errorCode: 18
      - name: createEntities
        object: testRunner
        arguments:
          entities:
            - client:
                id: &client client
                observeEvents:
                  - poolReadyEvent
                  - poolClearedEvent
                  - connectionClosedEvent
                uriOptions:
                  appname: authErrorTest
                  minPoolSize: 1

      - name: waitForEvent
        object: testRunner
        arguments:
          client: *client
          event:
            poolReadyEvent: {}
          count: 1
      
      - name: waitForEvent
        object: testRunner
        arguments:
          client: *client
          event:
            poolClearedEvent: {}
          count: 1

      - name: waitForEvent
        object: testRunner
        arguments:
          client: *client
          event:
            connectionClosedEvent: {}
          count: 1

  - description: Pool is not cleared on handshake error during minPoolSize population
    operations:
      - name: failPoint
        object: testRunner
        arguments:
          client: *setupClient
          failPoint:
            configureFailPoint: failCommand
            mode:
              skip: 1 # skip one to let monitoring thread to move pool to ready state
            data:
              failCommands:
                - hello
                - isMaster
              appName: authErrorTest
              closeConnection: true
        
      - name: createEntities
        object: testRunner
        arguments:
          entities:
            - client:
                id: &client client
                observeEvents:
                  - poolReadyEvent
                  - poolClearedEvent
                  - connectionClosedEvent
                uriOptions:
                  appname: authErrorTest
                  minPoolSize: 5
                  maxConnecting: 1
                  # ensure that once we've connected to the server, the failCommand won't
                  # be triggered by monitors and will only be triggered by handshakes
                  serverMonitoringMode: poll
                  heartbeatFrequencyMS: 1000000

      - name: waitForEvent
        object: testRunner
        arguments:
          client: *client
          event:
            poolReadyEvent: {}
          count: 1

      - name: waitForEvent
        object: testRunner
        arguments:
          client: *client
          event:
            connectionClosedEvent: {}
          count: 1

      - name: assertEventCount
        object: testRunner
        arguments:
          client: *client
          event:
            poolClearedEvent: {}
          count: 0
