|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | | -"""Test that async cancellation performed by users raises the expected error.""" |
| 15 | +"""Test that async cancellation performed by users clean up resources correctly.""" |
16 | 16 | from __future__ import annotations |
17 | 17 |
|
18 | 18 | import asyncio |
@@ -71,7 +71,7 @@ async def task(): |
71 | 71 |
|
72 | 72 | self.assertFalse(session.in_transaction) |
73 | 73 |
|
74 | | - async def test_async_cancellation_kills_cursor(self): |
| 74 | + async def test_async_cancellation_closes_cursor(self): |
75 | 75 | client = await self.async_rs_or_single_client() |
76 | 76 | await connected(client) |
77 | 77 | for _ in range(2): |
@@ -101,3 +101,33 @@ async def task(): |
101 | 101 | await task |
102 | 102 |
|
103 | 103 | self.assertTrue(cursor._killed) |
| 104 | + |
| 105 | + async def test_async_cancellation_closes_change_stream(self): |
| 106 | + client = await self.async_rs_or_single_client() |
| 107 | + await connected(client) |
| 108 | + self.addAsyncCleanup(client.db.test.drop) |
| 109 | + |
| 110 | + change_stream = await client.db.test.watch(batch_size=2) |
| 111 | + |
| 112 | + # Make sure getMore commands block |
| 113 | + fail_command = { |
| 114 | + "configureFailPoint": "failCommand", |
| 115 | + "mode": "alwaysOn", |
| 116 | + "data": {"failCommands": ["getMore"], "blockConnection": True, "blockTimeMS": 200}, |
| 117 | + } |
| 118 | + |
| 119 | + async def task(): |
| 120 | + async with self.fail_point(fail_command): |
| 121 | + for _ in range(2): |
| 122 | + await client.db.test.insert_one({"x": 1}) |
| 123 | + await change_stream.next() |
| 124 | + |
| 125 | + task = asyncio.create_task(task()) |
| 126 | + |
| 127 | + await asyncio.sleep(0.1) |
| 128 | + |
| 129 | + task.cancel() |
| 130 | + with self.assertRaises(asyncio.CancelledError): |
| 131 | + await task |
| 132 | + |
| 133 | + self.assertTrue(change_stream._closed) |
0 commit comments