{
  "description": "runCursorCommand",
  "schemaVersion": "1.9",
  "createEntities": [
    {
      "client": {
        "id": "client",
        "useMultipleMongoses": false,
        "observeEvents": [
          "commandStartedEvent",
          "connectionReadyEvent",
          "connectionCheckedOutEvent",
          "connectionCheckedInEvent"
        ]
      }
    },
    {
      "session": {
        "id": "session",
        "client": "client"
      }
    },
    {
      "database": {
        "id": "db",
        "client": "client",
        "databaseName": "db"
      }
    },
    {
      "collection": {
        "id": "collection",
        "database": "db",
        "collectionName": "collection"
      }
    }
  ],
  "initialData": [
    {
      "collectionName": "collection",
      "databaseName": "db",
      "documents": [
        {
          "_id": 1,
          "x": 11
        },
        {
          "_id": 2,
          "x": 22
        },
        {
          "_id": 3,
          "x": 33
        },
        {
          "_id": 4,
          "x": 44
        },
        {
          "_id": 5,
          "x": 55
        }
      ]
    }
  ],
  "tests": [
    {
      "description": "successfully executes checkMetadataConsistency cursor creating command",
      "runOnRequirements": [
        {
          "minServerVersion": "7.0",
          "topologies": [
            "sharded"
          ]
        }
      ],
      "operations": [
        {
          "name": "runCursorCommand",
          "object": "db",
          "arguments": {
            "commandName": "checkMetadataConsistency",
            "command": {
              "checkMetadataConsistency": 1
            }
          }
        }
      ],
      "expectEvents": [
        {
          "client": "client",
          "eventType": "command",
          "events": [
            {
              "commandStartedEvent": {
                "command": {
                  "checkMetadataConsistency": 1,
                  "$db": "db",
                  "lsid": {
                    "$$exists": true
                  }
                },
                "commandName": "checkMetadataConsistency"
              }
            }
          ]
        }
      ]
    },
    {
      "description": "errors if the command response is not a cursor",
      "operations": [
        {
          "name": "createCommandCursor",
          "object": "db",
          "arguments": {
            "commandName": "ping",
            "command": {
              "ping": 1
            }
          },
          "expectError": {
            "isClientError": true
          }
        }
      ]
    },
    {
      "description": "creates an implicit session that is reused across getMores",
      "operations": [
        {
          "name": "runCursorCommand",
          "object": "db",
          "arguments": {
            "commandName": "find",
            "command": {
              "find": "collection",
              "batchSize": 2
            }
          },
          "expectResult": [
            {
              "_id": 1,
              "x": 11
            },
            {
              "_id": 2,
              "x": 22
            },
            {
              "_id": 3,
              "x": 33
            },
            {
              "_id": 4,
              "x": 44
            },
            {
              "_id": 5,
              "x": 55
            }
          ]
        },
        {
          "name": "assertSameLsidOnLastTwoCommands",
          "object": "testRunner",
          "arguments": {
            "client": "client"
          }
        }
      ],
      "expectEvents": [
        {
          "client": "client",
          "eventType": "command",
          "events": [
            {
              "commandStartedEvent": {
                "command": {
                  "find": "collection",
                  "batchSize": 2,
                  "$db": "db",
                  "lsid": {
                    "$$exists": true
                  }
                },
                "commandName": "find"
              }
            },
            {
              "commandStartedEvent": {
                "command": {
                  "getMore": {
                    "$$type": [
                      "int",
                      "long"
                    ]
                  },
                  "collection": "collection",
                  "$db": "db",
                  "lsid": {
                    "$$exists": true
                  }
                },
                "commandName": "getMore"
              }
            }
          ]
        }
      ]
    },
    {
      "description": "accepts an explicit session that is reused across getMores",
      "operations": [
        {
          "name": "runCursorCommand",
          "object": "db",
          "arguments": {
            "commandName": "find",
            "session": "session",
            "command": {
              "find": "collection",
              "batchSize": 2
            }
          },
          "expectResult": [
            {
              "_id": 1,
              "x": 11
            },
            {
              "_id": 2,
              "x": 22
            },
            {
              "_id": 3,
              "x": 33
            },
            {
              "_id": 4,
              "x": 44
            },
            {
              "_id": 5,
              "x": 55
            }
          ]
        },
        {
          "name": "assertSameLsidOnLastTwoCommands",
          "object": "testRunner",
          "arguments": {
            "client": "client"
          }
        }
      ],
      "expectEvents": [
        {
          "client": "client",
          "eventType": "command",
          "events": [
            {
              "commandStartedEvent": {
                "command": {
                  "find": "collection",
                  "batchSize": 2,
                  "$db": "db",
                  "lsid": {
                    "$$sessionLsid": "session"
                  }
                },
                "commandName": "find"
              }
            },
            {
              "commandStartedEvent": {
                "command": {
                  "getMore": {
                    "$$type": [
                      "int",
                      "long"
                    ]
                  },
                  "collection": "collection",
                  "$db": "db",
                  "lsid": {
                    "$$sessionLsid": "session"
                  }
                },
                "commandName": "getMore"
              }
            }
          ]
        }
      ]
    },
    {
      "description": "returns pinned connections to the pool when the cursor is exhausted",
      "runOnRequirements": [
        {
          "topologies": [
            "load-balanced"
          ]
        }
      ],
      "operations": [
        {
          "name": "createCommandCursor",
          "object": "db",
          "arguments": {
            "commandName": "find",
            "batchSize": 2,
            "session": "session",
            "command": {
              "find": "collection",
              "batchSize": 2
            }
          },
          "saveResultAsEntity": "cursor"
        },
        {
          "name": "assertNumberConnectionsCheckedOut",
          "object": "testRunner",
          "arguments": {
            "client": "client",
            "connections": 1
          }
        },
        {
          "name": "iterateUntilDocumentOrError",
          "object": "cursor",
          "expectResult": {
            "_id": 1,
            "x": 11
          }
        },
        {
          "name": "iterateUntilDocumentOrError",
          "object": "cursor",
          "expectResult": {
            "_id": 2,
            "x": 22
          }
        },
        {
          "name": "iterateUntilDocumentOrError",
          "object": "cursor",
          "expectResult": {
            "_id": 3,
            "x": 33
          }
        },
        {
          "name": "iterateUntilDocumentOrError",
          "object": "cursor",
          "expectResult": {
            "_id": 4,
            "x": 44
          }
        },
        {
          "name": "iterateUntilDocumentOrError",
          "object": "cursor",
          "expectResult": {
            "_id": 5,
            "x": 55
          }
        },
        {
          "name": "assertNumberConnectionsCheckedOut",
          "object": "testRunner",
          "arguments": {
            "client": "client",
            "connections": 0
          }
        }
      ],
      "expectEvents": [
        {
          "client": "client",
          "eventType": "command",
          "events": [
            {
              "commandStartedEvent": {
                "command": {
                  "find": "collection",
                  "batchSize": 2,
                  "$db": "db",
                  "lsid": {
                    "$$sessionLsid": "session"
                  }
                },
                "commandName": "find"
              }
            },
            {
              "commandStartedEvent": {
                "command": {
                  "getMore": {
                    "$$type": [
                      "int",
                      "long"
                    ]
                  },
                  "collection": "collection",
                  "$db": "db",
                  "lsid": {
                    "$$sessionLsid": "session"
                  }
                },
                "commandName": "getMore"
              }
            },
            {
              "commandStartedEvent": {
                "command": {
                  "getMore": {
                    "$$type": [
                      "int",
                      "long"
                    ]
                  },
                  "collection": "collection",
                  "$db": "db",
                  "lsid": {
                    "$$sessionLsid": "session"
                  }
                },
                "commandName": "getMore"
              }
            }
          ]
        },
        {
          "client": "client",
          "eventType": "cmap",
          "events": [
            {
              "connectionReadyEvent": {}
            },
            {
              "connectionCheckedOutEvent": {}
            },
            {
              "connectionCheckedInEvent": {}
            }
          ]
        }
      ]
    },
    {
      "description": "returns pinned connections to the pool when the cursor is closed",
      "runOnRequirements": [
        {
          "topologies": [
            "load-balanced"
          ]
        }
      ],
      "operations": [
        {
          "name": "createCommandCursor",
          "object": "db",
          "arguments": {
            "commandName": "find",
            "command": {
              "find": "collection",
              "batchSize": 2
            }
          },
          "saveResultAsEntity": "cursor"
        },
        {
          "name": "assertNumberConnectionsCheckedOut",
          "object": "testRunner",
          "arguments": {
            "client": "client",
            "connections": 1
          }
        },
        {
          "name": "close",
          "object": "cursor"
        },
        {
          "name": "assertNumberConnectionsCheckedOut",
          "object": "testRunner",
          "arguments": {
            "client": "client",
            "connections": 0
          }
        }
      ]
    },
    {
      "description": "supports configuring getMore batchSize",
      "operations": [
        {
          "name": "runCursorCommand",
          "object": "db",
          "arguments": {
            "commandName": "find",
            "batchSize": 5,
            "command": {
              "find": "collection",
              "batchSize": 1
            }
          },
          "expectResult": [
            {
              "_id": 1,
              "x": 11
            },
            {
              "_id": 2,
              "x": 22
            },
            {
              "_id": 3,
              "x": 33
            },
            {
              "_id": 4,
              "x": 44
            },
            {
              "_id": 5,
              "x": 55
            }
          ]
        }
      ],
      "expectEvents": [
        {
          "client": "client",
          "eventType": "command",
          "events": [
            {
              "commandStartedEvent": {
                "command": {
                  "find": "collection",
                  "batchSize": 1,
                  "$db": "db",
                  "lsid": {
                    "$$exists": true
                  }
                },
                "commandName": "find"
              }
            },
            {
              "commandStartedEvent": {
                "command": {
                  "getMore": {
                    "$$type": [
                      "int",
                      "long"
                    ]
                  },
                  "collection": "collection",
                  "batchSize": 5,
                  "$db": "db",
                  "lsid": {
                    "$$exists": true
                  }
                },
                "commandName": "getMore"
              }
            }
          ]
        }
      ]
    },
    {
      "description": "supports configuring getMore maxTimeMS",
      "operations": [
        {
          "name": "runCursorCommand",
          "object": "db",
          "arguments": {
            "commandName": "find",
            "maxTimeMS": 300,
            "command": {
              "find": "collection",
              "maxTimeMS": 200,
              "batchSize": 1
            }
          },
          "ignoreResultAndError": true
        }
      ],
      "expectEvents": [
        {
          "client": "client",
          "eventType": "command",
          "ignoreExtraEvents": true,
          "events": [
            {
              "commandStartedEvent": {
                "command": {
                  "find": "collection",
                  "maxTimeMS": 200,
                  "batchSize": 1,
                  "$db": "db",
                  "lsid": {
                    "$$exists": true
                  }
                },
                "commandName": "find"
              }
            },
            {
              "commandStartedEvent": {
                "command": {
                  "getMore": {
                    "$$type": [
                      "int",
                      "long"
                    ]
                  },
                  "collection": "collection",
                  "$db": "db",
                  "maxTimeMS": 300,
                  "lsid": {
                    "$$exists": true
                  }
                },
                "commandName": "getMore"
              }
            }
          ]
        }
      ]
    },
    {
      "description": "supports configuring getMore comment",
      "runOnRequirements": [
        {
          "minServerVersion": "4.4"
        }
      ],
      "operations": [
        {
          "name": "runCursorCommand",
          "object": "db",
          "arguments": {
            "commandName": "find",
            "comment": {
              "hello": "getMore"
            },
            "command": {
              "find": "collection",
              "batchSize": 1,
              "comment": {
                "hello": "find"
              }
            }
          },
          "expectResult": [
            {
              "_id": 1,
              "x": 11
            },
            {
              "_id": 2,
              "x": 22
            },
            {
              "_id": 3,
              "x": 33
            },
            {
              "_id": 4,
              "x": 44
            },
            {
              "_id": 5,
              "x": 55
            }
          ]
        }
      ],
      "expectEvents": [
        {
          "client": "client",
          "eventType": "command",
          "events": [
            {
              "commandStartedEvent": {
                "command": {
                  "find": "collection",
                  "batchSize": 1,
                  "comment": {
                    "hello": "find"
                  },
                  "$db": "db",
                  "lsid": {
                    "$$exists": true
                  }
                },
                "commandName": "find"
              }
            },
            {
              "commandStartedEvent": {
                "command": {
                  "getMore": {
                    "$$type": [
                      "int",
                      "long"
                    ]
                  },
                  "collection": "collection",
                  "comment": {
                    "hello": "getMore"
                  },
                  "$db": "db",
                  "lsid": {
                    "$$exists": true
                  }
                },
                "commandName": "getMore"
              }
            }
          ]
        }
      ]
    },
    {
      "description": "does not close the cursor when receiving an empty batch",
      "runOnRequirements": [
        {
          "serverless": "forbid"
        }
      ],
      "operations": [
        {
          "name": "dropCollection",
          "object": "db",
          "arguments": {
            "collection": "cappedCollection"
          }
        },
        {
          "name": "createCollection",
          "object": "db",
          "arguments": {
            "collection": "cappedCollection",
            "capped": true,
            "size": 4096,
            "max": 3
          },
          "saveResultAsEntity": "cappedCollection"
        },
        {
          "name": "insertMany",
          "object": "cappedCollection",
          "arguments": {
            "documents": [
              {
                "_id": 1,
                "x": 11
              },
              {
                "_id": 2,
                "x": 22
              }
            ]
          }
        },
        {
          "name": "createCommandCursor",
          "object": "db",
          "arguments": {
            "cursorType": "tailable",
            "commandName": "find",
            "batchSize": 2,
            "command": {
              "find": "cappedCollection",
              "tailable": true
            }
          },
          "saveResultAsEntity": "cursor"
        },
        {
          "name": "iterateOnce",
          "object": "cursor"
        },
        {
          "name": "iterateOnce",
          "object": "cursor"
        },
        {
          "name": "iterateOnce",
          "object": "cursor"
        },
        {
          "name": "close",
          "object": "cursor"
        }
      ],
      "expectEvents": [
        {
          "client": "client",
          "eventType": "command",
          "events": [
            {
              "commandStartedEvent": {
                "command": {
                  "drop": "cappedCollection"
                },
                "commandName": "drop"
              }
            },
            {
              "commandStartedEvent": {
                "command": {
                  "create": "cappedCollection"
                },
                "commandName": "create"
              }
            },
            {
              "commandStartedEvent": {
                "command": {
                  "insert": "cappedCollection"
                },
                "commandName": "insert"
              }
            },
            {
              "commandStartedEvent": {
                "command": {
                  "find": "cappedCollection",
                  "$db": "db",
                  "lsid": {
                    "$$exists": true
                  }
                },
                "commandName": "find"
              }
            },
            {
              "commandStartedEvent": {
                "command": {
                  "getMore": {
                    "$$type": [
                      "int",
                      "long"
                    ]
                  },
                  "collection": "cappedCollection",
                  "$db": "db",
                  "lsid": {
                    "$$exists": true
                  }
                },
                "commandName": "getMore"
              }
            },
            {
              "commandStartedEvent": {
                "command": {
                  "killCursors": "cappedCollection",
                  "cursors": {
                    "$$type": "array"
                  }
                },
                "commandName": "killCursors"
              }
            }
          ]
        }
      ]
    }
  ]
}
