{
  "description": "runCursorCommand",
  "schemaVersion": "1.9",
  "runOnRequirements": [
    {
      "minServerVersion": "4.4"
    }
  ],
  "createEntities": [
    {
      "client": {
        "id": "failPointClient",
        "useMultipleMongoses": false
      }
    },
    {
      "client": {
        "id": "commandClient",
        "useMultipleMongoses": false,
        "observeEvents": [
          "commandStartedEvent",
          "commandSucceededEvent"
        ]
      }
    },
    {
      "client": {
        "id": "client",
        "useMultipleMongoses": false,
        "observeEvents": [
          "commandStartedEvent"
        ],
        "ignoreCommandMonitoringEvents": [
          "killCursors"
        ]
      }
    },
    {
      "database": {
        "id": "commandDb",
        "client": "commandClient",
        "databaseName": "commandDb"
      }
    },
    {
      "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": "errors if timeoutMode is set without timeoutMS",
      "operations": [
        {
          "name": "runCursorCommand",
          "object": "db",
          "arguments": {
            "commandName": "find",
            "command": {
              "find": "collection"
            },
            "timeoutMode": "cursorLifetime"
          },
          "expectError": {
            "isClientError": true
          }
        }
      ]
    },
    {
      "description": "error if timeoutMode is cursorLifetime and cursorType is tailableAwait",
      "operations": [
        {
          "name": "runCursorCommand",
          "object": "db",
          "arguments": {
            "commandName": "find",
            "command": {
              "find": "collection"
            },
            "timeoutMode": "cursorLifetime",
            "cursorType": "tailableAwait"
          },
          "expectError": {
            "isClientError": true
          }
        }
      ]
    },
    {
      "description": "Non-tailable cursor lifetime remaining timeoutMS applied to getMore if timeoutMode is unset",
      "runOnRequirements": [
        {
          "serverless": "forbid"
        }
      ],
      "operations": [
        {
          "name": "failPoint",
          "object": "testRunner",
          "arguments": {
            "client": "failPointClient",
            "failPoint": {
              "configureFailPoint": "failCommand",
              "mode": {
                "times": 2
              },
              "data": {
                "failCommands": [
                  "find",
                  "getMore"
                ],
                "blockConnection": true,
                "blockTimeMS": 60
              }
            }
          }
        },
        {
          "name": "runCursorCommand",
          "object": "db",
          "arguments": {
            "commandName": "find",
            "timeoutMS": 100,
            "command": {
              "find": "collection",
              "batchSize": 2
            }
          },
          "expectError": {
            "isTimeoutError": true
          }
        }
      ],
      "expectEvents": [
        {
          "client": "client",
          "events": [
            {
              "commandStartedEvent": {
                "commandName": "find",
                "command": {
                  "find": "collection",
                  "maxTimeMS": {
                    "$$type": [
                      "int",
                      "long"
                    ]
                  }
                }
              }
            },
            {
              "commandStartedEvent": {
                "commandName": "getMore",
                "command": {
                  "getMore": {
                    "$$type": [
                      "int",
                      "long"
                    ]
                  },
                  "collection": "collection",
                  "maxTimeMS": {
                    "$$exists": true
                  }
                }
              }
            }
          ]
        }
      ]
    },
    {
      "description": "Non=tailable cursor iteration timeoutMS is refreshed for getMore if timeoutMode is iteration - failure",
      "runOnRequirements": [
        {
          "serverless": "forbid"
        }
      ],
      "operations": [
        {
          "name": "failPoint",
          "object": "testRunner",
          "arguments": {
            "client": "failPointClient",
            "failPoint": {
              "configureFailPoint": "failCommand",
              "mode": {
                "times": 1
              },
              "data": {
                "failCommands": [
                  "getMore"
                ],
                "blockConnection": true,
                "blockTimeMS": 60
              }
            }
          }
        },
        {
          "name": "runCursorCommand",
          "object": "db",
          "arguments": {
            "commandName": "find",
            "command": {
              "find": "collection",
              "batchSize": 2
            },
            "timeoutMode": "iteration",
            "timeoutMS": 100,
            "batchSize": 2
          },
          "expectError": {
            "isTimeoutError": true
          }
        }
      ],
      "expectEvents": [
        {
          "client": "client",
          "events": [
            {
              "commandStartedEvent": {
                "commandName": "find",
                "databaseName": "db",
                "command": {
                  "find": "collection",
                  "maxTimeMS": {
                    "$$exists": false
                  }
                }
              }
            },
            {
              "commandStartedEvent": {
                "commandName": "getMore",
                "databaseName": "db",
                "command": {
                  "getMore": {
                    "$$type": [
                      "int",
                      "long"
                    ]
                  },
                  "collection": "collection",
                  "maxTimeMS": {
                    "$$exists": false
                  }
                }
              }
            }
          ]
        }
      ]
    },
    {
      "description": "Tailable cursor iteration timeoutMS is refreshed for getMore - failure",
      "runOnRequirements": [
        {
          "serverless": "forbid"
        }
      ],
      "operations": [
        {
          "name": "failPoint",
          "object": "testRunner",
          "arguments": {
            "client": "failPointClient",
            "failPoint": {
              "configureFailPoint": "failCommand",
              "mode": {
                "times": 1
              },
              "data": {
                "failCommands": [
                  "getMore"
                ],
                "blockConnection": true,
                "blockTimeMS": 60
              }
            }
          }
        },
        {
          "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": {
            "commandName": "find",
            "command": {
              "find": "cappedCollection",
              "batchSize": 1,
              "tailable": true
            },
            "timeoutMode": "iteration",
            "timeoutMS": 100,
            "batchSize": 1,
            "cursorType": "tailable"
          },
          "saveResultAsEntity": "tailableCursor"
        },
        {
          "name": "iterateUntilDocumentOrError",
          "object": "tailableCursor"
        },
        {
          "name": "iterateUntilDocumentOrError",
          "object": "tailableCursor",
          "expectError": {
            "isTimeoutError": true
          }
        }
      ],
      "expectEvents": [
        {
          "client": "client",
          "events": [
            {
              "commandStartedEvent": {
                "commandName": "drop"
              }
            },
            {
              "commandStartedEvent": {
                "commandName": "create"
              }
            },
            {
              "commandStartedEvent": {
                "commandName": "insert"
              }
            },
            {
              "commandStartedEvent": {
                "commandName": "find",
                "databaseName": "db",
                "command": {
                  "find": "cappedCollection",
                  "tailable": true,
                  "awaitData": {
                    "$$exists": false
                  },
                  "maxTimeMS": {
                    "$$exists": false
                  }
                }
              }
            },
            {
              "commandStartedEvent": {
                "commandName": "getMore",
                "databaseName": "db",
                "command": {
                  "getMore": {
                    "$$type": [
                      "int",
                      "long"
                    ]
                  },
                  "collection": "cappedCollection",
                  "maxTimeMS": {
                    "$$exists": false
                  }
                }
              }
            }
          ]
        }
      ]
    },
    {
      "description": "Tailable cursor awaitData iteration timeoutMS is refreshed for getMore - failure",
      "runOnRequirements": [
        {
          "serverless": "forbid"
        }
      ],
      "operations": [
        {
          "name": "failPoint",
          "object": "testRunner",
          "arguments": {
            "client": "failPointClient",
            "failPoint": {
              "configureFailPoint": "failCommand",
              "mode": {
                "times": 1
              },
              "data": {
                "failCommands": [
                  "getMore"
                ],
                "blockConnection": true,
                "blockTimeMS": 60
              }
            }
          }
        },
        {
          "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": [
              {
                "foo": "bar"
              },
              {
                "fizz": "buzz"
              }
            ]
          }
        },
        {
          "name": "createCommandCursor",
          "object": "db",
          "arguments": {
            "command": {
              "find": "cappedCollection",
              "tailable": true,
              "awaitData": true
            },
            "cursorType": "tailableAwait",
            "batchSize": 1
          },
          "saveResultAsEntity": "tailableCursor"
        },
        {
          "name": "iterateUntilDocumentOrError",
          "object": "tailableCursor"
        },
        {
          "name": "iterateUntilDocumentOrError",
          "object": "tailableCursor",
          "expectError": {
            "isTimeoutError": true
          }
        }
      ],
      "expectEvents": [
        {
          "client": "client",
          "events": [
            {
              "commandStartedEvent": {
                "commandName": "drop"
              }
            },
            {
              "commandStartedEvent": {
                "commandName": "create"
              }
            },
            {
              "commandStartedEvent": {
                "commandName": "insert"
              }
            },
            {
              "commandStartedEvent": {
                "commandName": "find",
                "databaseName": "db",
                "command": {
                  "find": "cappedCollection",
                  "tailable": true,
                  "awaitData": true,
                  "maxTimeMS": {
                    "$$exists": true
                  }
                }
              }
            },
            {
              "commandStartedEvent": {
                "commandName": "getMore",
                "databaseName": "db",
                "command": {
                  "getMore": {
                    "$$type": [
                      "int",
                      "long"
                    ]
                  },
                  "collection": "cappedCollection"
                }
              }
            }
          ]
        }
      ]
    }
  ]
}
