Intelligence, Advanced Charting & Trading Platform
test(db): race two real senders on single-use permit and revoke consent after claim
davoodmehraban89 committed
Oct 23, 2026 at 02:40 UTC
ffcf8d65a2896e21b92a00cef325f9e7a2aa2e85
1 file changed
+31
-1
scripts/check-alert-runtime-concurrency.mjs
+31
-1
@@ -50,8 +50,38 @@ assert.equal(count,'1');
50
const runtime=stmt("SELECT revision::text||':'||(state #>> '{alert,phase}') "+
51
"FROM public.alert_rule_runtime WHERE user_id='"+user+"' AND rule_key='"+rule+"'");
52
assert.equal(runtime,'1:TRIGGERED');
53
+// Distinct process race after one CONSENTED claim: a single-use send permit
54
+// must authorize exactly one sender, even for the SAME valid claim token.
55
+const row=stmt("SELECT outbox_id::text||'|'||claim_token::text "+
56
+ "FROM public.alert_notification_claim_consented('2026-10-22T12:00:05Z',30)");
57
+const [claimedId,token]=row.split('|');
58
+assert.equal(claimedId,success[0].split('|')[2]);
59
+assert.match(token,/^[a-f0-9-]{36}$/i);
60
+const begin="SELECT public.alert_notification_begin_egress('"+claimedId+
61
+ "','"+token+"','2026-10-22T12:00:06Z')::text";
62
+const competing=await Promise.all([0,1].map(()=>new Promise((resolve,reject)=>{
63
+ const proc=spawn('psql',['-X','-q','-A','-t','-v','ON_ERROR_STOP=1','-c',begin],
64
+ {env,stdio:['ignore','pipe','pipe']});
65
+ let out='';
66
+ proc.stdout.on('data',x=>out+=x.toString());
67
+ proc.on('error',reject);
68
+ proc.on('close',code=>code===0?resolve(out.trim()):
69
+ reject(new Error('CONCURRENT_SEND_PERMIT_FAILED_REDACTED')));
70
+})));
71
+assert.deepEqual(competing.slice().sort(),['f','t']);
72
+const permitState=stmt("SELECT count(*) FROM public.notification_outbox "+
73
+ "WHERE id='"+claimedId+"' AND send_started_at='2026-10-22T12:00:06Z'");
74
+assert.equal(permitState,'1');
75
+assert.equal(stmt("SELECT public.alert_notification_egress_allowed('"+
76
+ claimedId+"','"+token+"','2026-10-22T12:00:07Z')::text"),'t');
77
+stmt("UPDATE public.alert_notification_consents SET revoked_at='2026-10-22T12:00:08Z' "+
78
+ "WHERE user_id='"+user+"' AND recipient_ref='"+dest+"' AND channel='BALE'");
79
+assert.equal(stmt("SELECT public.alert_notification_egress_allowed('"+
80
+ claimedId+"','"+token+"','2026-10-22T12:00:09Z')::text"),'f');
81
+
82
console.log('ALERT_STATE_OUTBOX_CAS_VERIFIED',JSON.stringify({
83
simultaneousEvaluations:2,successfulTriggers:1,staleOrTerminal:1,
55
- outboxRows:1,revision:1,liveDatabaseTouched:false,
84
+ outboxRows:1,revision:1,parallelSendPermits:2,successfulSendPermits:1,
85
+ consentRevokedAfterPermitPreventsEgress:true,liveDatabaseTouched:false,
86
location:'disposable_local_postgres_ci'
87
}));