---
description: minPoolSize-error

schemaVersion: "1.4"

runOnRequirements:
  # Require SERVER-49336 for failCommand + appName on the initial handshake.
  - minServerVersion: "4.4.7"
    serverless: forbid
    topologies:
      - single

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

initialData: &initialData
  - collectionName: &collectionName sdam-minPoolSize-error
    databaseName: &databaseName sdam-tests
    documents: []

tests:
  - description: Server error on minPoolSize background creation
    operations:
      # Configure the initial monitor handshake to succeed but the
      # first or second background minPoolSize establishments to fail.
      - name: failPoint
        object: testRunner
        arguments:
          client: *setupClient
          failPoint:
            configureFailPoint: failCommand
            mode:
              skip: 3
            data:
              failCommands:
                - hello
                - isMaster
              appName: SDAMminPoolSizeError
              errorCode: 91
      - name: createEntities
        object: testRunner
        arguments:
          entities:
            - client:
                id: &client client
                useMultipleMongoses: false
                observeEvents:
                  - serverDescriptionChangedEvent
                  - poolClearedEvent
                  - poolReadyEvent
                uriOptions:
                  heartbeatFrequencyMS: 10000
                  appname: SDAMminPoolSizeError
                  minPoolSize: 10
                  serverMonitoringMode: poll
                  serverSelectionTimeoutMS: 1000
            - database:
                id: &database database
                client: *client
                databaseName: *databaseName
            - collection:
                id: &collection collection
                database: *database
                collectionName: *collectionName
      # Wait for monitor to succeed handshake and mark the pool as ready.
      - name: waitForEvent
        object: testRunner
        arguments:
          client: *client
          event:
            poolReadyEvent: {}
          count: 1
      # Background connection establishment ensuring minPoolSize should fail,
      # causing the pool to be cleared.
      - name: waitForEvent
        object: testRunner
        arguments:
          client: *client
          event:
            poolClearedEvent: {}
          count: 1
      # The server should be marked as Unknown as part of this.
      - name: waitForEvent
        object: testRunner
        arguments:
          client: *client
          event:
            serverDescriptionChangedEvent:
              newDescription:
                type: Unknown
          count: 1
      # Executing a command should fail server selection due to not being able
      # to find the primary.
      - name: runCommand
        object: *database
        arguments:
          command:
            ping: {}
          commandName: ping
        expectError:
          isError: true
      # Disable the failpoint, allowing the monitor to discover the primary again.
      - name: failPoint
        object: testRunner
        arguments:
          failPoint:
            configureFailPoint: failCommand
            mode: "off"
          client: *setupClient
      # Perform an operation to ensure the node is discovered.
      - name: runCommand
        object: *database
        arguments:
          command:
            ping: 1
          commandName: ping
      # Assert that the monitor discovered the primary and mark the pool as ready again.
      - name: assertEventCount
        object: testRunner
        arguments:
          client: *client
          event:
            poolReadyEvent: {}
          count: 2
