Skip to content

Commit da59318

Browse files
authored
PYTHON-4610 More robust to_list tests (mongodb#1773)
1 parent d08fec6 commit da59318

File tree

2 files changed

+24
-28
lines changed

2 files changed

+24
-28
lines changed

test/asynchronous/test_cursor.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,41 +1380,39 @@ async def test_getMore_does_not_send_readPreference(self):
13801380
self.assertEqual("getMore", started[1].command_name)
13811381
self.assertNotIn("$readPreference", started[1].command)
13821382

1383+
@async_client_context.require_version_min(4, 0)
13831384
@async_client_context.require_replica_set
13841385
async def test_to_list_tailable(self):
13851386
oplog = self.client.local.oplog.rs
13861387
last = await oplog.find().sort("$natural", pymongo.DESCENDING).limit(-1).next()
13871388
ts = last["ts"]
1388-
1389+
# Set maxAwaitTimeMS=1 to speed up the test and avoid blocking on the noop writer.
13891390
c = oplog.find(
13901391
{"ts": {"$gte": ts}}, cursor_type=pymongo.CursorType.TAILABLE_AWAIT, oplog_replay=True
1391-
)
1392-
1392+
).max_await_time_ms(1)
1393+
self.addAsyncCleanup(c.close)
13931394
docs = await c.to_list()
1394-
13951395
self.assertGreaterEqual(len(docs), 1)
13961396

13971397
async def test_to_list_empty(self):
13981398
c = self.db.does_not_exist.find()
1399-
14001399
docs = await c.to_list()
1401-
14021400
self.assertEqual([], docs)
14031401

1404-
@async_client_context.require_replica_set
1402+
@async_client_context.require_change_streams
14051403
async def test_command_cursor_to_list(self):
1406-
c = await self.db.test.aggregate([{"$changeStream": {}}])
1407-
1404+
# Set maxAwaitTimeMS=1 to speed up the test.
1405+
c = await self.db.test.aggregate([{"$changeStream": {}}], maxAwaitTimeMS=1)
1406+
self.addAsyncCleanup(c.close)
14081407
docs = await c.to_list()
1409-
14101408
self.assertGreaterEqual(len(docs), 0)
14111409

1412-
@async_client_context.require_replica_set
1410+
@async_client_context.require_change_streams
14131411
async def test_command_cursor_to_list_empty(self):
1414-
c = await self.db.does_not_exist.aggregate([{"$changeStream": {}}])
1415-
1412+
# Set maxAwaitTimeMS=1 to speed up the test.
1413+
c = await self.db.does_not_exist.aggregate([{"$changeStream": {}}], maxAwaitTimeMS=1)
1414+
self.addAsyncCleanup(c.close)
14161415
docs = await c.to_list()
1417-
14181416
self.assertEqual([], docs)
14191417

14201418

test/test_cursor.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,41 +1371,39 @@ def test_getMore_does_not_send_readPreference(self):
13711371
self.assertEqual("getMore", started[1].command_name)
13721372
self.assertNotIn("$readPreference", started[1].command)
13731373

1374+
@client_context.require_version_min(4, 0)
13741375
@client_context.require_replica_set
13751376
def test_to_list_tailable(self):
13761377
oplog = self.client.local.oplog.rs
13771378
last = oplog.find().sort("$natural", pymongo.DESCENDING).limit(-1).next()
13781379
ts = last["ts"]
1379-
1380+
# Set maxAwaitTimeMS=1 to speed up the test and avoid blocking on the noop writer.
13801381
c = oplog.find(
13811382
{"ts": {"$gte": ts}}, cursor_type=pymongo.CursorType.TAILABLE_AWAIT, oplog_replay=True
1382-
)
1383-
1383+
).max_await_time_ms(1)
1384+
self.addCleanup(c.close)
13841385
docs = c.to_list()
1385-
13861386
self.assertGreaterEqual(len(docs), 1)
13871387

13881388
def test_to_list_empty(self):
13891389
c = self.db.does_not_exist.find()
1390-
13911390
docs = c.to_list()
1392-
13931391
self.assertEqual([], docs)
13941392

1395-
@client_context.require_replica_set
1393+
@client_context.require_change_streams
13961394
def test_command_cursor_to_list(self):
1397-
c = self.db.test.aggregate([{"$changeStream": {}}])
1398-
1395+
# Set maxAwaitTimeMS=1 to speed up the test.
1396+
c = self.db.test.aggregate([{"$changeStream": {}}], maxAwaitTimeMS=1)
1397+
self.addCleanup(c.close)
13991398
docs = c.to_list()
1400-
14011399
self.assertGreaterEqual(len(docs), 0)
14021400

1403-
@client_context.require_replica_set
1401+
@client_context.require_change_streams
14041402
def test_command_cursor_to_list_empty(self):
1405-
c = self.db.does_not_exist.aggregate([{"$changeStream": {}}])
1406-
1403+
# Set maxAwaitTimeMS=1 to speed up the test.
1404+
c = self.db.does_not_exist.aggregate([{"$changeStream": {}}], maxAwaitTimeMS=1)
1405+
self.addCleanup(c.close)
14071406
docs = c.to_list()
1408-
14091407
self.assertEqual([], docs)
14101408

14111409

0 commit comments

Comments
 (0)