Skip to content

Commit d0fca05

Browse files
committed
Improved testing for max_time_ms, PYTHON-550.
1 parent cb6ded3 commit d0fca05

File tree

1 file changed

+28
-26
lines changed

1 file changed

+28
-26
lines changed

test/test_cursor.py

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@
1919
import re
2020
import sys
2121
import unittest
22-
from struct import pack
2322
sys.path[0:0] = [""]
2423

2524
from nose.plugins.skip import SkipTest
2625

27-
from bson import BSON
2826
from bson.code import Code
2927
from bson.son import SON
3028
from pymongo import (ASCENDING,
@@ -37,8 +35,6 @@
3735
from pymongo.errors import (InvalidOperation,
3836
OperationFailure,
3937
ExecutionTimeout)
40-
from pymongo.helpers import (_unpack_response,
41-
_check_command_response)
4238
from test import version
4339
from test.test_client import get_client
4440
from test.utils import is_mongos, get_command_line, server_started_with_auth
@@ -57,28 +53,6 @@ def test_max_time_ms(self):
5753
if not version.at_least(self.db.connection, (2, 5, 3, -1)):
5854
raise SkipTest("MaxTimeMS requires MongoDB >= 2.5.3")
5955

60-
max_time_ms_response = {
61-
'$err': 'operation exceeded time limit',
62-
'code': 50
63-
}
64-
bson_response = BSON.encode(max_time_ms_response)
65-
response_flags = pack("<i", 2)
66-
cursor_id = pack("<q", 0)
67-
starting_from = pack("<i", 0)
68-
number_returned = pack("<i", 1)
69-
op_reply = (response_flags + cursor_id + starting_from +
70-
number_returned + bson_response)
71-
self.assertRaises(ExecutionTimeout, _unpack_response,
72-
op_reply)
73-
74-
command_response = {
75-
'ok': 0,
76-
'errmsg': 'operation exceeded time limit',
77-
'code': 50
78-
}
79-
self.assertRaises(ExecutionTimeout, _check_command_response,
80-
command_response, None)
81-
8256
db = self.db
8357
db.pymongo_test.drop()
8458
coll = db.pymongo_test
@@ -103,6 +77,7 @@ def test_max_time_ms(self):
10377
self.assertTrue(coll.find_one(max_time_ms=1000))
10478

10579
if "enableTestCommands=1" in get_command_line(self.client)["argv"]:
80+
# Cursor parses server timeout error in response to initial query.
10681
self.client.admin.command("configureFailPoint",
10782
"maxTimeAlwaysTimeOut",
10883
mode="alwaysOn")
@@ -121,6 +96,33 @@ def test_max_time_ms(self):
12196
"maxTimeAlwaysTimeOut",
12297
mode="off")
12398

99+
def test_max_time_ms_getmore(self):
100+
# Test that Cursor handles server timeout error in response to getmore.
101+
if "enableTestCommands=1" not in get_command_line(self.client)["argv"]:
102+
raise SkipTest("Need test commands enabled")
103+
104+
coll = self.db.pymongo_test
105+
coll.insert({} for _ in range(200))
106+
cursor = coll.find().max_time_ms(100)
107+
108+
# Send initial query before turning on failpoint.
109+
cursor.next()
110+
self.client.admin.command("configureFailPoint",
111+
"maxTimeAlwaysTimeOut",
112+
mode="alwaysOn")
113+
try:
114+
try:
115+
# Iterate up to first getmore.
116+
list(cursor)
117+
except ExecutionTimeout:
118+
pass
119+
else:
120+
self.fail("ExecutionTimeout not raised")
121+
finally:
122+
self.client.admin.command("configureFailPoint",
123+
"maxTimeAlwaysTimeOut",
124+
mode="off")
125+
124126
def test_explain(self):
125127
a = self.db.test.find()
126128
b = a.explain()

0 commit comments

Comments
 (0)