{
  "description": "pool-clear-on-error-checkout",
  "schemaVersion": "1.4",
  "runOnRequirements": [
    {
      "minServerVersion": "4.4",
      "serverless": "forbid",
      "topologies": [
        "single",
        "replicaset",
        "sharded"
      ]
    }
  ],
  "createEntities": [
    {
      "client": {
        "id": "setupClient",
        "useMultipleMongoses": false
      }
    }
  ],
  "tests": [
    {
      "description": "Pool is cleared before connection is closed (authentication error)",
      "runOnRequirements": [
        {
          "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",
                  "useMultipleMongoses": false,
                  "observeEvents": [
                    "connectionCheckOutStartedEvent",
                    "poolClearedEvent",
                    "connectionClosedEvent"
                  ],
                  "uriOptions": {
                    "retryWrites": false,
                    "appname": "authErrorTest"
                  }
                }
              },
              {
                "database": {
                  "id": "database",
                  "client": "client",
                  "databaseName": "foo"
                }
              },
              {
                "collection": {
                  "id": "collection",
                  "database": "database",
                  "collectionName": "bar"
                }
              }
            ]
          }
        },
        {
          "name": "insertMany",
          "object": "collection",
          "arguments": {
            "documents": [
              {
                "_id": 3
              },
              {
                "_id": 4
              }
            ]
          },
          "expectError": {
            "isError": true
          }
        },
        {
          "name": "waitForEvent",
          "object": "testRunner",
          "arguments": {
            "client": "client",
            "event": {
              "poolClearedEvent": {}
            },
            "count": 1
          }
        },
        {
          "name": "waitForEvent",
          "object": "testRunner",
          "arguments": {
            "client": "client",
            "event": {
              "connectionClosedEvent": {}
            },
            "count": 1
          }
        }
      ],
      "expectEvents": [
        {
          "client": "client",
          "eventType": "cmap",
          "events": [
            {
              "connectionCheckOutStartedEvent": {}
            },
            {
              "poolClearedEvent": {}
            },
            {
              "connectionClosedEvent": {}
            }
          ]
        }
      ]
    },
    {
      "description": "Pool is cleared before connection is closed (handshake error)",
      "runOnRequirements": [
        {
          "topologies": [
            "single"
          ]
        }
      ],
      "operations": [
        {
          "name": "createEntities",
          "object": "testRunner",
          "arguments": {
            "entities": [
              {
                "client": {
                  "id": "client",
                  "useMultipleMongoses": false,
                  "observeEvents": [
                    "connectionCheckOutStartedEvent",
                    "poolClearedEvent",
                    "connectionClosedEvent",
                    "topologyDescriptionChangedEvent"
                  ],
                  "uriOptions": {
                    "retryWrites": false,
                    "appname": "authErrorTest",
                    "minPoolSize": 0,
                    "serverMonitoringMode": "poll",
                    "heartbeatFrequencyMS": 1000000
                  }
                }
              },
              {
                "database": {
                  "id": "database",
                  "client": "client",
                  "databaseName": "foo"
                }
              },
              {
                "collection": {
                  "id": "collection",
                  "database": "database",
                  "collectionName": "bar"
                }
              }
            ]
          }
        },
        {
          "name": "waitForEvent",
          "object": "testRunner",
          "arguments": {
            "client": "client",
            "event": {
              "topologyDescriptionChangedEvent": {
                "previousDescription": {
                  "type": "Unknown"
                },
                "newDescription": {
                  "type": "Single"
                }
              }
            },
            "count": 1
          }
        },
        {
          "name": "failPoint",
          "object": "testRunner",
          "arguments": {
            "client": "setupClient",
            "failPoint": {
              "configureFailPoint": "failCommand",
              "mode": {
                "times": 1
              },
              "data": {
                "failCommands": [
                  "hello",
                  "isMaster"
                ],
                "appName": "authErrorTest",
                "closeConnection": true
              }
            }
          }
        },
        {
          "name": "insertMany",
          "object": "collection",
          "arguments": {
            "documents": [
              {
                "_id": 3
              },
              {
                "_id": 4
              }
            ]
          },
          "expectError": {
            "isError": true
          }
        },
        {
          "name": "waitForEvent",
          "object": "testRunner",
          "arguments": {
            "client": "client",
            "event": {
              "poolClearedEvent": {}
            },
            "count": 1
          }
        },
        {
          "name": "waitForEvent",
          "object": "testRunner",
          "arguments": {
            "client": "client",
            "event": {
              "connectionClosedEvent": {}
            },
            "count": 1
          }
        }
      ],
      "expectEvents": [
        {
          "client": "client",
          "eventType": "cmap",
          "events": [
            {
              "connectionCheckOutStartedEvent": {}
            },
            {
              "poolClearedEvent": {}
            },
            {
              "connectionClosedEvent": {}
            }
          ]
        }
      ]
    }
  ]
}
