|
| 1 | +import asyncio |
| 2 | +import typing |
| 3 | +import uuid |
| 4 | + |
| 5 | +import sqlalchemy as sa |
| 6 | +from fastapi import status |
| 7 | + |
| 8 | +from app import models |
| 9 | + |
| 10 | + |
| 11 | +if typing.TYPE_CHECKING: |
| 12 | + from httpx import AsyncClient, Response |
| 13 | + from sqlalchemy.ext.asyncio import AsyncEngine |
| 14 | + |
| 15 | + |
| 16 | +async def wait_for_lock_waiters(engine: AsyncEngine, count: int, request: asyncio.Task[typing.Any]) -> None: |
| 17 | + statement: typing.Final = sa.text( |
| 18 | + "SELECT count(*) FROM pg_stat_activity WHERE datname = current_database() AND wait_event_type = 'Lock'" |
| 19 | + ) |
| 20 | + async with asyncio.timeout(5): |
| 21 | + while not request.done(): |
| 22 | + async with engine.connect() as connection: |
| 23 | + if await connection.scalar(statement) >= count: |
| 24 | + return |
| 25 | + await asyncio.sleep(0.01) |
| 26 | + |
| 27 | + |
| 28 | +async def test_concurrent_card_updates_in_opposite_orders_do_not_deadlock( |
| 29 | + client: AsyncClient, committing_engine: AsyncEngine |
| 30 | +) -> None: |
| 31 | + async with committing_engine.begin() as connection: |
| 32 | + deck_id = await connection.scalar( |
| 33 | + sa.insert(models.Deck).values(name="locking", idempotency_key=uuid.uuid4()).returning(models.Deck.id) |
| 34 | + ) |
| 35 | + card1, card2, card3 = ( |
| 36 | + await connection.scalars( |
| 37 | + sa.insert(models.Card) |
| 38 | + .values([{"deck_id": deck_id, "front": f"card{i}"} for i in range(1, 4)]) |
| 39 | + .returning(models.Card.id) |
| 40 | + ) |
| 41 | + ).all() |
| 42 | + url: typing.Final = f"/api/decks/{deck_id}/cards/" |
| 43 | + requests: typing.Final[list[asyncio.Task[Response]]] = [] |
| 44 | + try: |
| 45 | + async with committing_engine.connect() as blocker: |
| 46 | + await blocker.execute(sa.text("SELECT 1 FROM cards WHERE id = :id FOR UPDATE"), {"id": card3}) |
| 47 | + requests.append( |
| 48 | + asyncio.create_task( |
| 49 | + client.put( |
| 50 | + url, |
| 51 | + json=[ |
| 52 | + {"id": card1, "front": "a1"}, |
| 53 | + {"id": card3, "front": "a3"}, |
| 54 | + {"id": card2, "front": "a2"}, |
| 55 | + ], |
| 56 | + ) |
| 57 | + ) |
| 58 | + ) |
| 59 | + await wait_for_lock_waiters(committing_engine, 1, requests[0]) |
| 60 | + requests.append( |
| 61 | + asyncio.create_task(client.put(url, json=[{"id": card2, "front": "b2"}, {"id": card1, "front": "b1"}])) |
| 62 | + ) |
| 63 | + await wait_for_lock_waiters(committing_engine, 2, requests[1]) |
| 64 | + responses = await asyncio.gather(*requests) |
| 65 | + finally: |
| 66 | + for request in requests: |
| 67 | + request.cancel() |
| 68 | + await asyncio.gather(*requests, return_exceptions=True) |
| 69 | + async with committing_engine.begin() as connection: |
| 70 | + await connection.execute(sa.delete(models.Card).where(models.Card.deck_id == deck_id)) |
| 71 | + await connection.execute(sa.delete(models.Deck).where(models.Deck.id == deck_id)) |
| 72 | + |
| 73 | + assert [response.status_code for response in responses] == [status.HTTP_200_OK, status.HTTP_200_OK] |
0 commit comments