---
description: insert-shutdown-error

schemaVersion: "1.10"

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

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

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

tests:
  - description: Concurrent shutdown error on insert
    operations:
      - name: createEntities
        object: testRunner
        arguments:
          entities:
            - client:
                id: &client client
                useMultipleMongoses: false
                uriOptions:
                  retryWrites: false
                  heartbeatFrequencyMS: 500
                  appname: shutdownErrorInsertTest
                observeEvents:
                  - serverDescriptionChangedEvent
                  - poolClearedEvent
            - database:
                id: &database database
                client: *client
                databaseName: *databaseName
            - collection:
                id: &collection collection
                database: *database
                collectionName: *collectionName
      # Perform an operation to ensure the node is discovered.
      - name: insertOne
        object: *collection
        arguments:
          document:
            _id: 1
      # Configure the next two inserts to fail with a non-timeout shutdown
      # errors. Block the connection for 500ms to ensure both operations check
      # out connections from the same pool generation.
      - name: failPoint
        object: testRunner
        arguments:
          failPoint:
            configureFailPoint: failCommand
            mode:
              times: 2
            data:
              failCommands:
                - insert
              appName: shutdownErrorInsertTest
              errorCode: 91
              blockConnection: true
              blockTimeMS: 500
          client: *setupClient
      # Start threads.
      - name: createEntities
        object: testRunner
        arguments:
          entities:
            - thread:
                id: &thread0 thread0
            - thread:
                id: &thread1 thread1
      # Perform concurrent insert operations. Both fail with shutdown errors.
      - name: runOnThread
        object: testRunner
        arguments:
          thread: *thread0
          operation:
            name: insertOne
            object: *collection
            arguments:
              document:
                _id: 2
            expectError:
              isError: true
      - name: runOnThread
        object: testRunner
        arguments:
          thread: *thread1
          operation:
            name: insertOne
            object: *collection
            arguments:
              document:
                _id: 3
            expectError:
              isError: true
      # Stop threads.
      - name: waitForThread
        object: testRunner
        arguments:
          thread: *thread0
      - name: waitForThread
        object: testRunner
        arguments:
          thread: *thread1
      # The first shutdown error should mark the server Unknown and then clear
      # the pool.
      - name: waitForEvent
        object: testRunner
        arguments:
          client: *client
          event:
            serverDescriptionChangedEvent:
              newDescription:
                type: Unknown
          count: 1
      - name: waitForEvent
        object: testRunner
        arguments:
          client: *client
          event:
            poolClearedEvent: {}
          count: 1
      # Perform an operation to ensure the node is rediscovered.
      - name: insertOne
        object: *collection
        arguments:
          document:
            _id: 4
      # Assert the server was marked Unknown and pool was cleared exactly once.
      - name: assertEventCount
        object: testRunner
        arguments:
          client: *client
          event:
            serverDescriptionChangedEvent:
              newDescription:
                type: Unknown
          count: 1
      - name: assertEventCount
        object: testRunner
        arguments:
          client: *client
          event:
            poolClearedEvent: {}
          count: 1

    # Order of operations is non-deterministic so we cannot check events.
    outcome:
      - collectionName: *collectionName
        databaseName: *databaseName
        documents:
          - _id: 1
          - _id: 4
