Skip to content

Commit 22caa27

Browse files
committed
Add more tests for maxTimeMS PYTHON-550
1 parent 502b7d4 commit 22caa27

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

test/test_cursor.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -102,19 +102,24 @@ def test_max_time_ms(self):
102102

103103
self.assertTrue(coll.find_one(max_time_ms=1000))
104104

105-
reducer = Code("""function(obj, prev){prev.count++;}""")
106-
coll.group(key={"amalia": 1}, condition={}, initial={"count": 0},
107-
reduce=reducer, maxTimeMS=1000)
108-
109105
if "enableTestCommands=1" in get_command_line(self.client)["argv"]:
110106
self.client.admin.command("configureFailPoint",
111107
"maxTimeAlwaysTimeOut",
112108
mode="alwaysOn")
113-
self.assertRaises(ExecutionTimeout,
114-
coll.find_one, max_time_ms=1)
115-
self.client.admin.command("configureFailPoint",
116-
"maxTimeAlwaysTimeOut",
117-
mode="off")
109+
try:
110+
cursor = coll.find().max_time_ms(1)
111+
try:
112+
cursor.next()
113+
except ExecutionTimeout:
114+
pass
115+
else:
116+
self.fail("ExecutionTimeout not raised")
117+
self.assertRaises(ExecutionTimeout,
118+
coll.find_one, max_time_ms=1)
119+
finally:
120+
self.client.admin.command("configureFailPoint",
121+
"maxTimeAlwaysTimeOut",
122+
mode="off")
118123

119124
def test_explain(self):
120125
a = self.db.test.find()

test/test_database.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,15 @@
4040
from pymongo.database import Database
4141
from pymongo.errors import (CollectionInvalid,
4242
ConfigurationError,
43+
ExecutionTimeout,
4344
InvalidName,
4445
OperationFailure)
4546
from pymongo.son_manipulator import (AutoReference,
4647
NamespaceInjector,
4748
ObjectIdShuffler)
4849
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)
5152
from test.test_client import get_client
5253

5354

@@ -959,6 +960,35 @@ def test_command_read_pref_warning(self):
959960
warnings.resetwarnings()
960961
warnings.simplefilter("ignore")
961962

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+
962992

963993
if __name__ == "__main__":
964994
unittest.main()

0 commit comments

Comments
 (0)