{
  "description": "cursors are correctly pinned to connections for load-balanced clusters",
  "schemaVersion": "1.4",
  "runOnRequirements": [
    {
      "topologies": [
        "load-balanced"
      ]
    }
  ],
  "createEntities": [
    {
      "client": {
        "id": "client0",
        "useMultipleMongoses": true,
        "observeEvents": [
          "commandStartedEvent",
          "commandSucceededEvent",
          "commandFailedEvent",
          "connectionReadyEvent",
          "connectionClosedEvent",
          "connectionCheckedOutEvent",
          "connectionCheckedInEvent"
        ]
      }
    },
    {
      "database": {
        "id": "database0",
        "client": "client0",
        "databaseName": "database0Name"
      }
    },
    {
      "collection": {
        "id": "collection0",
        "database": "database0",
        "collectionName": "coll0"
      }
    },
    {
      "collection": {
        "id": "collection1",
        "database": "database0",
        "collectionName": "coll1"
      }
    },
    {
      "collection": {
        "id": "collection2",
        "database": "database0",
        "collectionName": "coll2"
      }
    }
  ],
  "initialData": [
    {
      "collectionName": "coll0",
      "databaseName": "database0Name",
      "documents": [
        {
          "_id": 1
        },
        {
          "_id": 2
        },
        {
          "_id": 3
        }
      ]
    },
    {
      "collectionName": "coll1",
      "databaseName": "database0Name",
      "documents": []
    },
    {
      "collectionName": "coll2",
      "databaseName": "database0Name",
      "documents": []
    }
  ],
  "tests": [
    {
      "description": "no connection is pinned if all documents are returned in the initial batch",
      "operations": [
        {
          "name": "createFindCursor",
          "object": "collection0",
          "arguments": {
            "filter": {}
          },
          "saveResultAsEntity": "cursor0"
        },
        {
          "name": "assertNumberConnectionsCheckedOut",
          "object": "testRunner",
          "arguments": {
            "client": "client0",
            "connections": 0
          }
        }
      ],
      "expectEvents": [
        {
          "client": "client0",
          "events": [
            {
              "commandStartedEvent": {
                "command": {
                  "find": "coll0",
                  "filter": {}
                },
                "commandName": "find"
              }
            },
            {
              "commandSucceededEvent": {
                "reply": {
                  "cursor": {
                    "id": 0,
                    "firstBatch": {
                      "$$type": "array"
                    },
                    "ns": {
                      "$$type": "string"
                    }
                  }
                },
                "commandName": "find"
              }
            }
          ]
        },
        {
          "client": "client0",
          "eventType": "cmap",
          "events": [
            {
              "connectionReadyEvent": {}
            },
            {
              "connectionCheckedOutEvent": {}
            },
            {
              "connectionCheckedInEvent": {}
            }
          ]
        }
      ]
    },
    {
      "description": "pinned connections are returned when the cursor is drained",
      "operations": [
        {
          "name": "createFindCursor",
          "object": "collection0",
          "arguments": {
            "filter": {},
            "batchSize": 2
          },
          "saveResultAsEntity": "cursor0"
        },
        {
          "name": "assertNumberConnectionsCheckedOut",
          "object": "testRunner",
          "arguments": {
            "client": "client0",
            "connections": 1
          }
        },
        {
          "name": "iterateUntilDocumentOrError",
          "object": "cursor0",
          "expectResult": {
            "_id": 1
          }
        },
        {
          "name": "iterateUntilDocumentOrError",
          "object": "cursor0",
          "expectResult": {
            "_id": 2
          }
        },
        {
          "name": "iterateUntilDocumentOrError",
          "object": "cursor0",
          "expectResult": {
            "_id": 3
          }
        },
        {
          "name": "assertNumberConnectionsCheckedOut",
          "object": "testRunner",
          "arguments": {
            "client": "client0",
            "connections": 0
          }
        },
        {
          "name": "close",
          "object": "cursor0"
        }
      ],
      "expectEvents": [
        {
          "client": "client0",
          "events": [
            {
              "commandStartedEvent": {
                "command": {
                  "find": "coll0",
                  "filter": {},
                  "batchSize": 2
                },
                "commandName": "find"
              }
            },
            {
              "commandSucceededEvent": {
                "reply": {
                  "cursor": {
                    "id": {
                      "$$type": "long"
                    },
                    "firstBatch": {
                      "$$type": "array"
                    },
                    "ns": {
                      "$$type": "string"
                    }
                  }
                },
                "commandName": "find"
              }
            },
            {
              "commandStartedEvent": {
                "command": {
                  "getMore": {
                    "$$type": "long"
                  },
                  "collection": "coll0"
                },
                "commandName": "getMore"
              }
            },
            {
              "commandSucceededEvent": {
                "reply": {
                  "cursor": {
                    "id": 0,
                    "ns": {
                      "$$type": "string"
                    },
                    "nextBatch": {
                      "$$type": "array"
                    }
                  }
                },
                "commandName": "getMore"
              }
            }
          ]
        },
        {
          "client": "client0",
          "eventType": "cmap",
          "events": [
            {
              "connectionReadyEvent": {}
            },
            {
              "connectionCheckedOutEvent": {}
            },
            {
              "connectionCheckedInEvent": {}
            }
          ]
        }
      ]
    },
    {
      "description": "pinned connections are returned to the pool when the cursor is closed",
      "operations": [
        {
          "name": "createFindCursor",
          "object": "collection0",
          "arguments": {
            "filter": {},
            "batchSize": 2
          },
          "saveResultAsEntity": "cursor0"
        },
        {
          "name": "assertNumberConnectionsCheckedOut",
          "object": "testRunner",
          "arguments": {
            "client": "client0",
            "connections": 1
          }
        },
        {
          "name": "close",
          "object": "cursor0"
        },
        {
          "name": "assertNumberConnectionsCheckedOut",
          "object": "testRunner",
          "arguments": {
            "client": "client0",
            "connections": 0
          }
        }
      ],
      "expectEvents": [
        {
          "client": "client0",
          "events": [
            {
              "commandStartedEvent": {
                "command": {
                  "find": "coll0",
                  "filter": {},
                  "batchSize": 2
                },
                "commandName": "find"
              }
            },
            {
              "commandSucceededEvent": {
                "reply": {
                  "cursor": {
                    "id": {
                      "$$type": "long"
                    },
                    "firstBatch": {
                      "$$type": "array"
                    },
                    "ns": {
                      "$$type": "string"
                    }
                  }
                },
                "commandName": "find"
              }
            },
            {
              "commandStartedEvent": {
                "commandName": "killCursors"
              }
            },
            {
              "commandSucceededEvent": {
                "commandName": "killCursors"
              }
            }
          ]
        },
        {
          "client": "client0",
          "eventType": "cmap",
          "events": [
            {
              "connectionReadyEvent": {}
            },
            {
              "connectionCheckedOutEvent": {}
            },
            {
              "connectionCheckedInEvent": {}
            }
          ]
        }
      ]
    },
    {
      "description": "pinned connections are not returned after an network error during getMore",
      "operations": [
        {
          "name": "failPoint",
          "object": "testRunner",
          "arguments": {
            "client": "client0",
            "failPoint": {
              "configureFailPoint": "failCommand",
              "mode": {
                "times": 1
              },
              "data": {
                "failCommands": [
                  "getMore"
                ],
                "closeConnection": true
              }
            }
          }
        },
        {
          "name": "createFindCursor",
          "object": "collection0",
          "arguments": {
            "filter": {},
            "batchSize": 2
          },
          "saveResultAsEntity": "cursor0"
        },
        {
          "name": "assertNumberConnectionsCheckedOut",
          "object": "testRunner",
          "arguments": {
            "client": "client0",
            "connections": 1
          }
        },
        {
          "name": "iterateUntilDocumentOrError",
          "object": "cursor0",
          "expectResult": {
            "_id": 1
          }
        },
        {
          "name": "iterateUntilDocumentOrError",
          "object": "cursor0",
          "expectResult": {
            "_id": 2
          }
        },
        {
          "name": "iterateUntilDocumentOrError",
          "object": "cursor0",
          "expectError": {
            "isClientError": true
          }
        },
        {
          "name": "assertNumberConnectionsCheckedOut",
          "object": "testRunner",
          "arguments": {
            "client": "client0",
            "connections": 1
          }
        },
        {
          "name": "close",
          "object": "cursor0"
        },
        {
          "name": "assertNumberConnectionsCheckedOut",
          "object": "testRunner",
          "arguments": {
            "client": "client0",
            "connections": 0
          }
        }
      ],
      "expectEvents": [
        {
          "client": "client0",
          "events": [
            {
              "commandStartedEvent": {
                "command": {
                  "find": "coll0",
                  "filter": {},
                  "batchSize": 2
                },
                "commandName": "find"
              }
            },
            {
              "commandSucceededEvent": {
                "reply": {
                  "cursor": {
                    "id": {
                      "$$type": "long"
                    },
                    "firstBatch": {
                      "$$type": "array"
                    },
                    "ns": {
                      "$$type": "string"
                    }
                  }
                },
                "commandName": "find"
              }
            },
            {
              "commandStartedEvent": {
                "command": {
                  "getMore": {
                    "$$type": "long"
                  },
                  "collection": "coll0"
                },
                "commandName": "getMore"
              }
            },
            {
              "commandFailedEvent": {
                "commandName": "getMore"
              }
            }
          ]
        },
        {
          "client": "client0",
          "eventType": "cmap",
          "events": [
            {
              "connectionReadyEvent": {}
            },
            {
              "connectionCheckedOutEvent": {}
            },
            {
              "connectionCheckedInEvent": {}
            },
            {
              "connectionCheckedOutEvent": {}
            },
            {
              "connectionCheckedInEvent": {}
            },
            {
              "connectionClosedEvent": {
                "reason": "error"
              }
            }
          ]
        }
      ]
    },
    {
      "description": "pinned connections are returned after a network error during a killCursors request",
      "operations": [
        {
          "name": "failPoint",
          "object": "testRunner",
          "arguments": {
            "client": "client0",
            "failPoint": {
              "configureFailPoint": "failCommand",
              "mode": {
                "times": 1
              },
              "data": {
                "failCommands": [
                  "killCursors"
                ],
                "closeConnection": true
              }
            }
          }
        },
        {
          "name": "createFindCursor",
          "object": "collection0",
          "arguments": {
            "filter": {},
            "batchSize": 2
          },
          "saveResultAsEntity": "cursor0"
        },
        {
          "name": "assertNumberConnectionsCheckedOut",
          "object": "testRunner",
          "arguments": {
            "client": "client0",
            "connections": 1
          }
        },
        {
          "name": "close",
          "object": "cursor0"
        },
        {
          "name": "assertNumberConnectionsCheckedOut",
          "object": "testRunner",
          "arguments": {
            "client": "client0",
            "connections": 0
          }
        }
      ],
      "expectEvents": [
        {
          "client": "client0",
          "events": [
            {
              "commandStartedEvent": {
                "command": {
                  "find": "coll0",
                  "filter": {},
                  "batchSize": 2
                },
                "commandName": "find"
              }
            },
            {
              "commandSucceededEvent": {
                "reply": {
                  "cursor": {
                    "id": {
                      "$$type": "long"
                    },
                    "firstBatch": {
                      "$$type": "array"
                    },
                    "ns": {
                      "$$type": "string"
                    }
                  }
                },
                "commandName": "find"
              }
            },
            {
              "commandStartedEvent": {
                "commandName": "killCursors"
              }
            },
            {
              "commandFailedEvent": {
                "commandName": "killCursors"
              }
            }
          ]
        },
        {
          "client": "client0",
          "eventType": "cmap",
          "events": [
            {
              "connectionReadyEvent": {}
            },
            {
              "connectionCheckedOutEvent": {}
            },
            {
              "connectionCheckedInEvent": {}
            },
            {
              "connectionCheckedOutEvent": {}
            },
            {
              "connectionCheckedInEvent": {}
            },
            {
              "connectionClosedEvent": {
                "reason": "error"
              }
            }
          ]
        }
      ]
    },
    {
      "description": "pinned connections are not returned to the pool after a non-network error on getMore",
      "operations": [
        {
          "name": "failPoint",
          "object": "testRunner",
          "arguments": {
            "client": "client0",
            "failPoint": {
              "configureFailPoint": "failCommand",
              "mode": {
                "times": 1
              },
              "data": {
                "failCommands": [
                  "getMore"
                ],
                "errorCode": 7
              }
            }
          }
        },
        {
          "name": "createFindCursor",
          "object": "collection0",
          "arguments": {
            "filter": {},
            "batchSize": 2
          },
          "saveResultAsEntity": "cursor0"
        },
        {
          "name": "iterateUntilDocumentOrError",
          "object": "cursor0",
          "expectResult": {
            "_id": 1
          }
        },
        {
          "name": "iterateUntilDocumentOrError",
          "object": "cursor0",
          "expectResult": {
            "_id": 2
          }
        },
        {
          "name": "iterateUntilDocumentOrError",
          "object": "cursor0",
          "expectError": {
            "errorCode": 7
          }
        },
        {
          "name": "assertNumberConnectionsCheckedOut",
          "object": "testRunner",
          "arguments": {
            "client": "client0",
            "connections": 1
          }
        },
        {
          "name": "close",
          "object": "cursor0"
        },
        {
          "name": "assertNumberConnectionsCheckedOut",
          "object": "testRunner",
          "arguments": {
            "client": "client0",
            "connections": 0
          }
        }
      ],
      "expectEvents": [
        {
          "client": "client0",
          "events": [
            {
              "commandStartedEvent": {
                "command": {
                  "find": "coll0",
                  "filter": {},
                  "batchSize": 2
                },
                "commandName": "find"
              }
            },
            {
              "commandSucceededEvent": {
                "reply": {
                  "cursor": {
                    "id": {
                      "$$type": "long"
                    },
                    "firstBatch": {
                      "$$type": "array"
                    },
                    "ns": {
                      "$$type": "string"
                    }
                  }
                },
                "commandName": "find"
              }
            },
            {
              "commandStartedEvent": {
                "command": {
                  "getMore": {
                    "$$type": "long"
                  },
                  "collection": "coll0"
                },
                "commandName": "getMore"
              }
            },
            {
              "commandFailedEvent": {
                "commandName": "getMore"
              }
            },
            {
              "commandStartedEvent": {
                "commandName": "killCursors"
              }
            },
            {
              "commandSucceededEvent": {
                "commandName": "killCursors"
              }
            }
          ]
        },
        {
          "client": "client0",
          "eventType": "cmap",
          "events": [
            {
              "connectionReadyEvent": {}
            },
            {
              "connectionCheckedOutEvent": {}
            },
            {
              "connectionCheckedInEvent": {}
            },
            {
              "connectionCheckedOutEvent": {}
            },
            {
              "connectionCheckedInEvent": {}
            }
          ]
        }
      ]
    },
    {
      "description": "aggregate pins the cursor to a connection",
      "operations": [
        {
          "name": "aggregate",
          "object": "collection0",
          "arguments": {
            "pipeline": [],
            "batchSize": 2
          }
        },
        {
          "name": "assertNumberConnectionsCheckedOut",
          "object": "testRunner",
          "arguments": {
            "client": "client0",
            "connections": 0
          }
        }
      ],
      "expectEvents": [
        {
          "client": "client0",
          "events": [
            {
              "commandStartedEvent": {
                "command": {
                  "aggregate": "coll0",
                  "cursor": {
                    "batchSize": 2
                  }
                },
                "commandName": "aggregate"
              }
            },
            {
              "commandSucceededEvent": {
                "commandName": "aggregate"
              }
            },
            {
              "commandStartedEvent": {
                "command": {
                  "getMore": {
                    "$$type": "long"
                  },
                  "collection": "coll0"
                },
                "commandName": "getMore"
              }
            },
            {
              "commandSucceededEvent": {
                "reply": {
                  "cursor": {
                    "id": 0,
                    "ns": {
                      "$$type": "string"
                    },
                    "nextBatch": {
                      "$$type": "array"
                    }
                  }
                },
                "commandName": "getMore"
              }
            }
          ]
        },
        {
          "client": "client0",
          "eventType": "cmap",
          "events": [
            {
              "connectionReadyEvent": {}
            },
            {
              "connectionCheckedOutEvent": {}
            },
            {
              "connectionCheckedInEvent": {}
            }
          ]
        }
      ]
    },
    {
      "description": "listCollections pins the cursor to a connection",
      "runOnRequirements": [
        {
          "serverless": "forbid"
        }
      ],
      "operations": [
        {
          "name": "listCollections",
          "object": "database0",
          "arguments": {
            "filter": {},
            "batchSize": 2
          }
        },
        {
          "name": "assertNumberConnectionsCheckedOut",
          "object": "testRunner",
          "arguments": {
            "client": "client0",
            "connections": 0
          }
        }
      ],
      "expectEvents": [
        {
          "client": "client0",
          "events": [
            {
              "commandStartedEvent": {
                "command": {
                  "listCollections": 1,
                  "cursor": {
                    "batchSize": 2
                  }
                },
                "commandName": "listCollections",
                "databaseName": "database0Name"
              }
            },
            {
              "commandSucceededEvent": {
                "commandName": "listCollections"
              }
            },
            {
              "commandStartedEvent": {
                "command": {
                  "getMore": {
                    "$$type": "long"
                  },
                  "collection": {
                    "$$type": "string"
                  }
                },
                "commandName": "getMore"
              }
            },
            {
              "commandSucceededEvent": {
                "reply": {
                  "cursor": {
                    "id": 0,
                    "ns": {
                      "$$type": "string"
                    },
                    "nextBatch": {
                      "$$type": "array"
                    }
                  }
                },
                "commandName": "getMore"
              }
            }
          ]
        },
        {
          "client": "client0",
          "eventType": "cmap",
          "events": [
            {
              "connectionReadyEvent": {}
            },
            {
              "connectionCheckedOutEvent": {}
            },
            {
              "connectionCheckedInEvent": {}
            }
          ]
        }
      ]
    },
    {
      "description": "listIndexes pins the cursor to a connection",
      "operations": [
        {
          "name": "createIndex",
          "object": "collection0",
          "arguments": {
            "keys": {
              "x": 1
            },
            "name": "x_1"
          }
        },
        {
          "name": "createIndex",
          "object": "collection0",
          "arguments": {
            "keys": {
              "y": 1
            },
            "name": "y_1"
          }
        },
        {
          "name": "listIndexes",
          "object": "collection0",
          "arguments": {
            "batchSize": 2
          }
        },
        {
          "name": "assertNumberConnectionsCheckedOut",
          "object": "testRunner",
          "arguments": {
            "client": "client0",
            "connections": 0
          }
        }
      ],
      "expectEvents": [
        {
          "client": "client0",
          "events": [
            {
              "commandStartedEvent": {
                "command": {
                  "createIndexes": "coll0",
                  "indexes": [
                    {
                      "name": "x_1",
                      "key": {
                        "x": 1
                      }
                    }
                  ]
                },
                "commandName": "createIndexes"
              }
            },
            {
              "commandSucceededEvent": {
                "commandName": "createIndexes"
              }
            },
            {
              "commandStartedEvent": {
                "command": {
                  "createIndexes": "coll0",
                  "indexes": [
                    {
                      "name": "y_1",
                      "key": {
                        "y": 1
                      }
                    }
                  ]
                },
                "commandName": "createIndexes"
              }
            },
            {
              "commandSucceededEvent": {
                "commandName": "createIndexes"
              }
            },
            {
              "commandStartedEvent": {
                "command": {
                  "listIndexes": "coll0",
                  "cursor": {
                    "batchSize": 2
                  }
                },
                "commandName": "listIndexes",
                "databaseName": "database0Name"
              }
            },
            {
              "commandSucceededEvent": {
                "commandName": "listIndexes"
              }
            },
            {
              "commandStartedEvent": {
                "command": {
                  "getMore": {
                    "$$type": "long"
                  },
                  "collection": "coll0"
                },
                "commandName": "getMore"
              }
            },
            {
              "commandSucceededEvent": {
                "reply": {
                  "cursor": {
                    "id": 0,
                    "ns": {
                      "$$type": "string"
                    },
                    "nextBatch": {
                      "$$type": "array"
                    }
                  }
                },
                "commandName": "getMore"
              }
            }
          ]
        },
        {
          "client": "client0",
          "eventType": "cmap",
          "events": [
            {
              "connectionReadyEvent": {}
            },
            {
              "connectionCheckedOutEvent": {}
            },
            {
              "connectionCheckedInEvent": {}
            },
            {
              "connectionCheckedOutEvent": {}
            },
            {
              "connectionCheckedInEvent": {}
            },
            {
              "connectionCheckedOutEvent": {}
            },
            {
              "connectionCheckedInEvent": {}
            }
          ]
        }
      ]
    },
    {
      "description": "change streams pin to a connection",
      "runOnRequirements": [
        {
          "serverless": "forbid"
        }
      ],
      "operations": [
        {
          "name": "createChangeStream",
          "object": "collection0",
          "arguments": {
            "pipeline": []
          },
          "saveResultAsEntity": "changeStream0"
        },
        {
          "name": "assertNumberConnectionsCheckedOut",
          "object": "testRunner",
          "arguments": {
            "client": "client0",
            "connections": 1
          }
        },
        {
          "name": "close",
          "object": "changeStream0"
        },
        {
          "name": "assertNumberConnectionsCheckedOut",
          "object": "testRunner",
          "arguments": {
            "client": "client0",
            "connections": 0
          }
        }
      ],
      "expectEvents": [
        {
          "client": "client0",
          "events": [
            {
              "commandStartedEvent": {
                "commandName": "aggregate"
              }
            },
            {
              "commandSucceededEvent": {
                "commandName": "aggregate"
              }
            },
            {
              "commandStartedEvent": {
                "commandName": "killCursors"
              }
            },
            {
              "commandSucceededEvent": {
                "commandName": "killCursors"
              }
            }
          ]
        },
        {
          "client": "client0",
          "eventType": "cmap",
          "events": [
            {
              "connectionReadyEvent": {}
            },
            {
              "connectionCheckedOutEvent": {}
            },
            {
              "connectionCheckedInEvent": {}
            }
          ]
        }
      ]
    }
  ]
}
