@davoodmehraban89 / VELYQOR / commits / 4ffbdef
Fork 0

Intelligence, Advanced Charting & Trading Platform

test(db): exercise idempotent enqueue, lease, stale-worker refusal, bounded retry and acknowledgements

davoodmehraban89 committed Oct 22, 2026 at 23:14 UTC 4ffbdefaca3ee5999034300179f8710394101716
1 file changed +157
db/tests/notification_outbox_invariants.sql new
+157
@@ -0,0 +1,157 @@
1 +-- All fixture rows and transitions are rolled back. This file runs only in
2 +-- GitHub Actions ephemeral PostgreSQL, never in live Neon.
3 +BEGIN;
4 +
5 +DO $$
6 +DECLARE
7 + u UUID := '11111111-1111-4111-8111-111111111111';
8 + r UUID := '22222222-2222-4222-8222-222222222222';
9 + other_u UUID := '33333333-3333-4333-8333-333333333333';
10 + t0 TIMESTAMPTZ := '2026-10-22T12:00:00Z';
11 + n UUID;
12 + dup UUID;
13 + n2 UUID;
14 + claimed RECORD;
15 + another RECORD;
16 + decision TEXT;
17 + attempts_actual INTEGER;
18 + current_status TEXT;
19 + accepted_id TEXT;
20 +BEGIN
21 + INSERT INTO app_users(id) VALUES (u),(other_u);
22 +
23 + n := notification_outbox_enqueue(u,'event-1','BALE',r,2);
24 + dup := notification_outbox_enqueue(u,'event-1','BALE',r,2);
25 + IF n <> dup THEN RAISE EXCEPTION 'idempotent enqueue changed row ID'; END IF;
26 + SELECT count(*) INTO attempts_actual FROM notification_outbox
27 + WHERE user_id=u AND event_key='event-1';
28 + IF attempts_actual <> 1 THEN RAISE EXCEPTION 'duplicate event inserted'; END IF;
29 +
30 + BEGIN
31 + PERFORM notification_outbox_enqueue(u,'', 'BALE',r,2);
32 + RAISE EXCEPTION 'blank event accepted';
33 + EXCEPTION WHEN invalid_parameter_value THEN NULL;
34 + END;
35 + BEGIN
36 + PERFORM notification_outbox_enqueue(u,'event-2','TELEGRAM',r,2);
37 + RAISE EXCEPTION 'unapproved channel accepted';
38 + EXCEPTION WHEN invalid_parameter_value THEN NULL;
39 + END;
40 +
41 + SELECT * INTO claimed FROM notification_outbox_claim(t0,30);
42 + IF claimed.outbox_id <> n OR claimed.attempt_number <> 1
43 + OR claimed.claim_token IS NULL THEN
44 + RAISE EXCEPTION 'claim did not return exact unique lease';
45 + END IF;
46 + SELECT * INTO another FROM notification_outbox_claim(t0,30);
47 + IF another.outbox_id IS NOT NULL THEN
48 + RAISE EXCEPTION 'concurrent/in-flight claim returned already leased row';
49 + END IF;
50 + decision := notification_outbox_finish(n,gen_random_uuid(),
51 + 'SENT','msg-1',t0 + interval '5 seconds');
52 + IF decision <> 'IGNORED_STALE_OR_INELIGIBLE' THEN
53 + RAISE EXCEPTION 'wrong lease token completed another worker claim';
54 + END IF;
55 + BEGIN
56 + PERFORM notification_outbox_finish(n,claimed.claim_token,
57 + 'SENT',NULL,t0 + interval '5 seconds');
58 + RAISE EXCEPTION 'provider acceptance without message ID allowed';
59 + EXCEPTION WHEN invalid_parameter_value THEN NULL;
60 + END;
61 +
62 + decision := notification_outbox_finish(n,claimed.claim_token,
63 + 'SENT','provider-accepted-id',t0 + interval '5 seconds');
64 + IF decision <> 'SENT' THEN RAISE EXCEPTION 'verified send failed'; END IF;
65 + decision := notification_outbox_finish(n,claimed.claim_token,
66 + 'SENT','provider-accepted-id',t0 + interval '6 seconds');
67 + IF decision <> 'IGNORED_STALE_OR_INELIGIBLE' THEN
68 + RAISE EXCEPTION 'duplicate send completion succeeded';
69 + END IF;
70 + SELECT status,provider_message_id INTO current_status,accepted_id
71 + FROM notification_outbox WHERE id=n;
72 + IF current_status <> 'SENT' OR accepted_id <> 'provider-accepted-id' THEN
73 + RAISE EXCEPTION 'provider receipt not durable';
74 + END IF;
75 +
76 + IF notification_outbox_acknowledge(n,other_u,t0 + interval '7 seconds') THEN
77 + RAISE EXCEPTION 'cross-user acknowledgement succeeded';
78 + END IF;
79 + IF notification_outbox_acknowledge(n,u,t0) THEN
80 + RAISE EXCEPTION 'acknowledgement before send accepted';
81 + END IF;
82 + IF NOT notification_outbox_acknowledge(n,u,t0 + interval '8 seconds') THEN
83 + RAISE EXCEPTION 'explicit user acknowledgement failed';
84 + END IF;
85 + IF notification_outbox_acknowledge(n,u,t0 + interval '9 seconds') THEN
86 + RAISE EXCEPTION 'duplicate acknowledgement succeeded';
87 + END IF;
88 + SELECT status INTO current_status FROM notification_outbox WHERE id=n;
89 + IF current_status <> 'ACKNOWLEDGED' THEN
90 + RAISE EXCEPTION 'message ACK state not persisted';
91 + END IF;
92 +
93 + -- Explicit provider rejection is the ONLY automatic retry-eligible outcome.
94 + n2 := notification_outbox_enqueue(u,'event-2','BALE',r,2);
95 + SELECT * INTO claimed FROM notification_outbox_claim(t0 + interval '1 minute',30);
96 + IF claimed.outbox_id <> n2 THEN RAISE EXCEPTION 'unexpected retry target'; END IF;
97 + decision := notification_outbox_finish(n2,claimed.claim_token,
98 + 'REJECTED',NULL,t0 + interval '65 seconds',60);
99 + IF decision <> 'FAILED' THEN RAISE EXCEPTION 'explicit reject incorrect'; END IF;
100 +
101 + SELECT * INTO another FROM notification_outbox_claim(t0 + interval '90 seconds',30);
102 + IF another.outbox_id IS NOT NULL THEN RAISE EXCEPTION 'retry occurred too early'; END IF;
103 + SELECT * INTO claimed FROM notification_outbox_claim(t0 + interval '126 seconds',30);
104 + IF claimed.outbox_id <> n2 OR claimed.attempt_number <> 2 THEN
105 + RAISE EXCEPTION 'retry after verified rejection failed';
106 + END IF;
107 + decision := notification_outbox_finish(n2,claimed.claim_token,
108 + 'UNKNOWN',NULL,t0 + interval '130 seconds');
109 + IF decision <> 'UNKNOWN' THEN RAISE EXCEPTION 'ambiguous send not terminal'; END IF;
110 + SELECT * INTO another FROM notification_outbox_claim(t0 + interval '3 minutes',30);
111 + IF another.outbox_id IS NOT NULL THEN
112 + RAISE EXCEPTION 'unknown outcome retried despite duplicate-send risk';
113 + END IF;
114 +
115 + -- Crash after transport attempt but before outcome saved: lease expires,
116 + -- record becomes UNKNOWN; NEVER claim again without manual reconciliation.
117 + n2 := notification_outbox_enqueue(u,'event-3','BALE',r,3);
118 + SELECT * INTO claimed FROM notification_outbox_claim(t0 + interval '4 minutes',30);
119 + IF claimed.outbox_id <> n2 THEN RAISE EXCEPTION 'lease test wrong event'; END IF;
120 + SELECT * INTO another FROM notification_outbox_claim(t0 + interval '5 minutes',30);
121 + IF another.outbox_id IS NOT NULL THEN
122 + RAISE EXCEPTION 'crashed in-flight message re-claimed';
123 + END IF;
124 + SELECT status INTO current_status FROM notification_outbox WHERE id=n2;
125 + IF current_status <> 'UNKNOWN' THEN
126 + RAISE EXCEPTION 'expired in-flight lease did not turn UNKNOWN';
127 + END IF;
128 + IF notification_outbox_finish(n2,claimed.claim_token,'SENT','late',
129 + t0 + interval '5 minutes') <> 'IGNORED_STALE_OR_INELIGIBLE' THEN
130 + RAISE EXCEPTION 'late worker modified unknown outcome';
131 + END IF;
132 +
133 + -- DB constraint enforcement: cannot forge received state without evidence.
134 + BEGIN
135 + UPDATE notification_outbox SET status='SENT'
136 + WHERE id=n2;
137 + RAISE EXCEPTION 'SENT without provider receipt bypassed SQL constraint';
138 + EXCEPTION WHEN check_violation THEN NULL;
139 + END;
140 + BEGIN
141 + UPDATE notification_outbox SET status='LEASED' WHERE id=n2;
142 + RAISE EXCEPTION 'LEASED without lease token bypassed SQL constraint';
143 + EXCEPTION WHEN check_violation THEN NULL;
144 + END;
145 +
146 + -- Idempotency scoped per user: no leakage between two users.
147 + dup := notification_outbox_enqueue(other_u,'event-1','BALE',r,2);
148 + IF dup = n THEN RAISE EXCEPTION 'user-scoped idempotency violated'; END IF;
149 +
150 + -- SQL permissions do not grant anonymous read or execution.
151 + IF has_table_privilege('public','notification_outbox','SELECT')
152 + AND EXISTS(SELECT 1 FROM pg_roles WHERE rolname='public') THEN
153 + RAISE EXCEPTION 'unexpected public table privilege';
154 + END IF;
155 +END $$;
156 +
157 +ROLLBACK;