forked from mtusi2026/mtusi2026lab6
Лабораторная 6: наблюдаемость мессенджера
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
"""Run via docker compose exec -T api-a python - with this file on stdin."""
|
||||
from hashlib import sha256
|
||||
from io import BytesIO
|
||||
from uuid import uuid4
|
||||
|
||||
from botocore.exceptions import ClientError
|
||||
from PIL import Image
|
||||
|
||||
from messenger.attachments import AttachmentStorage
|
||||
from messenger.clean_attachments import GRACE_SECONDS, clean_batch
|
||||
from messenger.config import Config
|
||||
from messenger.db import Database
|
||||
from messenger.errors import Unavailable
|
||||
from messenger.store import Store
|
||||
|
||||
|
||||
def main():
|
||||
config = Config.from_env()
|
||||
db = Database(config)
|
||||
db.open()
|
||||
store = Store(config, db, None)
|
||||
s3 = AttachmentStorage()
|
||||
user, chat, aid = [str(uuid4()) for _ in range(3)]
|
||||
key = 'attachments/' + aid
|
||||
payload = BytesIO()
|
||||
Image.new('RGB', (2, 2), 'green').save(payload, format='PNG')
|
||||
data = payload.getvalue()
|
||||
|
||||
try:
|
||||
with db.cursor() as cur:
|
||||
cur.execute('INSERT INTO users (id,username,password_hash,display_name) VALUES (%s,%s,%s,%s)',
|
||||
(user, 'gc_' + user[:8], 'unused', 'Cleanup check'))
|
||||
cur.execute("INSERT INTO chats (id,title) VALUES (%s,'Cleanup check')", (chat,))
|
||||
store.reserve_attachment(aid, chat, user, 'probe.png', 'image/png',
|
||||
len(data), sha256(data).hexdigest(), key)
|
||||
|
||||
def put_then_fail(object_key):
|
||||
s3.put(object_key, BytesIO(data), len(data), 'image/png')
|
||||
raise Unavailable('synthetic failure after successful PUT')
|
||||
|
||||
try:
|
||||
store.finish_attachment(aid, put_then_fail)
|
||||
except Unavailable as exc:
|
||||
assert str(exc) == 'synthetic failure after successful PUT'
|
||||
else:
|
||||
raise AssertionError('fault did not run')
|
||||
obj = s3.client.get_object(Bucket=s3.bucket, Key=key)
|
||||
with obj['Body'] as body:
|
||||
assert body.read() == data
|
||||
with db.cursor() as cur:
|
||||
cur.execute('SELECT status FROM attachments WHERE id=%s', (aid,))
|
||||
assert cur.fetchone()['status'] == 'pending'
|
||||
cur.execute("UPDATE attachments SET updated_at=now()-make_interval(secs => %s) WHERE id=%s",
|
||||
(GRACE_SECONDS + 1, aid))
|
||||
clean_batch(store, s3)
|
||||
with db.cursor() as cur:
|
||||
cur.execute('SELECT 1 FROM attachments WHERE id=%s', (aid,))
|
||||
assert cur.fetchone() is None
|
||||
try:
|
||||
obj = s3.client.get_object(Bucket=s3.bucket, Key=key)
|
||||
except ClientError as exc:
|
||||
assert exc.response['ResponseMetadata']['HTTPStatusCode'] == 404, exc
|
||||
else:
|
||||
obj['Body'].close()
|
||||
raise AssertionError('orphan object still exists')
|
||||
print('PASS: actual MinIO object existed after failed commit and was removed by cleanup')
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user