Intelligence, Advanced Charting & Trading Platform
test(db): ensure confirmed provider rejection gets one fresh permit, while ambiguous outcomes never retry
davoodmehraban89 committed
Oct 23, 2026 at 14:47 UTC
600e2e697986c4ee5814a6882beb4dfdd0e47158
1 file changed
+141
db/tests/alert_egress_verified_retry.sql
new
+141
@@ -0,0 +1,141 @@
1
+-- Transactional CI-only test: after a provider-confirmed rejection the
2
+-- next authorized lease must be able to consume a NEW one-send permit.
3
+-- Ambiguous send and expired leases remain terminal, without duplicate POSTs.
4
+BEGIN;
5
+
6
+DO $$
7
+DECLARE
8
+ u UUID := 'b1111111-1111-4111-8111-111111111111';
9
+ recipient UUID := 'b2222222-2222-4222-8222-222222222222';
10
+ t0 TIMESTAMPTZ := '2026-10-22T12:00:00Z';
11
+ one RECORD;
12
+ two RECORD;
13
+ no_claim RECORD;
14
+ oid UUID;
15
+ rejected TEXT;
16
+ attempt_count INTEGER;
17
+ old_started TIMESTAMPTZ;
18
+ new_started TIMESTAMPTZ;
19
+BEGIN
20
+ IF current_database() <> 'velyqor_test' THEN
21
+ RAISE EXCEPTION 'REFUSE_NON_EPHEMERAL_DATABASE';
22
+ END IF;
23
+
24
+ INSERT INTO public.app_users(id) VALUES (u);
25
+ INSERT INTO public.alert_notification_consents(
26
+ user_id,recipient_ref,channel,consented_at
27
+ ) VALUES (u,recipient,'BALE',t0-interval '1 hour');
28
+
29
+ oid := public.notification_outbox_enqueue(
30
+ u,'explicit-rejection-and-safe-retry','BALE',recipient,2
31
+ );
32
+
33
+ SELECT * INTO one
34
+ FROM public.alert_notification_claim_consented(t0,30);
35
+ IF one.outbox_id IS DISTINCT FROM oid OR one.attempt_number <> 1 THEN
36
+ RAISE EXCEPTION 'initial claim missing';
37
+ END IF;
38
+ IF NOT public.alert_notification_begin_egress(
39
+ oid,one.claim_token,recipient,'BALE',t0+interval '1 second'
40
+ ) THEN
41
+ RAISE EXCEPTION 'initial one-time send permit denied';
42
+ END IF;
43
+ SELECT send_started_at INTO old_started
44
+ FROM public.notification_outbox WHERE id=oid;
45
+ IF old_started IS NULL THEN
46
+ RAISE EXCEPTION 'first send permit not persisted';
47
+ END IF;
48
+
49
+ -- ONLY a definite, verified provider rejection authorizes future retry.
50
+ rejected := public.notification_outbox_finish(
51
+ oid,one.claim_token,'REJECTED',NULL,t0+interval '2 seconds',60
52
+ );
53
+ IF rejected <> 'FAILED' THEN
54
+ RAISE EXCEPTION 'confirmed provider rejection not recorded';
55
+ END IF;
56
+ SELECT * INTO no_claim
57
+ FROM public.alert_notification_claim_consented(t0+interval '40 seconds',30);
58
+ IF no_claim.outbox_id IS NOT NULL THEN
59
+ RAISE EXCEPTION 'confirmed rejection retried before cooldown';
60
+ END IF;
61
+
62
+ SELECT * INTO two
63
+ FROM public.alert_notification_claim_consented(t0+interval '63 seconds',30);
64
+ IF two.outbox_id IS DISTINCT FROM oid
65
+ OR two.attempt_number <> 2
66
+ OR two.claim_token = one.claim_token THEN
67
+ RAISE EXCEPTION 'confirmed rejection did not yield fresh retry lease';
68
+ END IF;
69
+ SELECT send_started_at INTO new_started
70
+ FROM public.notification_outbox WHERE id=oid;
71
+ IF new_started IS NOT NULL THEN
72
+ RAISE EXCEPTION 'previous single-send permit survived a legitimate retry lease';
73
+ END IF;
74
+
75
+ IF public.alert_notification_begin_egress(
76
+ oid,one.claim_token,recipient,'BALE',t0+interval '64 seconds'
77
+ ) THEN
78
+ RAISE EXCEPTION 'previous worker token authorized second attempt';
79
+ END IF;
80
+ IF NOT public.alert_notification_begin_egress(
81
+ oid,two.claim_token,recipient,'BALE',t0+interval '64 seconds'
82
+ ) THEN
83
+ RAISE EXCEPTION 'new lease could not obtain legitimate second permit';
84
+ END IF;
85
+ IF public.alert_notification_begin_egress(
86
+ oid,two.claim_token,recipient,'BALE',t0+interval '64 seconds'
87
+ ) THEN
88
+ RAISE EXCEPTION 'same lease consumed two send permits';
89
+ END IF;
90
+
91
+ rejected := public.notification_outbox_finish(
92
+ oid,two.claim_token,'REJECTED',NULL,t0+interval '65 seconds',60
93
+ );
94
+ IF rejected <> 'FAILED' THEN
95
+ RAISE EXCEPTION 'second confirmed reject not recorded';
96
+ END IF;
97
+
98
+ SELECT * INTO no_claim
99
+ FROM public.alert_notification_claim_consented(t0+interval '200 seconds',30);
100
+ IF no_claim.outbox_id IS NOT NULL THEN
101
+ RAISE EXCEPTION 'attempt limit bypass after repeated confirmed rejection';
102
+ END IF;
103
+ SELECT attempts INTO attempt_count
104
+ FROM public.notification_outbox WHERE id=oid;
105
+ IF attempt_count <> 2 THEN
106
+ RAISE EXCEPTION 'attempt accounting incorrect';
107
+ END IF;
108
+
109
+ -- UNKNOWN does not become eligible for any new lease regardless of elapsed
110
+ -- time and its old send_started_at remains as an audit marker.
111
+ oid := public.notification_outbox_enqueue(
112
+ u,'unknown-is-terminal','BALE',recipient,3
113
+ );
114
+ SELECT * INTO one
115
+ FROM public.alert_notification_claim_consented(t0+interval '240 seconds',30);
116
+ IF one.outbox_id IS DISTINCT FROM oid THEN
117
+ RAISE EXCEPTION 'unknown test failed to claim';
118
+ END IF;
119
+ IF NOT public.alert_notification_begin_egress(
120
+ oid,one.claim_token,recipient,'BALE',t0+interval '241 seconds'
121
+ ) THEN
122
+ RAISE EXCEPTION 'unknown test initial send permit unavailable';
123
+ END IF;
124
+ PERFORM public.notification_outbox_finish(
125
+ oid,one.claim_token,'UNKNOWN',NULL,t0+interval '242 seconds'
126
+ );
127
+ SELECT * INTO no_claim
128
+ FROM public.alert_notification_claim_consented(t0+interval '600 seconds',30);
129
+ IF no_claim.outbox_id IS NOT NULL THEN
130
+ RAISE EXCEPTION 'uncertain message was retried';
131
+ END IF;
132
+ SELECT send_started_at INTO old_started
133
+ FROM public.notification_outbox WHERE id=oid;
134
+ IF old_started IS NULL THEN
135
+ RAISE EXCEPTION 'unknown outcome lost durable send-attempt marker';
136
+ END IF;
137
+
138
+ RAISE NOTICE 'ALERT_VERIFIED_RETRY_PERMIT_INVARIANTS_PASSED';
139
+END $$;
140
+
141
+ROLLBACK;