test(work): interrupt attachment receipt after platform acceptance

This commit is contained in:
ex_tianzy
2026-11-23 15:28:43 +08:00
parent 3f4b8f983b
commit adaec211e5
+62 -4
View File
@@ -102,8 +102,30 @@ async def test_restart_recovers_only_confirmed_unsent_attachment(env,monkeypatch
await api.flush()
assert [r[0] for r in calls]==['upload','reply_file','upload']
if second_status=='sending':
original_reply=env['platform'].reply_file
accepted=asyncio.Event()
async def accept_without_returning_receipt(*args,**kwargs):
# The fake platform accepts the reply, then the connector exits
# before receiving or persisting the response.
await original_reply(*args,**kwargs)
accepted.set()
await asyncio.Future()
with monkeypatch.context() as patch:
patch.setattr(env['platform'],'reply_file',accept_without_returning_receipt)
interrupted=asyncio.create_task(api.flush())
try:
await asyncio.wait_for(accepted.wait(),timeout=3)
finally:
interrupted.cancel()
with pytest.raises(asyncio.CancelledError):
await interrupted
assert api.db.execute("SELECT status FROM work_files_v1 WHERE file_id=?",
(two.json()['file_id'],)).fetchone()['status']=='sending'
with api.db:
api.db.execute("UPDATE work_files_v1 SET status='sending',updated=? WHERE file_id=? AND status='uploaded'",
# Advance only the age of the observed claim past recovery grace.
api.db.execute("UPDATE work_files_v1 SET updated=? WHERE file_id=? AND status='sending'",
(time.time()-121,two.json()['file_id']))
second=create_app(env['settings'],env['redis'],env['platform'])
try:
@@ -111,7 +133,7 @@ async def test_restart_recovers_only_confirmed_unsent_attachment(env,monkeypatch
await second.state.files.flush()
await second.state.deliveries.flush()
async with httpx.AsyncClient(transport=httpx.ASGITransport(second),base_url='https://relay.example.test') as http:
path='/work/v2/tasks/state'
path='/work/v2/tasks/delivery'
status=await http.post(path,json=wire(env,path,task_id='task-files'))
assert status.status_code==200
receipt=status.json()['delivery']
@@ -130,8 +152,44 @@ async def test_restart_recovers_only_confirmed_unsent_attachment(env,monkeypatch
finally:
second.state.store.close()
assert env['platform'].replies==[('om-original','synthetic result')]
assert [r[0] for r in calls]==(['upload','reply_file','upload','reply_file']
if second_status=='uploaded' else ['upload','reply_file','upload'])
assert [r[0] for r in calls]==['upload','reply_file','upload','reply_file']
async def test_live_connector_loop_delivers_text_while_attachment_upload_is_blocked(env,monkeypatch):
_,calls=fake_platform(env,monkeypatch);create_task(env)
started=asyncio.Event()
permit_upload=asyncio.Event()
replied=asyncio.Event()
original_upload=env['platform'].upload_file
original_reply=env['platform'].reply
async def blocked_upload(*args,**kwargs):
started.set()
await permit_upload.wait()
return await original_upload(*args,**kwargs)
async def mark_text_reply(*args,**kwargs):
await original_reply(*args,**kwargs)
replied.set()
monkeypatch.setattr(env['platform'],'upload_file',blocked_upload)
monkeypatch.setattr(env['platform'],'reply',mark_text_reply)
assert (await upload(env)).status_code==200
try:
await asyncio.wait_for(started.wait(),timeout=12)
path='/work/v2/tasks/result'
result=await env['client'].post(path,json=wire(env,path,task_id='task-files',result='text while upload is blocked'))
assert result.status_code==200
await asyncio.wait_for(replied.wait(),timeout=12)
path='/work/v2/tasks/delivery'
result=await env['client'].post(path,json=wire(env,path,task_id='task-files'))
assert result.status_code==200
delivery=result.json()['delivery']
assert delivery['text']['platform_sent'] is True
assert delivery['attachments'][0]['status']=='uploading'
assert not calls
finally:
permit_upload.set()
async def test_local_file_integrity_failure_is_not_a_platform_unknown(env,monkeypatch):