|
6 | 6 |
|
7 | 7 |
|
8 | 8 | import asyncio |
| 9 | +import contextlib |
| 10 | +from unittest import mock |
9 | 11 |
|
10 | 12 | import asyncpg |
| 13 | +from asyncpg import connect_utils |
11 | 14 | from asyncpg import connection as pg_connection |
12 | 15 | from asyncpg import _testbase as tb |
13 | 16 |
|
@@ -152,3 +155,141 @@ async def test_timeout_covers_prepare_01(self): |
152 | 155 | with self.assertRaises(asyncio.TimeoutError): |
153 | 156 | meth = getattr(self.con, methname) |
154 | 157 | await meth('select pg_sleep($1)', 0.2) |
| 158 | + |
| 159 | + |
| 160 | +class TestCloseTimeoutPendingCancel(tb.ClusterTestCase): |
| 161 | + |
| 162 | + @contextlib.asynccontextmanager |
| 163 | + async def pending_cancel(self, *, cancel_sent, command_timeout=None): |
| 164 | + con = await self.connect(command_timeout=command_timeout) |
| 165 | + cancel_started = asyncio.Event() |
| 166 | + |
| 167 | + async def cancel(**kwargs): |
| 168 | + cancel_started.set() |
| 169 | + if not cancel_sent: |
| 170 | + await self.loop.create_future() |
| 171 | + |
| 172 | + # Exercise the actual Cython futures: either the cancel connection |
| 173 | + # stalls, or it closes without the query receiving ReadyForQuery. |
| 174 | + with mock.patch.object(connect_utils, '_cancel', cancel): |
| 175 | + cancellations = [] |
| 176 | + try: |
| 177 | + with self.assertRaises(asyncio.TimeoutError): |
| 178 | + await con.execute('select pg_sleep(10)', timeout=0.01) |
| 179 | + await cancel_started.wait() |
| 180 | + cancellations = list(con._cancellations) |
| 181 | + self.assertTrue(con._protocol._is_cancelling()) |
| 182 | + yield con |
| 183 | + finally: |
| 184 | + con.terminate() |
| 185 | + con._transport.abort() |
| 186 | + await asyncio.gather(*cancellations, return_exceptions=True) |
| 187 | + |
| 188 | + async def test_close_times_out_pending_cancel(self): |
| 189 | + for cancel_sent in (False, True): |
| 190 | + with self.subTest(cancel_sent=cancel_sent): |
| 191 | + async with self.pending_cancel(cancel_sent=cancel_sent) as con: |
| 192 | + proto = con._protocol |
| 193 | + with self.assertRaises(asyncio.TimeoutError), \ |
| 194 | + self.assertRunUnder(MAX_RUNTIME): |
| 195 | + await con.close(timeout=0.05) |
| 196 | + self.assertTrue(con._transport.is_closing()) |
| 197 | + self.assertFalse(proto._is_cancelling()) |
| 198 | + |
| 199 | + async def test_close_uses_command_timeout(self): |
| 200 | + async with self.pending_cancel( |
| 201 | + cancel_sent=False, command_timeout=0.05) as con: |
| 202 | + with self.assertRaises(asyncio.TimeoutError), \ |
| 203 | + self.assertRunUnder(MAX_RUNTIME): |
| 204 | + await con.close() |
| 205 | + self.assertTrue(con._transport.is_closing()) |
| 206 | + |
| 207 | + async def test_cancel_close_aborts_transport(self): |
| 208 | + for cancel_sent in (False, True): |
| 209 | + with self.subTest(cancel_sent=cancel_sent): |
| 210 | + async with self.pending_cancel(cancel_sent=cancel_sent) as con: |
| 211 | + task = self.loop.create_task(con.close()) |
| 212 | + await asyncio.sleep(0) |
| 213 | + task.cancel() |
| 214 | + with self.assertRaises(asyncio.CancelledError): |
| 215 | + await task |
| 216 | + self.assertTrue(con._transport.is_closing()) |
| 217 | + |
| 218 | + async def test_connection_lost_during_close(self): |
| 219 | + for cancel_sent in (False, True): |
| 220 | + for timeout in (None, 0.05): |
| 221 | + with self.subTest(cancel_sent=cancel_sent, timeout=timeout): |
| 222 | + async with self.pending_cancel( |
| 223 | + cancel_sent=cancel_sent) as con: |
| 224 | + task = self.loop.create_task( |
| 225 | + con.close(timeout=timeout)) |
| 226 | + await asyncio.sleep(0) |
| 227 | + con._transport.abort() |
| 228 | + await asyncio.wait_for(task, MAX_RUNTIME) |
| 229 | + self.assertTrue(con.is_closed()) |
| 230 | + self.assertFalse(con._protocol._is_cancelling()) |
| 231 | + # A lost transport must not leave a new shutdown |
| 232 | + # timer that attempts another cancel after cleanup. |
| 233 | + await asyncio.sleep(0.1) |
| 234 | + self.assertFalse(con._cancellations) |
| 235 | + |
| 236 | + async def test_close_has_one_timeout_budget(self): |
| 237 | + terminate_sent = asyncio.Event() |
| 238 | + |
| 239 | + class NoTerminateProtocol(connect_utils.protocol.Protocol): |
| 240 | + def connection_made(self, transport): |
| 241 | + def write(data): |
| 242 | + if bytes(data) == b'X\x00\x00\x00\x04': |
| 243 | + terminate_sent.set() |
| 244 | + else: |
| 245 | + transport.write(data) |
| 246 | + |
| 247 | + # Keep the server connected after Terminate. Pausing reads |
| 248 | + # does not suppress disconnects on Windows' Proactor loop. |
| 249 | + wrapped = mock.Mock(wraps=transport) |
| 250 | + wrapped.write.side_effect = write |
| 251 | + super().connection_made(wrapped) |
| 252 | + |
| 253 | + with mock.patch.object(connect_utils.protocol, 'Protocol', |
| 254 | + NoTerminateProtocol): |
| 255 | + con = await self.connect() |
| 256 | + proto = con._protocol |
| 257 | + drain_cancels = proto._drain_cancels |
| 258 | + |
| 259 | + async def delayed_drain(): |
| 260 | + await asyncio.sleep(0.3) |
| 261 | + await drain_cancels() |
| 262 | + |
| 263 | + try: |
| 264 | + with self.assertRaises(asyncio.TimeoutError): |
| 265 | + await con.execute('select pg_sleep(10)', timeout=0.01) |
| 266 | + with mock.patch.object(proto, '_drain_cancels', delayed_drain): |
| 267 | + with self.assertRaises(asyncio.TimeoutError), \ |
| 268 | + self.assertRunUnder(0.7): |
| 269 | + await con.close(timeout=0.5) |
| 270 | + self.assertTrue(terminate_sent.is_set()) |
| 271 | + self.assertTrue(con._transport.is_closing()) |
| 272 | + finally: |
| 273 | + con.terminate() |
| 274 | + con._transport.abort() |
| 275 | + |
| 276 | + async def test_close_timeout_resolves_running_query(self): |
| 277 | + con = await self.connect() |
| 278 | + |
| 279 | + async def cancel(**kwargs): |
| 280 | + await self.loop.create_future() |
| 281 | + |
| 282 | + with mock.patch.object(connect_utils, '_cancel', cancel): |
| 283 | + task = self.loop.create_task(con.execute('select pg_sleep(10)')) |
| 284 | + try: |
| 285 | + await asyncio.sleep(0) |
| 286 | + with self.assertRaises(asyncio.TimeoutError): |
| 287 | + await con.close(timeout=0.05) |
| 288 | + with self.assertRaises(asyncpg.ConnectionDoesNotExistError): |
| 289 | + await asyncio.wait_for(task, MAX_RUNTIME) |
| 290 | + self.assertTrue(con._transport.is_closing()) |
| 291 | + finally: |
| 292 | + con.terminate() |
| 293 | + con._transport.abort() |
| 294 | + task.cancel() |
| 295 | + await asyncio.gather(task, return_exceptions=True) |
0 commit comments