{
  "description": "timeoutMS behaves correctly for non-tailable cursors",
  "schemaVersion": "1.9",
  "runOnRequirements": [
    {
      "minServerVersion": "4.4"
    }
  ],
  "createEntities": [
    {
      "client": {
        "id": "failPointClient",
        "useMultipleMongoses": false
      }
    },
    {
      "client": {
        "id": "client",
        "uriOptions": {
          "timeoutMS": 200
        },
        "useMultipleMongoses": false,
        "observeEvents": [
          "commandStartedEvent"
        ],
        "ignoreCommandMonitoringEvents": [
          "killCursors"
        ]
      }
    },
    {
      "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
        }
      ]
    },
    {
      "collectionName": "aggregateOutputColl",
      "databaseName": "test",
      "documents": []
    }
  ],
  "tests": [
    {
      "description": "timeoutMS applied to find if timeoutMode is cursor_lifetime",
      "operations": [
        {
          "name": "failPoint",
          "object": "testRunner",
          "arguments": {
            "client": "failPointClient",
            "failPoint": {
              "configureFailPoint": "failCommand",
              "mode": {
                "times": 1
              },
              "data": {
                "failCommands": [
                  "find"
                ],
                "blockConnection": true,
                "blockTimeMS": 250
              }
            }
          }
        },
        {
          "name": "find",
          "object": "collection",
          "arguments": {
            "filter": {},
            "timeoutMode": "cursorLifetime"
          },
          "expectError": {
            "isTimeoutError": true
          }
        }
      ],
      "expectEvents": [
        {
          "client": "client",
          "events": [
            {
              "commandStartedEvent": {
                "commandName": "find",
                "databaseName": "test",
                "command": {
                  "find": "coll",
                  "maxTimeMS": {
                    "$$type": [
                      "int",
                      "long"
                    ]
                  }
                }
              }
            }
          ]
        }
      ]
    },
    {
      "description": "remaining timeoutMS applied to getMore if timeoutMode is unset",
      "operations": [
        {
          "name": "failPoint",
          "object": "testRunner",
          "arguments": {
            "client": "failPointClient",
            "failPoint": {
              "configureFailPoint": "failCommand",
              "mode": {
                "times": 2
              },
              "data": {
                "failCommands": [
                  "find",
                  "getMore"
                ],
                "blockConnection": true,
                "blockTimeMS": 125
              }
            }
          }
        },
        {
          "name": "find",
          "object": "collection",
          "arguments": {
            "filter": {},
            "timeoutMS": 200,
            "batchSize": 2
          },
          "expectError": {
            "isTimeoutError": true
          }
        }
      ],
      "expectEvents": [
        {
          "client": "client",
          "events": [
            {
              "commandStartedEvent": {
                "commandName": "find",
                "databaseName": "test",
                "command": {
                  "find": "coll",
                  "maxTimeMS": {
                    "$$type": [
                      "int",
                      "long"
                    ]
                  }
                }
              }
            },
            {
              "commandStartedEvent": {
                "commandName": "getMore",
                "databaseName": "test",
                "command": {
                  "getMore": {
                    "$$type": [
                      "int",
                      "long"
                    ]
                  },
                  "collection": "coll",
                  "maxTimeMS": {
                    "$$exists": false
                  }
                }
              }
            }
          ]
        }
      ]
    },
    {
      "description": "remaining timeoutMS applied to getMore if timeoutMode is cursor_lifetime",
      "operations": [
        {
          "name": "failPoint",
          "object": "testRunner",
          "arguments": {
            "client": "failPointClient",
            "failPoint": {
              "configureFailPoint": "failCommand",
              "mode": {
                "times": 2
              },
              "data": {
                "failCommands": [
                  "find",
                  "getMore"
                ],
                "blockConnection": true,
                "blockTimeMS": 150
              }
            }
          }
        },
        {
          "name": "find",
          "object": "collection",
          "arguments": {
            "filter": {},
            "timeoutMode": "cursorLifetime",
            "timeoutMS": 200,
            "batchSize": 2
          },
          "expectError": {
            "isTimeoutError": true
          }
        }
      ],
      "expectEvents": [
        {
          "client": "client",
          "events": [
            {
              "commandStartedEvent": {
                "commandName": "find",
                "databaseName": "test",
                "command": {
                  "find": "coll",
                  "maxTimeMS": {
                    "$$type": [
                      "int",
                      "long"
                    ]
                  }
                }
              }
            },
            {
              "commandStartedEvent": {
                "commandName": "getMore",
                "databaseName": "test",
                "command": {
                  "getMore": {
                    "$$type": [
                      "int",
                      "long"
                    ]
                  },
                  "collection": "coll",
                  "maxTimeMS": {
                    "$$exists": false
                  }
                }
              }
            }
          ]
        }
      ]
    },
    {
      "description": "timeoutMS applied to find if timeoutMode is iteration",
      "operations": [
        {
          "name": "failPoint",
          "object": "testRunner",
          "arguments": {
            "client": "failPointClient",
            "failPoint": {
              "configureFailPoint": "failCommand",
              "mode": {
                "times": 1
              },
              "data": {
                "failCommands": [
                  "find"
                ],
                "blockConnection": true,
                "blockTimeMS": 250
              }
            }
          }
        },
        {
          "name": "find",
          "object": "collection",
          "arguments": {
            "filter": {},
            "timeoutMode": "iteration"
          },
          "expectError": {
            "isTimeoutError": true
          }
        }
      ],
      "expectEvents": [
        {
          "client": "client",
          "events": [
            {
              "commandStartedEvent": {
                "commandName": "find",
                "databaseName": "test",
                "command": {
                  "find": "coll",
                  "maxTimeMS": {
                    "$$exists": false
                  }
                }
              }
            }
          ]
        }
      ]
    },
    {
      "description": "timeoutMS is refreshed for getMore if timeoutMode is iteration - success",
      "operations": [
        {
          "name": "failPoint",
          "object": "testRunner",
          "arguments": {
            "client": "failPointClient",
            "failPoint": {
              "configureFailPoint": "failCommand",
              "mode": {
                "times": 2
              },
              "data": {
                "failCommands": [
                  "find",
                  "getMore"
                ],
                "blockConnection": true,
                "blockTimeMS": 125
              }
            }
          }
        },
        {
          "name": "find",
          "object": "collection",
          "arguments": {
            "filter": {},
            "timeoutMode": "iteration",
            "timeoutMS": 200,
            "batchSize": 2
          }
        }
      ],
      "expectEvents": [
        {
          "client": "client",
          "events": [
            {
              "commandStartedEvent": {
                "commandName": "find",
                "databaseName": "test",
                "command": {
                  "find": "coll",
                  "maxTimeMS": {
                    "$$exists": false
                  }
                }
              }
            },
            {
              "commandStartedEvent": {
                "commandName": "getMore",
                "databaseName": "test",
                "command": {
                  "getMore": {
                    "$$type": [
                      "int",
                      "long"
                    ]
                  },
                  "collection": "coll",
                  "maxTimeMS": {
                    "$$exists": false
                  }
                }
              }
            }
          ]
        }
      ]
    },
    {
      "description": "timeoutMS is refreshed for getMore if timeoutMode is iteration - failure",
      "operations": [
        {
          "name": "failPoint",
          "object": "testRunner",
          "arguments": {
            "client": "failPointClient",
            "failPoint": {
              "configureFailPoint": "failCommand",
              "mode": {
                "times": 1
              },
              "data": {
                "failCommands": [
                  "getMore"
                ],
                "blockConnection": true,
                "blockTimeMS": 250
              }
            }
          }
        },
        {
          "name": "find",
          "object": "collection",
          "arguments": {
            "filter": {},
            "timeoutMode": "iteration",
            "batchSize": 2
          },
          "expectError": {
            "isTimeoutError": true
          }
        }
      ],
      "expectEvents": [
        {
          "client": "client",
          "events": [
            {
              "commandStartedEvent": {
                "commandName": "find",
                "databaseName": "test",
                "command": {
                  "find": "coll",
                  "maxTimeMS": {
                    "$$exists": false
                  }
                }
              }
            },
            {
              "commandStartedEvent": {
                "commandName": "getMore",
                "databaseName": "test",
                "command": {
                  "getMore": {
                    "$$type": [
                      "int",
                      "long"
                    ]
                  },
                  "collection": "coll",
                  "maxTimeMS": {
                    "$$exists": false
                  }
                }
              }
            }
          ]
        }
      ]
    },
    {
      "description": "aggregate with $out errors if timeoutMode is iteration",
      "operations": [
        {
          "name": "aggregate",
          "object": "collection",
          "arguments": {
            "pipeline": [
              {
                "$out": "aggregateOutputColl"
              }
            ],
            "timeoutMS": 100,
            "timeoutMode": "iteration"
          },
          "expectError": {
            "isClientError": true
          }
        }
      ],
      "expectEvents": [
        {
          "client": "client",
          "events": []
        }
      ]
    },
    {
      "description": "aggregate with $merge errors if timeoutMode is iteration",
      "operations": [
        {
          "name": "aggregate",
          "object": "collection",
          "arguments": {
            "pipeline": [
              {
                "$merge": "aggregateOutputColl"
              }
            ],
            "timeoutMS": 100,
            "timeoutMode": "iteration"
          },
          "expectError": {
            "isClientError": true
          }
        }
      ],
      "expectEvents": [
        {
          "client": "client",
          "events": []
        }
      ]
    }
  ]
}
