{
  "description": "timeoutMS behaves correctly when closing cursors",
  "schemaVersion": "1.9",
  "runOnRequirements": [
    {
      "minServerVersion": "4.4"
    }
  ],
  "createEntities": [
    {
      "client": {
        "id": "failPointClient",
        "useMultipleMongoses": false
      }
    },
    {
      "client": {
        "id": "client",
        "useMultipleMongoses": false,
        "observeEvents": [
          "commandStartedEvent",
          "commandSucceededEvent",
          "commandFailedEvent"
        ]
      }
    },
    {
      "database": {
        "id": "database",
        "client": "client",
        "databaseName": "test"
      }
    },
    {
      "collection": {
        "id": "collection",
        "database": "database",
        "collectionName": "coll"
      }
    }
  ],
  "initialData": [
    {
      "collectionName": "coll",
      "databaseName": "test",
      "documents": [
        {
          "_id": 0
        },
        {
          "_id": 1
        },
        {
          "_id": 2
        }
      ]
    }
  ],
  "tests": [
    {
      "description": "timeoutMS is refreshed for close",
      "operations": [
        {
          "name": "failPoint",
          "object": "testRunner",
          "arguments": {
            "client": "failPointClient",
            "failPoint": {
              "configureFailPoint": "failCommand",
              "mode": {
                "times": 1
              },
              "data": {
                "failCommands": [
                  "getMore"
                ],
                "blockConnection": true,
                "blockTimeMS": 250
              }
            }
          }
        },
        {
          "name": "createFindCursor",
          "object": "collection",
          "arguments": {
            "filter": {},
            "batchSize": 2,
            "timeoutMS": 200
          },
          "saveResultAsEntity": "cursor"
        },
        {
          "name": "iterateUntilDocumentOrError",
          "object": "cursor"
        },
        {
          "name": "iterateUntilDocumentOrError",
          "object": "cursor"
        },
        {
          "name": "iterateUntilDocumentOrError",
          "object": "cursor",
          "expectError": {
            "isTimeoutError": true
          }
        },
        {
          "name": "close",
          "object": "cursor"
        }
      ],
      "expectEvents": [
        {
          "client": "client",
          "events": [
            {
              "commandStartedEvent": {
                "commandName": "find"
              }
            },
            {
              "commandSucceededEvent": {
                "commandName": "find"
              }
            },
            {
              "commandStartedEvent": {
                "commandName": "getMore"
              }
            },
            {
              "commandFailedEvent": {
                "commandName": "getMore"
              }
            },
            {
              "commandStartedEvent": {
                "command": {
                  "killCursors": "coll",
                  "maxTimeMS": {
                    "$$type": [
                      "int",
                      "long"
                    ]
                  }
                },
                "commandName": "killCursors"
              }
            },
            {
              "commandSucceededEvent": {
                "commandName": "killCursors"
              }
            }
          ]
        }
      ]
    },
    {
      "description": "timeoutMS can be overridden for close",
      "operations": [
        {
          "name": "failPoint",
          "object": "testRunner",
          "arguments": {
            "client": "client",
            "failPoint": {
              "configureFailPoint": "failCommand",
              "mode": {
                "times": 1
              },
              "data": {
                "failCommands": [
                  "killCursors"
                ],
                "blockConnection": true,
                "blockTimeMS": 250
              }
            }
          }
        },
        {
          "name": "createFindCursor",
          "object": "collection",
          "arguments": {
            "filter": {},
            "batchSize": 2,
            "timeoutMS": 200
          },
          "saveResultAsEntity": "cursor"
        },
        {
          "name": "close",
          "object": "cursor",
          "arguments": {
            "timeoutMS": 400
          }
        }
      ],
      "expectEvents": [
        {
          "client": "client",
          "events": [
            {
              "commandStartedEvent": {
                "commandName": "find"
              }
            },
            {
              "commandSucceededEvent": {
                "commandName": "find"
              }
            },
            {
              "commandStartedEvent": {
                "command": {
                  "killCursors": "coll",
                  "maxTimeMS": {
                    "$$type": [
                      "int",
                      "long"
                    ]
                  }
                },
                "commandName": "killCursors"
              }
            },
            {
              "commandSucceededEvent": {
                "commandName": "killCursors"
              }
            }
          ]
        }
      ]
    }
  ]
}
