forked from mtusi2026/mtusi2026lab5
ЛР 5: реализация мессенджера и сценарии проверки
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
"""Inject worker termination around effects and verify a stale version is a no-op."""
|
||||
|
||||
import collections
|
||||
import json
|
||||
from unittest.mock import patch
|
||||
from uuid import UUID
|
||||
|
||||
from attachments import Objects
|
||||
from db import Store
|
||||
from pipeline import GROUP, STREAM, claim, connect_broker, process, relay_once, try_publish
|
||||
from replay import replay
|
||||
|
||||
|
||||
class InjectedCrash(BaseException):
|
||||
pass
|
||||
|
||||
|
||||
def expire_lease(store, message):
|
||||
with store.pool.connection() as db:
|
||||
db.execute("""
|
||||
UPDATE image_jobs SET leased_until=clock_timestamp()-interval '1 second'
|
||||
WHERE id=%s
|
||||
""", (UUID(message["job_id"]),))
|
||||
|
||||
|
||||
def state(store, row):
|
||||
with store.pool.connection() as db:
|
||||
return db.execute("SELECT * FROM attachments WHERE id=%s", (row,)).fetchone()
|
||||
|
||||
|
||||
def event(broker, job_id):
|
||||
for _, fields in reversed(broker.xrange(STREAM)):
|
||||
message = json.loads(fields["job"])
|
||||
if message["job_id"] == str(job_id):
|
||||
return message
|
||||
raise AssertionError("durable job was not relayed")
|
||||
|
||||
|
||||
def main():
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("attachment", type=UUID, help="One newly queued image in the isolated test stack")
|
||||
args = parser.parse_args()
|
||||
store, broker, objects = Store(), connect_broker(), Objects()
|
||||
attachment = args.attachment
|
||||
try:
|
||||
assert state(store, attachment)["status"] == "queued"
|
||||
with store.pool.connection() as db:
|
||||
original = db.execute("SELECT id FROM image_jobs WHERE attachment_id=%s", (attachment,)).fetchone()["id"]
|
||||
assert relay_once(store, broker)
|
||||
message = event(broker, original)
|
||||
job = claim(store, message)
|
||||
assert job
|
||||
|
||||
# The first worker dies after taking a lease, before any object is written.
|
||||
expire_lease(store, message)
|
||||
job = claim(store, message)
|
||||
calls = collections.Counter()
|
||||
put = objects.put
|
||||
|
||||
def crash_after_object(key, *pos, **named):
|
||||
result = put(key, *pos, **named)
|
||||
calls[key] += 1
|
||||
raise InjectedCrash()
|
||||
|
||||
try:
|
||||
with patch.object(objects, "put", side_effect=crash_after_object):
|
||||
try_publish(store, objects, job)
|
||||
except InjectedCrash:
|
||||
pass
|
||||
else:
|
||||
raise AssertionError("fault did not fire after an object was written")
|
||||
|
||||
assert state(store, attachment)["status"] == "processing"
|
||||
expire_lease(store, message)
|
||||
assert process(store, objects, message) == "ready"
|
||||
first = state(store, attachment)
|
||||
assert process(store, objects, message) == "ignored"
|
||||
assert state(store, attachment) == first
|
||||
|
||||
second_id = replay(store, attachment, new_version=True)
|
||||
assert relay_once(store, broker)
|
||||
second_message = event(broker, second_id)
|
||||
assert process(store, objects, second_message) == "ready"
|
||||
second = state(store, attachment)
|
||||
assert second["pipeline_version"] == first["pipeline_version"] + 1
|
||||
assert process(store, objects, message) == "ignored"
|
||||
assert state(store, attachment) == second
|
||||
assert len(calls) == 1 and len(second["variants"]) == 3
|
||||
print(json.dumps({"before_effect": True, "after_effect_before_commit": True,
|
||||
"duplicate_after_commit": True, "old_delivery_after_v2": True,
|
||||
"ready_variants": len(second["variants"])}, indent=2))
|
||||
finally:
|
||||
broker.close()
|
||||
store.pool.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user