Skip to content

Commit 0423d60

Browse files
committed
WIP
1 parent 78724cd commit 0423d60

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
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 sys
20+
21+
sys.path[0:0] = [""]
22+
23+
from test.asynchronous import AsyncIntegrationTest, async_client_context, unittest
24+
from test.unified_format import generate_test_classes
25+
26+
import pymongo
27+
from pymongo import _csot
28+
from pymongo.errors import PyMongoError
29+
30+
_IS_SYNC = False
31+
32+
# Location of JSON test specifications.
33+
TEST_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), "csot")
34+
35+
# Generate unified tests.
36+
globals().update(generate_test_classes(TEST_PATH, module=__name__))
37+
38+
39+
class TestCSOT(AsyncIntegrationTest):
40+
RUN_ON_SERVERLESS = True
41+
RUN_ON_LOAD_BALANCER = True
42+
43+
async def test_timeout_nested(self):
44+
if os.environ.get("SKIP_CSOT_TESTS", ""):
45+
raise unittest.SkipTest("SKIP_CSOT_TESTS is set, skipping...")
46+
coll = self.db.coll
47+
self.assertEqual(_csot.get_timeout(), None)
48+
self.assertEqual(_csot.get_deadline(), float("inf"))
49+
self.assertEqual(_csot.get_rtt(), 0.0)
50+
with pymongo.timeout(10):
51+
await coll.find_one()
52+
self.assertEqual(_csot.get_timeout(), 10)
53+
deadline_10 = _csot.get_deadline()
54+
55+
# Capped at the original 10 deadline.
56+
with pymongo.timeout(15):
57+
await coll.find_one()
58+
self.assertEqual(_csot.get_timeout(), 15)
59+
self.assertEqual(_csot.get_deadline(), deadline_10)
60+
61+
# Should be reset to previous values
62+
self.assertEqual(_csot.get_timeout(), 10)
63+
self.assertEqual(_csot.get_deadline(), deadline_10)
64+
await coll.find_one()
65+
66+
with pymongo.timeout(5):
67+
await coll.find_one()
68+
self.assertEqual(_csot.get_timeout(), 5)
69+
self.assertLess(_csot.get_deadline(), deadline_10)
70+
71+
# Should be reset to previous values
72+
self.assertEqual(_csot.get_timeout(), 10)
73+
self.assertEqual(_csot.get_deadline(), deadline_10)
74+
await coll.find_one()
75+
76+
# Should be reset to previous values
77+
self.assertEqual(_csot.get_timeout(), None)
78+
self.assertEqual(_csot.get_deadline(), float("inf"))
79+
self.assertEqual(_csot.get_rtt(), 0.0)
80+
81+
@async_client_context.require_change_streams
82+
async def test_change_stream_can_resume_after_timeouts(self):
83+
if os.environ.get("SKIP_CSOT_TESTS", ""):
84+
raise unittest.SkipTest("SKIP_CSOT_TESTS is set, skipping...")
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(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/test_csot.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
from pymongo import _csot
2828
from pymongo.errors import PyMongoError
2929

30+
_IS_SYNC = True
31+
3032
# Location of JSON test specifications.
3133
TEST_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), "csot")
3234

tools/synchro.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ def async_only_test(f: str) -> bool:
201201
"test_connections_survive_primary_stepdown_spec.py",
202202
"test_create_entities.py",
203203
"test_crud_unified.py",
204+
"test_csot.py",
204205
"test_cursor.py",
205206
"test_database.py",
206207
"test_encryption.py",

0 commit comments

Comments
 (0)