|
| 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 clean up resources correctly.""" |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +import asyncio |
| 19 | +import sys |
| 20 | +from test.utils import async_get_pool, delay, one |
| 21 | + |
| 22 | +sys.path[0:0] = [""] |
| 23 | + |
| 24 | +from test.asynchronous import AsyncIntegrationTest, async_client_context, connected |
| 25 | + |
| 26 | + |
| 27 | +class TestAsyncCancellation(AsyncIntegrationTest): |
| 28 | + async def test_async_cancellation_closes_connection(self): |
| 29 | + pool = await async_get_pool(self.client) |
| 30 | + await self.client.db.test.insert_one({"x": 1}) |
| 31 | + self.addAsyncCleanup(self.client.db.test.delete_many, {}) |
| 32 | + |
| 33 | + conn = one(pool.conns) |
| 34 | + |
| 35 | + async def task(): |
| 36 | + await self.client.db.test.find_one({"$where": delay(0.2)}) |
| 37 | + |
| 38 | + task = asyncio.create_task(task()) |
| 39 | + |
| 40 | + await asyncio.sleep(0.1) |
| 41 | + |
| 42 | + task.cancel() |
| 43 | + with self.assertRaises(asyncio.CancelledError): |
| 44 | + await task |
| 45 | + |
| 46 | + self.assertTrue(conn.closed) |
| 47 | + |
| 48 | + @async_client_context.require_transactions |
| 49 | + async def test_async_cancellation_aborts_transaction(self): |
| 50 | + await self.client.db.test.insert_one({"x": 1}) |
| 51 | + self.addAsyncCleanup(self.client.db.test.delete_many, {}) |
| 52 | + |
| 53 | + session = self.client.start_session() |
| 54 | + |
| 55 | + async def callback(session): |
| 56 | + await self.client.db.test.find_one({"$where": delay(0.2)}, session=session) |
| 57 | + |
| 58 | + async def task(): |
| 59 | + await session.with_transaction(callback) |
| 60 | + |
| 61 | + task = asyncio.create_task(task()) |
| 62 | + |
| 63 | + await asyncio.sleep(0.1) |
| 64 | + |
| 65 | + task.cancel() |
| 66 | + with self.assertRaises(asyncio.CancelledError): |
| 67 | + await task |
| 68 | + |
| 69 | + self.assertFalse(session.in_transaction) |
| 70 | + |
| 71 | + @async_client_context.require_failCommand_blockConnection |
| 72 | + async def test_async_cancellation_closes_cursor(self): |
| 73 | + await self.client.db.test.insert_many([{"x": 1}, {"x": 2}]) |
| 74 | + self.addAsyncCleanup(self.client.db.test.delete_many, {}) |
| 75 | + |
| 76 | + cursor = self.client.db.test.find({}, batch_size=1) |
| 77 | + await cursor.next() |
| 78 | + |
| 79 | + # Make sure getMore commands block |
| 80 | + fail_command = { |
| 81 | + "configureFailPoint": "failCommand", |
| 82 | + "mode": "alwaysOn", |
| 83 | + "data": {"failCommands": ["getMore"], "blockConnection": True, "blockTimeMS": 200}, |
| 84 | + } |
| 85 | + |
| 86 | + async def task(): |
| 87 | + async with self.fail_point(fail_command): |
| 88 | + await cursor.next() |
| 89 | + |
| 90 | + task = asyncio.create_task(task()) |
| 91 | + |
| 92 | + await asyncio.sleep(0.1) |
| 93 | + |
| 94 | + task.cancel() |
| 95 | + with self.assertRaises(asyncio.CancelledError): |
| 96 | + await task |
| 97 | + |
| 98 | + self.assertTrue(cursor._killed) |
| 99 | + |
| 100 | + @async_client_context.require_change_streams |
| 101 | + @async_client_context.require_failCommand_blockConnection |
| 102 | + async def test_async_cancellation_closes_change_stream(self): |
| 103 | + self.addAsyncCleanup(self.client.db.test.delete_many, {}) |
| 104 | + change_stream = await self.client.db.test.watch(batch_size=2) |
| 105 | + |
| 106 | + # Make sure getMore commands block |
| 107 | + fail_command = { |
| 108 | + "configureFailPoint": "failCommand", |
| 109 | + "mode": "alwaysOn", |
| 110 | + "data": {"failCommands": ["getMore"], "blockConnection": True, "blockTimeMS": 200}, |
| 111 | + } |
| 112 | + |
| 113 | + async def task(): |
| 114 | + async with self.fail_point(fail_command): |
| 115 | + await self.client.db.test.insert_many([{"x": 1}, {"x": 2}]) |
| 116 | + await change_stream.next() |
| 117 | + |
| 118 | + task = asyncio.create_task(task()) |
| 119 | + |
| 120 | + await asyncio.sleep(0.1) |
| 121 | + |
| 122 | + task.cancel() |
| 123 | + with self.assertRaises(asyncio.CancelledError): |
| 124 | + await task |
| 125 | + |
| 126 | + self.assertTrue(change_stream._closed) |
0 commit comments