66 lines
2.5 KiB
Python
66 lines
2.5 KiB
Python
import asyncio
|
|
from datetime import timedelta
|
|
from uuid import UUID
|
|
|
|
from httpx import AsyncClient
|
|
from opentelemetry.sdk.trace import TracerProvider
|
|
from opentelemetry.trace import SpanKind
|
|
from sqlalchemy import select
|
|
from test_pipeline import accepted, job_of
|
|
|
|
from conftest import sql_connection
|
|
from messenger.attachments.infrastructure.postgres import JobRow, OutboxRow, PostgresJobRepository
|
|
from messenger.shared.infrastructure.trace_context import capture_context, restored_context
|
|
from pipeline import pipeline_of
|
|
|
|
|
|
async def test_context_survives_outbox_delivery_and_lease_recovery(client: AsyncClient) -> None:
|
|
provider = TracerProvider(shutdown_on_exit=False)
|
|
tracer = provider.get_tracer("test")
|
|
with tracer.start_as_current_span("upload", kind=SpanKind.SERVER):
|
|
origin = capture_context()
|
|
_, _, attachment_id = await accepted(client)
|
|
job = await job_of(attachment_id)
|
|
async with sql_connection() as connection:
|
|
assert (
|
|
await connection.scalar(select(JobRow.trace_context).where(JobRow.job_id == job.id))
|
|
== origin
|
|
)
|
|
assert (
|
|
await connection.scalar(
|
|
select(OutboxRow.trace_context).where(OutboxRow.job_id == job.id)
|
|
)
|
|
== origin
|
|
)
|
|
pipeline = await pipeline_of(client)
|
|
await pipeline.publish()
|
|
assert pipeline.publisher.contexts == [origin]
|
|
uow = pipeline.uow()
|
|
async with uow.transaction():
|
|
claim = await uow.jobs.claim(job, UUID(int=1), timedelta(seconds=-1))
|
|
assert claim is not None
|
|
await uow.commit()
|
|
await pipeline.recover()
|
|
await asyncio.sleep(0.02)
|
|
await pipeline.publish()
|
|
assert len(pipeline.publisher.contexts) == 2
|
|
assert pipeline.publisher.contexts[-1] == origin
|
|
with restored_context(origin), tracer.start_as_current_span("retry", kind=SpanKind.CONSUMER):
|
|
retry = capture_context()
|
|
async with uow.transaction():
|
|
await PostgresJobRepository(uow.session)._enqueue(job)
|
|
await uow.commit()
|
|
async with sql_connection() as connection:
|
|
assert (
|
|
await connection.scalar(
|
|
select(OutboxRow.trace_context)
|
|
.where(OutboxRow.job_id == job.id)
|
|
.order_by(OutboxRow.id.desc())
|
|
.limit(1)
|
|
)
|
|
== retry
|
|
)
|
|
assert retry["traceparent"].split("-")[1] == origin["traceparent"].split("-")[1]
|
|
assert retry != origin
|
|
provider.shutdown()
|