Skip to content

Commit 2601054

Browse files
committed
PYTHON-4745 - Document and Test Behavior when User Cancels Async Operation
1 parent 8ae9a04 commit 2601054

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright 2025-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 that async cancellation performed by users raises the expected error."""
16+
from __future__ import annotations
17+
18+
import asyncio
19+
import sys
20+
21+
sys.path[0:0] = [""]
22+
23+
from test.asynchronous import AsyncIntegrationTest
24+
25+
26+
class TestAsyncCancellation(AsyncIntegrationTest):
27+
async def test_async_cancellation(self):
28+
async def task():
29+
while True:
30+
await self.client.db.test.insert_one({"x": 1})
31+
await asyncio.sleep(0.005)
32+
33+
task = asyncio.create_task(task())
34+
35+
# Make sure the task successfully runs a few operations to simulate a long-running user task
36+
await asyncio.sleep(0.01)
37+
38+
task.cancel()
39+
with self.assertRaises(asyncio.CancelledError):
40+
await task

tools/synchro.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@
168168

169169
def async_only_test(f: str) -> bool:
170170
"""Return True for async tests that should not be converted to sync."""
171-
return f in ["test_locks.py", "test_concurrency.py"]
171+
return f in ["test_locks.py", "test_concurrency.py", "test_async_cancellation.py"]
172172

173173

174174
test_files = [

0 commit comments

Comments
 (0)