forked from mtusi2026/mtusi2026lab5
41 lines
2.3 KiB
Python
41 lines
2.3 KiB
Python
"""Real broker outage and bounded admission, followed by complete recovery."""
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
import hashlib
|
|
import json
|
|
import time
|
|
from lifecycle import compose,probe
|
|
from client import Client,check
|
|
|
|
|
|
def main():
|
|
c=Client('http://localhost:8085');started=time.monotonic()
|
|
try:
|
|
compose('stop','worker','publisher','broker')
|
|
alice,bob,eve,chat=c.bootstrap()
|
|
# Default deployment admits at most 100 pending logical jobs across APIs.
|
|
def upload(_):return Client(c.base).attachment(alice,chat)
|
|
with ThreadPoolExecutor(max_workers=8) as pool:accepted=list(pool.map(upload,range(100)))
|
|
fixture=accepted[0][1]
|
|
c.call('POST','/api/v1/chats/'+chat['id']+'/attachments?filename=full.png',429,actor=alice,raw=fixture,headers={'Content-Type':'image/png'})
|
|
c.message(alice,chat,'messages remain writable while image queue is full')
|
|
for att,_ in (accepted[0],accepted[-1]):
|
|
c.call('GET','/api/v1/attachments/'+att['id']+'/content',409,actor=bob)
|
|
admitted_seconds=time.monotonic()-started
|
|
# Every 202 was accepted while the broker was genuinely stopped.
|
|
state=probe(accepted[0][0]['id'])
|
|
check(state['published_at'] is None and state['state']=='queued','upload depended on unavailable broker')
|
|
compose('start','broker','publisher','worker');recovery=time.monotonic()
|
|
for att,data in accepted:
|
|
ready=c.wait_ready(bob,att,wait_seconds=120)
|
|
check(len(ready['variants'])==3 and ready['sha256']==hashlib.sha256(data).hexdigest(),'recovery lost accepted metadata')
|
|
for att,data in (accepted[0],accepted[-1]):
|
|
check(c.call('GET','/api/v1/attachments/'+att['id']+'/content',actor=bob)['bytes']==data,'recovery lost original bytes')
|
|
print(json.dumps({'status':'PASS','accepted_while_broker_down':len(accepted),'next_upload_status':429,
|
|
'messages_available':True,'all_ready':len(accepted),'admission_seconds':round(admitted_seconds,3),
|
|
'drain_seconds':round(time.monotonic()-recovery,3),'lost_accepted':0,
|
|
'profile':'100 PNG 320x200, 8 upload clients, 1 serial worker; closed-loop; not a throughput benchmark'},indent=2))
|
|
finally:compose('start','broker','publisher','worker')
|
|
|
|
|
|
if __name__=='__main__':main()
|