|
40 | 40 | from pymongo.database import Database |
41 | 41 | from pymongo.errors import (CollectionInvalid, |
42 | 42 | ConfigurationError, |
| 43 | + ExecutionTimeout, |
43 | 44 | InvalidName, |
44 | 45 | OperationFailure) |
45 | 46 | from pymongo.son_manipulator import (AutoReference, |
46 | 47 | NamespaceInjector, |
47 | 48 | ObjectIdShuffler) |
48 | 49 | from test import version |
49 | | -from test.utils import (is_mongos, server_started_with_auth, |
50 | | - remove_all_users) |
| 50 | +from test.utils import (get_command_line, is_mongos, |
| 51 | + remove_all_users, server_started_with_auth) |
51 | 52 | from test.test_client import get_client |
52 | 53 |
|
53 | 54 |
|
@@ -959,6 +960,35 @@ def test_command_read_pref_warning(self): |
959 | 960 | warnings.resetwarnings() |
960 | 961 | warnings.simplefilter("ignore") |
961 | 962 |
|
| 963 | + def test_command_max_time_ms(self): |
| 964 | + if not version.at_least(self.client, (2, 5, 3, -1)): |
| 965 | + raise SkipTest("MaxTimeMS requires MongoDB >= 2.5.3") |
| 966 | + if "enableTestCommands=1" not in get_command_line(self.client)["argv"]: |
| 967 | + raise SkipTest("Test commands must be enabled.") |
| 968 | + |
| 969 | + self.client.admin.command("configureFailPoint", |
| 970 | + "maxTimeAlwaysTimeOut", |
| 971 | + mode="alwaysOn") |
| 972 | + try: |
| 973 | + db = self.client.pymongo_test |
| 974 | + db.command('count', 'test') |
| 975 | + self.assertRaises(ExecutionTimeout, db.command, |
| 976 | + 'count', 'test', maxTimeMS=1) |
| 977 | + pipeline = [{'$project': {'name': 1, 'count': 1}}] |
| 978 | + # Database command helper. |
| 979 | + db.command('aggregate', 'test', pipeline=pipeline) |
| 980 | + self.assertRaises(ExecutionTimeout, db.command, |
| 981 | + 'aggregate', 'test', |
| 982 | + pipeline=pipeline, maxTimeMS=1) |
| 983 | + # Collection helper. |
| 984 | + db.test.aggregate(pipeline=pipeline) |
| 985 | + self.assertRaises(ExecutionTimeout, |
| 986 | + db.test.aggregate, pipeline, maxTimeMS=1) |
| 987 | + finally: |
| 988 | + self.client.admin.command("configureFailPoint", |
| 989 | + "maxTimeAlwaysTimeOut", |
| 990 | + mode="off") |
| 991 | + |
962 | 992 |
|
963 | 993 | if __name__ == "__main__": |
964 | 994 | unittest.main() |
0 commit comments