forked from mtusi2026/mtusi2026lab4
готово
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
"""The HTTP budget must not await a stuck driver's cancellation/rollback."""
|
||||
import asyncio
|
||||
from unittest.mock import patch
|
||||
from src.core.request_deadline import RequestDeadlineMiddleware
|
||||
from src.core.settings import settings
|
||||
|
||||
|
||||
async def main():
|
||||
release=asyncio.Event()
|
||||
calls=0
|
||||
async def stuck(scope,receive,send):
|
||||
nonlocal calls
|
||||
calls+=1
|
||||
try:
|
||||
await asyncio.Event().wait()
|
||||
except asyncio.CancelledError:
|
||||
await release.wait()
|
||||
# A misbehaving cancelled handler must not emit a late success.
|
||||
await send({'type':'http.response.start','status':201,'headers':[]})
|
||||
await send({'type':'http.response.body','body':b'late'})
|
||||
middleware=RequestDeadlineMiddleware(stuck)
|
||||
scope={'type':'http','method':'GET','path':'/api/v1/chats','headers':[], 'query_string':b''}
|
||||
async def receive():
|
||||
return {'type':'http.request','body':b'','more_body':False}
|
||||
responses=[]
|
||||
async def send(message): responses.append(message)
|
||||
with patch.object(settings,'http_request_timeout_seconds',.02),patch.object(settings,'http_concurrency_limit',2):
|
||||
for _ in range(3):
|
||||
await asyncio.wait_for(middleware(scope,receive,send),timeout=.15)
|
||||
assert calls==2 and len(middleware.pending)==2
|
||||
assert [m['status'] for m in responses if m['type']=='http.response.start']==[503]*3
|
||||
release.set()
|
||||
await asyncio.gather(*middleware.pending)
|
||||
await asyncio.sleep(0)
|
||||
assert not middleware.pending
|
||||
assert [m['status'] for m in responses if m['type']=='http.response.start']==[503]*3
|
||||
assert all(m.get('body')!=b'late' for m in responses)
|
||||
async def healthy(scope,receive,send):
|
||||
await send({'type':'http.response.start','status':200,'headers':[]})
|
||||
await send({'type':'http.response.body','body':b'ok'})
|
||||
middleware.app=healthy
|
||||
responses.clear()
|
||||
await middleware(scope,receive,send)
|
||||
assert responses[-1]['body']==b'ok'
|
||||
print('PASS: bounded response before slow rollback, retained capacity, no late response, recovery')
|
||||
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user