Intelligence, Advanced Charting & Trading Platform
test(risk): cross-session atomic alert state/outbox CAS race with two PostgreSQL clients
davoodmehraban89 committed
Oct 22, 2026 at 23:37 UTC
dab4f568044214403077bb38b2bdf9200d862064
1 file changed
+54
scripts/check-alert-runtime-concurrency.mjs
new
+54
@@ -0,0 +1,54 @@
1
+/**
2
+ * Local, disposable GitHub Actions PostgreSQL only.
3
+ * Cross-session compare-and-swap: two worker-like evaluations of same rule,
4
+ * same revision and same trigger may commit exactly one outbox event.
5
+ */
6
+import assert from 'node:assert/strict';
7
+import {execFileSync,spawn} from 'node:child_process';
8
+
9
+const env={...process.env};
10
+if(env.CI!=='true'||!['localhost','127.0.0.1'].includes(env.PGHOST)||
11
+ env.PGDATABASE!=='velyqor_test'||env.VELYQOR_TEST_DATABASE!=='velyqor_test')
12
+ throw new Error('REFUSING_NON_LOCAL_DATABASE');
13
+const user='c1111111-1111-4111-8111-111111111111';
14
+const dest='d2222222-2222-4222-8222-222222222222';
15
+const event='cross-session-alert:2026-10-22T12:00:00.000Z:0';
16
+const rule='cross-session-alert';
17
+const time='2026-10-22T12:00:00.000Z';
18
+const state=JSON.stringify({alert:{
19
+ id:rule,phase:'TRIGGERED',eventId:event,triggeredAt:time,lastTimestamp:time
20
+},gate:{suspended:false}});
21
+const stmt=q=>execFileSync('psql',['-X','-q','-A','-t','-v','ON_ERROR_STOP=1','-c',q],
22
+ {env,encoding:'utf8',timeout:14000,stdio:['ignore','pipe','pipe']}).trim();
23
+
24
+assert.equal(stmt('SELECT current_database()'),'velyqor_test');
25
+stmt("INSERT INTO public.app_users(id) VALUES ('"+user+"') ON CONFLICT DO NOTHING");
26
+stmt("INSERT INTO public.alert_notification_consents(user_id,recipient_ref,channel,consented_at) VALUES ('"+
27
+ user+"','"+dest+"','BALE','2026-10-22T11:00:00Z')");
28
+const sql="SELECT outcome||'|'||new_revision::text||'|'||coalesce(outbox_id::text,'') "+
29
+ "FROM public.alert_rule_commit('"+user+"','"+rule+"',0,'"+
30
+ state+"'::jsonb,'"+event+"','BALE','"+dest+"','2026-10-22T12:00:00Z')";
31
+const output=await Promise.all([0,1].map(()=>new Promise((resolve,reject)=>{
32
+ const child=spawn('psql',['-X','-q','-A','-t','-v','ON_ERROR_STOP=1','-c',sql],
33
+ {env,stdio:['ignore','pipe','pipe']});
34
+ let out='';
35
+ child.stdout.on('data',x=>out+=x.toString());
36
+ child.on('error',reject);
37
+ child.on('close',code=>code===0?resolve(out.trim()):
38
+ reject(new Error('CONCURRENT_CAS_STATEMENT_FAILED_REDACTED')));
39
+})));
40
+const success=output.filter(s=>s.startsWith('TRIGGERED_ENQUEUED|1|'));
41
+const losing=output.filter(s=>s==='STALE_REVISION|1|'||s==='ALREADY_TERMINAL|1|');
42
+assert.equal(success.length,1);
43
+assert.equal(losing.length,1);
44
+const count=stmt("SELECT count(*) FROM public.notification_outbox WHERE user_id='"+
45
+ user+"' AND event_key='"+event+"'");
46
+assert.equal(count,'1');
47
+const runtime=stmt("SELECT revision::text||':'||(state #>> '{alert,phase}') "+
48
+ "FROM public.alert_rule_runtime WHERE user_id='"+user+"' AND rule_key='"+rule+"'");
49
+assert.equal(runtime,'1:TRIGGERED');
50
+console.log('ALERT_STATE_OUTBOX_CAS_VERIFIED',JSON.stringify({
51
+ simultaneousEvaluations:2,successfulTriggers:1,staleOrTerminal:1,
52
+ outboxRows:1,revision:1,liveDatabaseTouched:false,
53
+ location:'disposable_local_postgres_ci'
54
+}));