|
25 | 25 | from bson.py3compat import u, PY3 |
26 | 26 | from bson.son import SON |
27 | 27 | from pymongo import (MongoClient, |
| 28 | + monitoring, |
28 | 29 | ASCENDING, |
29 | 30 | DESCENDING, |
30 | 31 | ALL, |
|
41 | 42 | host, |
42 | 43 | port, |
43 | 44 | IntegrationTest) |
44 | | -from test.utils import server_started_with_auth |
| 45 | +from test.utils import server_started_with_auth, single_client, EventListener |
45 | 46 |
|
46 | 47 | if PY3: |
47 | 48 | long = int |
@@ -190,6 +191,114 @@ def test_max_time_ms(self): |
190 | 191 | "maxTimeAlwaysTimeOut", |
191 | 192 | mode="off") |
192 | 193 |
|
| 194 | + @client_context.require_version_min(3, 1, 9, -1) |
| 195 | + def test_max_await_time_ms(self): |
| 196 | + db = self.db |
| 197 | + db.pymongo_test.drop() |
| 198 | + coll = db.create_collection("pymongo_test", capped=True, size=4096) |
| 199 | + |
| 200 | + self.assertRaises(TypeError, coll.find().max_await_time_ms, 'foo') |
| 201 | + coll.insert_one({"amalia": 1}) |
| 202 | + coll.insert_one({"amalia": 2}) |
| 203 | + |
| 204 | + coll.find().max_await_time_ms(None) |
| 205 | + coll.find().max_await_time_ms(long(1)) |
| 206 | + |
| 207 | + # When cursor is not tailable_await |
| 208 | + cursor = coll.find() |
| 209 | + self.assertEqual(None, cursor._Cursor__max_await_time_ms) |
| 210 | + cursor = coll.find().max_await_time_ms(99) |
| 211 | + self.assertEqual(None, cursor._Cursor__max_await_time_ms) |
| 212 | + |
| 213 | + # If cursor is tailable_await and timeout is unset |
| 214 | + cursor = coll.find(cursor_type=CursorType.TAILABLE_AWAIT) |
| 215 | + self.assertEqual(None, cursor._Cursor__max_await_time_ms) |
| 216 | + |
| 217 | + # If cursor is tailable_await and timeout is set |
| 218 | + cursor = coll.find( |
| 219 | + cursor_type=CursorType.TAILABLE_AWAIT).max_await_time_ms(99) |
| 220 | + self.assertEqual(99, cursor._Cursor__max_await_time_ms) |
| 221 | + |
| 222 | + cursor = coll.find( |
| 223 | + cursor_type=CursorType.TAILABLE_AWAIT).max_await_time_ms( |
| 224 | + 10).max_await_time_ms(90) |
| 225 | + self.assertEqual(90, cursor._Cursor__max_await_time_ms) |
| 226 | + |
| 227 | + listener = EventListener() |
| 228 | + saved_listeners = monitoring._LISTENERS |
| 229 | + monitoring._LISTENERS = monitoring._Listeners([]) |
| 230 | + coll = single_client( |
| 231 | + event_listeners=[listener])[self.db.name].pymongo_test |
| 232 | + results = listener.results |
| 233 | + |
| 234 | + try: |
| 235 | + # Tailable_await defaults. |
| 236 | + list(coll.find(cursor_type=CursorType.TAILABLE_AWAIT)) |
| 237 | + # find |
| 238 | + self.assertFalse('maxTimeMS' in results['started'][0].command) |
| 239 | + # getMore |
| 240 | + self.assertFalse('maxTimeMS' in results['started'][1].command) |
| 241 | + results.clear() |
| 242 | + |
| 243 | + # Tailable_await with max_await_time_ms set. |
| 244 | + list(coll.find( |
| 245 | + cursor_type=CursorType.TAILABLE_AWAIT).max_await_time_ms(99)) |
| 246 | + # find |
| 247 | + self.assertFalse('maxTimeMS' in results['started'][0].command) |
| 248 | + # getMore |
| 249 | + self.assertTrue('maxTimeMS' in results['started'][1].command) |
| 250 | + self.assertEqual(99, results['started'][1].command['maxTimeMS']) |
| 251 | + results.clear() |
| 252 | + |
| 253 | + # Tailable_await with max_time_ms |
| 254 | + list(coll.find( |
| 255 | + cursor_type=CursorType.TAILABLE_AWAIT).max_time_ms(1)) |
| 256 | + # find |
| 257 | + self.assertTrue('maxTimeMS' in results['started'][0].command) |
| 258 | + self.assertEqual(1, results['started'][0].command['maxTimeMS']) |
| 259 | + # getMore |
| 260 | + self.assertFalse('maxTimeMS' in results['started'][1].command) |
| 261 | + results.clear() |
| 262 | + |
| 263 | + # Tailable_await with both max_time_ms and max_await_time_ms |
| 264 | + list(coll.find( |
| 265 | + cursor_type=CursorType.TAILABLE_AWAIT).max_time_ms( |
| 266 | + 1).max_await_time_ms(99)) |
| 267 | + # find |
| 268 | + self.assertTrue('maxTimeMS' in results['started'][0].command) |
| 269 | + self.assertEqual(1, results['started'][0].command['maxTimeMS']) |
| 270 | + # getMore |
| 271 | + self.assertTrue('maxTimeMS' in results['started'][1].command) |
| 272 | + self.assertEqual(99, results['started'][1].command['maxTimeMS']) |
| 273 | + results.clear() |
| 274 | + |
| 275 | + # Non tailable_await with max_await_time_ms |
| 276 | + list(coll.find(batch_size=1).max_await_time_ms(99)) |
| 277 | + # find |
| 278 | + self.assertFalse('maxTimeMS' in results['started'][0].command) |
| 279 | + # getMore |
| 280 | + self.assertFalse('maxTimeMS' in results['started'][1].command) |
| 281 | + results.clear() |
| 282 | + |
| 283 | + # Non tailable_await with max_time_ms |
| 284 | + list(coll.find(batch_size=1).max_time_ms(99)) |
| 285 | + # find |
| 286 | + self.assertTrue('maxTimeMS' in results['started'][0].command) |
| 287 | + self.assertEqual(99, results['started'][0].command['maxTimeMS']) |
| 288 | + # getMore |
| 289 | + self.assertFalse('maxTimeMS' in results['started'][1].command) |
| 290 | + |
| 291 | + # Non tailable_await with both max_time_ms and max_await_time_ms |
| 292 | + list(coll.find(batch_size=1).max_time_ms(99).max_await_time_ms(88)) |
| 293 | + # find |
| 294 | + self.assertTrue('maxTimeMS' in results['started'][0].command) |
| 295 | + self.assertEqual(99, results['started'][0].command['maxTimeMS']) |
| 296 | + # getMore |
| 297 | + self.assertFalse('maxTimeMS' in results['started'][1].command) |
| 298 | + |
| 299 | + finally: |
| 300 | + monitoring._LISTENERS = saved_listeners |
| 301 | + |
193 | 302 | @client_context.require_version_min(2, 5, 3, -1) |
194 | 303 | @client_context.require_test_commands |
195 | 304 | def test_max_time_ms_getmore(self): |
|
0 commit comments