Forward the lab 4 foundation and migrate the lab 5 pipeline to SQLAlchemy
This commit is contained in:
@@ -3,12 +3,11 @@ from collections.abc import AsyncIterator
|
||||
from typing import Any
|
||||
from uuid import uuid4
|
||||
|
||||
import asyncpg
|
||||
from httpx import AsyncClient
|
||||
from starlette.types import Message
|
||||
from test_attachments import storage, trio, upload
|
||||
|
||||
from conftest import APPS, DATABASE, actor, keyed, resolve, running_app, scalar
|
||||
from conftest import APPS, actor, keyed, resolve, running_app, scalar, sql_connection
|
||||
from messenger.attachments.domain.events import AttachmentDiscarded
|
||||
from messenger.attachments.entrypoints.sweeper import UploadSweeper
|
||||
from messenger.shared.application.messagebus import EventBus
|
||||
@@ -19,27 +18,27 @@ AGED = "UPDATE attachments SET created_at = created_at - interval '1 hour' WHERE
|
||||
|
||||
|
||||
async def execute(sql: str, *args: Any) -> None:
|
||||
connection = await asyncpg.connect(DATABASE["url"])
|
||||
try:
|
||||
await connection.execute(sql, *args)
|
||||
finally:
|
||||
await connection.close()
|
||||
async with sql_connection() as connection:
|
||||
await connection.exec_driver_sql(sql, args)
|
||||
|
||||
|
||||
def sweeper(http: AsyncClient) -> UploadSweeper:
|
||||
return APPS[id(http)].state.sweeper
|
||||
|
||||
|
||||
async def discarded(http: AsyncClient) -> list[AttachmentDiscarded]:
|
||||
seen: list[AttachmentDiscarded] = []
|
||||
|
||||
async def keep(event: AttachmentDiscarded) -> None:
|
||||
seen.append(event)
|
||||
|
||||
(await resolve(http, EventBus)).project(AttachmentDiscarded, keep)
|
||||
async def discarded(http: AsyncClient) -> asyncio.Queue[AttachmentDiscarded]:
|
||||
seen: asyncio.Queue[AttachmentDiscarded] = asyncio.Queue()
|
||||
(await resolve(http, EventBus)).subscribe(AttachmentDiscarded, seen.put)
|
||||
return seen
|
||||
|
||||
|
||||
async def delivered(seen: asyncio.Queue[AttachmentDiscarded], count: int) -> list[str]:
|
||||
async with asyncio.timeout(2):
|
||||
events = [await seen.get() for _ in range(count)]
|
||||
assert seen.empty()
|
||||
return sorted(f"{event.reason} {event.attachment_id}" for event in events)
|
||||
|
||||
|
||||
async def intent(http: AsyncClient, user: dict[str, Any], chat: dict[str, Any]) -> str:
|
||||
store = await storage(http)
|
||||
store.failing = True
|
||||
@@ -67,15 +66,18 @@ async def test_commit_failure_after_put_is_compensated(client: AsyncClient) -> N
|
||||
trigger = f"lab4_fail_{uuid4().hex[:8]}"
|
||||
await execute(
|
||||
f"""CREATE FUNCTION {trigger}() RETURNS trigger LANGUAGE plpgsql AS $$
|
||||
BEGIN RAISE EXCEPTION 'injected' USING ERRCODE = '55P03'; END $$;
|
||||
CREATE TRIGGER {trigger} BEFORE UPDATE ON attachments FOR EACH ROW
|
||||
BEGIN RAISE EXCEPTION 'injected' USING ERRCODE = '55P03'; END $$"""
|
||||
)
|
||||
await execute(
|
||||
f"""CREATE TRIGGER {trigger} BEFORE UPDATE ON attachments FOR EACH ROW
|
||||
WHEN (NEW.filename = '{name}' AND NEW.status = 'queued') EXECUTE FUNCTION {trigger}()"""
|
||||
)
|
||||
before, seen = set(store.objects), await discarded(client)
|
||||
try:
|
||||
response = await upload(client, alice, chat, filename=name)
|
||||
finally:
|
||||
await execute(f"DROP TRIGGER {trigger} ON attachments; DROP FUNCTION {trigger}()")
|
||||
await execute(f"DROP TRIGGER {trigger} ON attachments")
|
||||
await execute(f"DROP FUNCTION {trigger}()")
|
||||
assert response.status_code == 503
|
||||
assert response.json()["error"]["code"] == "unavailable"
|
||||
assert await scalar("SELECT count(*) FROM attachments WHERE filename = $1", name) == 0
|
||||
@@ -88,7 +90,7 @@ async def test_commit_failure_after_put_is_compensated(client: AsyncClient) -> N
|
||||
== 0
|
||||
)
|
||||
assert set(store.objects) == before
|
||||
assert [event.reason for event in seen] == ["compensation"]
|
||||
assert [event.split()[0] for event in await delivered(seen, 1)] == ["compensation"]
|
||||
|
||||
|
||||
async def test_lost_intent_gives_503_and_no_object(client: AsyncClient) -> None:
|
||||
@@ -152,8 +154,7 @@ async def test_parallel_sweeps_share_the_work(client: AsyncClient) -> None:
|
||||
seen = await discarded(client)
|
||||
first, second = await asyncio.gather(sweeper(client).sweep(), sweeper(client).sweep())
|
||||
assert first + second == 5
|
||||
assert sorted(str(event.attachment_id) for event in seen) == sorted(pending)
|
||||
assert {event.reason for event in seen} == {"abandoned"}
|
||||
assert await delivered(seen, 5) == sorted(f"abandoned {item}" for item in pending)
|
||||
assert await sweeper(client).sweep() == 0
|
||||
objects = (await storage(client)).objects
|
||||
assert not any(f"staging/{attachment_id}" in objects for attachment_id in pending)
|
||||
|
||||
Reference in New Issue
Block a user