Skip to content

Commit d09adc8

Browse files
committed
PYTHON-4851 - Convert test.test_csot to async
1 parent 6973d2d commit d09adc8

File tree

5 files changed

+125
-4
lines changed

5 files changed

+125
-4
lines changed

test/asynchronous/test_csot.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Copyright 2022-present MongoDB, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Test the CSOT unified spec tests."""
16+
from __future__ import annotations
17+
18+
import os
19+
import pathlib
20+
import sys
21+
22+
sys.path[0:0] = [""]
23+
24+
from test.asynchronous import AsyncIntegrationTest, async_client_context, unittest
25+
from test.asynchronous.unified_format import generate_test_classes
26+
27+
import pymongo
28+
from pymongo import _csot
29+
from pymongo.errors import PyMongoError
30+
31+
_IS_SYNC = False
32+
33+
# Location of JSON test specifications.
34+
if _IS_SYNC:
35+
_TEST_PATH = os.path.join(pathlib.Path(__file__).resolve().parent, "csot")
36+
else:
37+
_TEST_PATH = os.path.join(pathlib.Path(__file__).resolve().parent.parent, "csot")
38+
39+
# Generate unified tests.
40+
globals().update(generate_test_classes(_TEST_PATH, module=__name__))
41+
42+
43+
class TestCSOT(AsyncIntegrationTest):
44+
RUN_ON_SERVERLESS = True
45+
RUN_ON_LOAD_BALANCER = True
46+
47+
async def test_timeout_nested(self):
48+
coll = self.db.coll
49+
self.assertEqual(_csot.get_timeout(), None)
50+
self.assertEqual(_csot.get_deadline(), float("inf"))
51+
self.assertEqual(_csot.get_rtt(), 0.0)
52+
with pymongo.timeout(10):
53+
await coll.find_one()
54+
self.assertEqual(_csot.get_timeout(), 10)
55+
deadline_10 = _csot.get_deadline()
56+
57+
# Capped at the original 10 deadline.
58+
with pymongo.timeout(15):
59+
await coll.find_one()
60+
self.assertEqual(_csot.get_timeout(), 15)
61+
self.assertEqual(_csot.get_deadline(), deadline_10)
62+
63+
# Should be reset to previous values
64+
self.assertEqual(_csot.get_timeout(), 10)
65+
self.assertEqual(_csot.get_deadline(), deadline_10)
66+
await coll.find_one()
67+
68+
with pymongo.timeout(5):
69+
await coll.find_one()
70+
self.assertEqual(_csot.get_timeout(), 5)
71+
self.assertLess(_csot.get_deadline(), deadline_10)
72+
73+
# Should be reset to previous values
74+
self.assertEqual(_csot.get_timeout(), 10)
75+
self.assertEqual(_csot.get_deadline(), deadline_10)
76+
await coll.find_one()
77+
78+
# Should be reset to previous values
79+
self.assertEqual(_csot.get_timeout(), None)
80+
self.assertEqual(_csot.get_deadline(), float("inf"))
81+
self.assertEqual(_csot.get_rtt(), 0.0)
82+
83+
@async_client_context.require_change_streams
84+
async def test_change_stream_can_resume_after_timeouts(self):
85+
coll = self.db.test
86+
await coll.insert_one({})
87+
async with await coll.watch() as stream:
88+
with pymongo.timeout(0.1):
89+
with self.assertRaises(PyMongoError) as ctx:
90+
await stream.next()
91+
self.assertTrue(ctx.exception.timeout)
92+
self.assertTrue(stream.alive)
93+
with self.assertRaises(PyMongoError) as ctx:
94+
await stream.try_next()
95+
self.assertTrue(ctx.exception.timeout)
96+
self.assertTrue(stream.alive)
97+
# Resume before the insert on 3.6 because 4.0 is required to avoid skipping documents
98+
if async_client_context.version < (4, 0):
99+
await stream.try_next()
100+
await coll.insert_one({})
101+
with pymongo.timeout(10):
102+
self.assertTrue(await stream.next())
103+
self.assertTrue(stream.alive)
104+
# Timeout applies to entire next() call, not only individual commands.
105+
with pymongo.timeout(0.5):
106+
with self.assertRaises(PyMongoError) as ctx:
107+
await stream.next()
108+
self.assertTrue(ctx.exception.timeout)
109+
self.assertTrue(stream.alive)
110+
self.assertFalse(stream.alive)
111+
112+
113+
if __name__ == "__main__":
114+
unittest.main()

test/asynchronous/unified_format.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ async def _databaseOperation_listCollections(self, target, *args, **kwargs):
767767
if "batch_size" in kwargs:
768768
kwargs["cursor"] = {"batchSize": kwargs.pop("batch_size")}
769769
cursor = await target.list_collections(*args, **kwargs)
770-
return list(cursor)
770+
return await cursor.to_list()
771771

772772
async def _databaseOperation_createCollection(self, target, *args, **kwargs):
773773
# PYTHON-1936 Ignore the listCollections event from create_collection.

test/test_csot.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from __future__ import annotations
1717

1818
import os
19+
import pathlib
1920
import sys
2021

2122
sys.path[0:0] = [""]
@@ -27,11 +28,16 @@
2728
from pymongo import _csot
2829
from pymongo.errors import PyMongoError
2930

31+
_IS_SYNC = True
32+
3033
# Location of JSON test specifications.
31-
TEST_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), "csot")
34+
if _IS_SYNC:
35+
_TEST_PATH = os.path.join(pathlib.Path(__file__).resolve().parent, "csot")
36+
else:
37+
_TEST_PATH = os.path.join(pathlib.Path(__file__).resolve().parent.parent, "csot")
3238

3339
# Generate unified tests.
34-
globals().update(generate_test_classes(TEST_PATH, module=__name__))
40+
globals().update(generate_test_classes(_TEST_PATH, module=__name__))
3541

3642

3743
class TestCSOT(IntegrationTest):

test/unified_format.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ def _databaseOperation_listCollections(self, target, *args, **kwargs):
763763
if "batch_size" in kwargs:
764764
kwargs["cursor"] = {"batchSize": kwargs.pop("batch_size")}
765765
cursor = target.list_collections(*args, **kwargs)
766-
return list(cursor)
766+
return cursor.to_list()
767767

768768
def _databaseOperation_createCollection(self, target, *args, **kwargs):
769769
# PYTHON-1936 Ignore the listCollections event from create_collection.

tools/synchro.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ def async_only_test(f: str) -> bool:
194194
"test_collection.py",
195195
"test_common.py",
196196
"test_connections_survive_primary_stepdown_spec.py",
197+
"test_csot.py",
197198
"test_cursor.py",
198199
"test_database.py",
199200
"test_encryption.py",

0 commit comments

Comments
 (0)