forked from mtusi2026/mtusi2026lab5
Fix cold worker claim result mapping with Core updates
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
import asyncio
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
|
||||
from httpx import AsyncClient
|
||||
from test_pipeline import accepted, job_of
|
||||
|
||||
from conftest import DATABASE
|
||||
|
||||
PROBE = """
|
||||
import asyncio, os
|
||||
from datetime import timedelta
|
||||
from uuid import uuid4
|
||||
from messenger.worker import create_worker
|
||||
from messenger.attachments.infrastructure.postgres import PostgresAttachmentsUnitOfWork
|
||||
from messenger.attachments.entrypoints.schemas import JobMessage
|
||||
from messenger.shared.infrastructure.postgres import Database
|
||||
|
||||
async def main():
|
||||
db = Database(os.environ['TEST_DATABASE_URL'], max_size=2, connect_timeout=2,
|
||||
acquire_timeout=2, command_timeout=3)
|
||||
job = JobMessage.model_validate_json(os.environ['TEST_JOB']).job()
|
||||
async def claim():
|
||||
uow = PostgresAttachmentsUnitOfWork(db)
|
||||
async with uow.transaction():
|
||||
claimed = await uow.jobs.claim(job, uuid4(), timedelta(seconds=5))
|
||||
assert claimed is not None
|
||||
assert claimed.lease.job.attempt == 1
|
||||
assert claimed.lease.job.created_at == job.created_at
|
||||
try:
|
||||
await asyncio.gather(claim(), claim())
|
||||
finally:
|
||||
await db.close()
|
||||
asyncio.run(main())
|
||||
"""
|
||||
|
||||
|
||||
async def test_cold_worker_claim_returning_is_stable(client: AsyncClient) -> None:
|
||||
_, _, ident = await accepted(client)
|
||||
job = await job_of(ident)
|
||||
for seed in range(24):
|
||||
child = await asyncio.create_subprocess_exec(
|
||||
sys.executable,
|
||||
"-c",
|
||||
PROBE,
|
||||
env={
|
||||
**os.environ,
|
||||
"PYTHONHASHSEED": str(seed),
|
||||
"TEST_DATABASE_URL": DATABASE["url"],
|
||||
"TEST_JOB": json.dumps(job.payload()),
|
||||
},
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
)
|
||||
try:
|
||||
_, errors = await asyncio.wait_for(child.communicate(), 20)
|
||||
finally:
|
||||
if child.returncode is None:
|
||||
child.kill()
|
||||
await child.wait()
|
||||
assert child.returncode == 0, f"seed={seed}: {errors.decode()}"
|
||||
Reference in New Issue
Block a user