|
| 1 | +import asyncio |
| 2 | + |
1 | 3 | import psycopg2
|
2 | 4 | import pytest
|
3 | 5 |
|
@@ -181,3 +183,61 @@ async def test_transaction_point_oldstyle(engine):
|
181 | 183 | (3, 'data')]
|
182 | 184 |
|
183 | 185 | await tr.commit()
|
| 186 | + |
| 187 | + |
| 188 | +async def test_timeout_in_transaction_context_manager(make_engine): |
| 189 | + engine = await make_engine(timeout=1) |
| 190 | + with pytest.raises(asyncio.TimeoutError): |
| 191 | + async with engine.acquire() as connection: |
| 192 | + async with Transaction(connection, IsolationLevel.read_committed): |
| 193 | + await connection.execute("SELECT pg_sleep(10)") |
| 194 | + |
| 195 | + engine.terminate() |
| 196 | + await engine.wait_closed() |
| 197 | + |
| 198 | + |
| 199 | +async def test_timeout_in_savepoint_context_manager(make_engine): |
| 200 | + engine = await make_engine(timeout=1) |
| 201 | + with pytest.raises(asyncio.TimeoutError): |
| 202 | + async with engine.acquire() as connection: |
| 203 | + async with Transaction( |
| 204 | + connection, IsolationLevel.read_committed |
| 205 | + ) as transaction: |
| 206 | + async with transaction.point(): |
| 207 | + await connection.execute("SELECT pg_sleep(10)") |
| 208 | + |
| 209 | + engine.terminate() |
| 210 | + await engine.wait_closed() |
| 211 | + |
| 212 | + |
| 213 | +async def test_cancel_in_transaction_context_manager(engine, loop): |
| 214 | + with pytest.raises(asyncio.CancelledError): |
| 215 | + async with engine.acquire() as connection: |
| 216 | + async with Transaction(connection, IsolationLevel.read_committed): |
| 217 | + task = loop.create_task( |
| 218 | + connection.execute("SELECT pg_sleep(10)")) |
| 219 | + |
| 220 | + async def cancel_soon(): |
| 221 | + await asyncio.sleep(1) |
| 222 | + task.cancel() |
| 223 | + |
| 224 | + loop.create_task(cancel_soon()) |
| 225 | + await task |
| 226 | + |
| 227 | + |
| 228 | +async def test_cancel_in_savepoint_context_manager(engine, loop): |
| 229 | + with pytest.raises(asyncio.CancelledError): |
| 230 | + async with engine.acquire() as connection: |
| 231 | + async with Transaction( |
| 232 | + connection, IsolationLevel.read_committed |
| 233 | + ) as transaction: |
| 234 | + async with transaction.point(): |
| 235 | + task = loop.create_task( |
| 236 | + connection.execute("SELECT pg_sleep(10)")) |
| 237 | + |
| 238 | + async def cancel_soon(): |
| 239 | + await asyncio.sleep(1) |
| 240 | + task.cancel() |
| 241 | + |
| 242 | + loop.create_task(cancel_soon()) |
| 243 | + await task |
0 commit comments