116 lines
3.4 KiB
PL/PgSQL
116 lines
3.4 KiB
PL/PgSQL
\set ON_ERROR_STOP on
|
|
|
|
BEGIN;
|
|
|
|
-- Run this contract as supabase_admin. The second database session holds the
|
|
-- drain's global claim lock while the first session attempts a user-authorized
|
|
-- lease of the same transactionally enqueued system row.
|
|
CREATE EXTENSION IF NOT EXISTS dblink SCHEMA extensions;
|
|
|
|
CREATE OR REPLACE FUNCTION pg_temp.assert_true(condition boolean, message text)
|
|
RETURNS void
|
|
LANGUAGE plpgsql
|
|
AS $$
|
|
BEGIN
|
|
IF condition IS NOT TRUE THEN
|
|
RAISE EXCEPTION 'assertion_failed: %', message;
|
|
END IF;
|
|
END;
|
|
$$;
|
|
|
|
INSERT INTO auth.users (
|
|
id, aud, role, email, encrypted_password, email_confirmed_at,
|
|
raw_app_meta_data, raw_user_meta_data, created_at, updated_at
|
|
) VALUES (
|
|
'61800000-0000-4000-8000-000000000001',
|
|
'authenticated', 'authenticated', 'push-lock-race@example.invalid',
|
|
crypt('fixture-password', gen_salt('bf')), now(),
|
|
'{"provider":"email","providers":["email"]}'::jsonb, '{}', now(), now()
|
|
);
|
|
|
|
INSERT INTO public.history (
|
|
id, user_id, title, original_text, mode, status, duration, word_count, app_version
|
|
) VALUES (
|
|
'61810000-0000-4000-8000-000000000001',
|
|
'61800000-0000-4000-8000-000000000001',
|
|
'Claim lock race', 'claim lock race fixture', 'file-transcription',
|
|
'completed', 1, 4, 'e2e'
|
|
);
|
|
|
|
INSERT INTO public.push_dispatch_attempts (
|
|
caller_id, actor_kind, event_type, resource_id, status, attempt_count,
|
|
next_retry_at, created_at
|
|
) VALUES (
|
|
NULL, 'system', 'transcription.completed',
|
|
'61810000-0000-4000-8000-000000000001', 'pending', 0,
|
|
'2026-08-20 00:00:00+00', '2026-08-20 00:00:00+00'
|
|
);
|
|
|
|
SELECT extensions.dblink_connect('push_claim_lock_holder', 'dbname=postgres');
|
|
SELECT *
|
|
FROM extensions.dblink(
|
|
'push_claim_lock_holder',
|
|
$$SELECT pg_advisory_lock(
|
|
hashtextextended('system-push-global', 73050)
|
|
)::text$$
|
|
) AS held(result text);
|
|
|
|
SET LOCAL statement_timeout = '750ms';
|
|
SET LOCAL ROLE authenticated;
|
|
SELECT set_config(
|
|
'request.jwt.claims',
|
|
'{"sub":"61800000-0000-4000-8000-000000000001","role":"authenticated"}',
|
|
true
|
|
);
|
|
|
|
DO $$
|
|
DECLARE
|
|
started_at timestamptz := clock_timestamp();
|
|
BEGIN
|
|
BEGIN
|
|
PERFORM public.reserve_push_dispatch(
|
|
'transcription.completed', '61810000-0000-4000-8000-000000000001'
|
|
);
|
|
RAISE EXCEPTION 'expected push_claim_busy';
|
|
EXCEPTION WHEN SQLSTATE '55P03' THEN
|
|
PERFORM pg_temp.assert_true(
|
|
SQLERRM = 'push_claim_busy',
|
|
'contended user lease returns the stable lock-not-available code'
|
|
);
|
|
PERFORM pg_temp.assert_true(
|
|
extract(epoch FROM clock_timestamp() - started_at) < 0.70,
|
|
'contended user lease fails before the statement timeout'
|
|
);
|
|
END;
|
|
END;
|
|
$$;
|
|
|
|
RESET ROLE;
|
|
SET LOCAL statement_timeout = 0;
|
|
|
|
SELECT pg_temp.assert_true(
|
|
EXISTS (
|
|
SELECT 1
|
|
FROM public.push_dispatch_attempts
|
|
WHERE event_type = 'transcription.completed'
|
|
AND resource_id = '61810000-0000-4000-8000-000000000001'
|
|
AND actor_kind = 'system'
|
|
AND status = 'pending'
|
|
AND attempt_count = 0
|
|
AND next_retry_at = '2026-08-20 00:00:00+00'
|
|
AND dispatch_lease_token IS NULL
|
|
AND lease_expires_at IS NULL
|
|
),
|
|
'busy user statement rolls back and preserves the durable pending outbox'
|
|
);
|
|
|
|
SELECT *
|
|
FROM extensions.dblink(
|
|
'push_claim_lock_holder',
|
|
$$SELECT pg_advisory_unlock(
|
|
hashtextextended('system-push-global', 73050)
|
|
)$$
|
|
) AS released(unlocked boolean);
|
|
SELECT extensions.dblink_disconnect('push_claim_lock_holder');
|
|
|
|
ROLLBACK;
|