64 lines
3.2 KiB
Python
64 lines
3.2 KiB
Python
import io
|
|
import pytest
|
|
from messenger.jobs import work, replay
|
|
from test_attachments import actors,upload,image_bytes,finish
|
|
from test_api import client
|
|
|
|
|
|
def job_for(client,aid):
|
|
with client.app.state.db.pool.connection() as conn:
|
|
return conn.execute('SELECT id FROM attachment_jobs WHERE attachment_id=%s',(aid,)).fetchone()['id']
|
|
|
|
|
|
def test_duplicate_ready_is_noop_and_not_replayable(client):
|
|
ah,_,_,chat=actors(client)
|
|
att=upload(client,ah,chat).json()
|
|
finish(client,att['id'])
|
|
path='/api/v1/attachments/'+att['id']
|
|
before=client.get(path,headers=ah).json()
|
|
job=job_for(client,att['id'])
|
|
for _ in range(3): assert work(client.app.state.db,client.app.state.storage,job,1)
|
|
assert client.get(path,headers=ah).json()==before
|
|
with pytest.raises(ValueError): replay(client.app.state.db,job,'must reject ready')
|
|
with client.app.state.db.pool.connection() as conn:
|
|
row=conn.execute('SELECT attempt FROM attachment_jobs WHERE id=%s',(job,)).fetchone()
|
|
assert row['attempt']==1
|
|
assert conn.execute('SELECT staging_key FROM attachments WHERE id=%s',(att['id'],)).fetchone()['staging_key'] is None
|
|
|
|
|
|
def test_old_delivery_does_not_consume_attempt(client):
|
|
# Direct invocation avoids consumer timing; it exercises the same real DB/S3 code.
|
|
ah,_,_,chat=actors(client)
|
|
att=upload(client,ah,chat).json()
|
|
job=job_for(client,att['id'])
|
|
assert work(client.app.state.db,client.app.state.storage,job,999)
|
|
with client.app.state.db.pool.connection() as conn:
|
|
row=conn.execute('SELECT attempt FROM attachment_jobs WHERE id=%s',(job,)).fetchone()
|
|
assert row['attempt'] in (0,1) # independent live consumer may already finish this job
|
|
finish(client,att['id'])
|
|
|
|
|
|
def test_replay_sql_failure_releases_session_lock(client):
|
|
import psycopg
|
|
from messenger.db import setting
|
|
from psycopg.rows import dict_row
|
|
ah,_,_,chat=actors(client)
|
|
att=upload(client,ah,chat).json()
|
|
finish(client,att['id'])
|
|
job=job_for(client,att['id'])
|
|
db=client.app.state.db
|
|
with db.pool.connection() as conn:
|
|
conn.execute("UPDATE attachments SET status='failed',staging_key=%s WHERE id=%s",('staging/'+att['id'],att['id']))
|
|
conn.execute("CREATE FUNCTION lab5_fail_audit() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN RAISE EXCEPTION 'synthetic audit failure'; END $$")
|
|
conn.execute('CREATE TRIGGER lab5_fail_audit BEFORE INSERT ON job_replays FOR EACH ROW EXECUTE FUNCTION lab5_fail_audit()')
|
|
try:
|
|
with pytest.raises(psycopg.Error): replay(db,job,'test failed transaction unlock')
|
|
with psycopg.connect(setting('DATABASE_URL'),row_factory=dict_row) as other:
|
|
acquired=other.execute('SELECT pg_try_advisory_lock(hashtextextended(%s,0)) AS ok',('job:'+str(job),)).fetchone()['ok']
|
|
if acquired: other.execute('SELECT pg_advisory_unlock(hashtextextended(%s,0))',('job:'+str(job),))
|
|
assert acquired, 'Aborted replay leaked a session advisory lock into the pool'
|
|
finally:
|
|
with db.pool.connection() as conn:
|
|
conn.execute('DROP TRIGGER lab5_fail_audit ON job_replays')
|
|
conn.execute('DROP FUNCTION lab5_fail_audit()')
|