Intelligence, Advanced Charting & Trading Platform

1 /**
2 * CI-ONLY PostgreSQL cross-session locking smoke test.
3 * Refuses to execute against any host or DB other than the local disposable
4 * GitHub Actions service. No live Neon access and no confidential values.
5 */
6 import {execFileSync,spawn} from 'node:child_process';
7 import assert from 'node:assert/strict';
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-CI or non-local PostgreSQL access');
13 }
14 const user='a1111111-1111-4111-8111-111111111111';
15 const recipient='b2222222-2222-4222-8222-222222222222';
16 const sql=statement=>execFileSync('psql',[
17 '-X','-q','-A','-t','-v','ON_ERROR_STOP=1','-c',statement
18 ],{env,timeout:12000,encoding:'utf8',stdio:['ignore','pipe','pipe']}).trim();
19 const enqueue=event=>"SELECT public.notification_outbox_enqueue('"+
20 user+"','"+event+"','BALE','"+recipient+"',3);";
21
22 assert.equal(sql('SELECT current_database()'),'velyqor_test');
23 sql("INSERT INTO public.app_users(id) VALUES('"+user+"') ON CONFLICT DO NOTHING;");
24
25 // Two separate psql connections race to enqueue the exact same logical event.
26 const [a,b]=await Promise.all([1,2].map(()=>new Promise((resolve,reject)=>{
27 const child=spawn('psql',['-X','-q','-A','-t','-v','ON_ERROR_STOP=1',
28 '-c',enqueue('ci-parallel-enqueue')],{env,stdio:['ignore','pipe','pipe']});
29 let out='';
30 child.stdout.on('data',x=>out+=x);
31 child.on('error',reject);
32 child.on('close',code=>code===0?resolve(out.trim()):
33 reject(new Error('Concurrent psql enqueue failed (details redacted)')));
34 })));
35 assert.match(a,/^[a-f0-9-]{36}$/i);
36 assert.equal(a,b);
37 assert.equal(sql("SELECT count(*) FROM public.notification_outbox WHERE event_key='ci-parallel-enqueue'"),'1');
38
39 // First claim is held UNCOMMITTED in a separate live connection; another
40 // connection must SKIP it instead of waiting, stealing or double-claiming.
41 const first=spawn('psql',['-X','-q','-A','-t','-v','ON_ERROR_STOP=1'],
42 {env,stdio:['pipe','pipe','pipe']});
43 let output='',finished=false;
44 first.stdout.on('data',v=>output+=v.toString());
45 first.on('close',()=>{finished=true});
46 const awaitOutput=async pattern=>{
47 const start=Date.now();
48 while(!pattern.test(output)){
49 if(finished)throw new Error('First transaction unexpectedly closed (redacted)');
50 if(Date.now()-start>12000)throw new Error('First transaction did not claim in time');
51 await new Promise(ok=>setTimeout(ok,50));
52 }
53 };
54 try{
55 first.stdin.write('BEGIN;\n');
56 first.stdin.write("SELECT outbox_id FROM public.notification_outbox_claim(now(),30) WHERE event_key='ci-parallel-enqueue';\n");
57 await awaitOutput(/[a-f0-9]{8}-[a-f0-9-]{27}/i);
58 const committedClaimId=output.match(/[a-f0-9]{8}-[a-f0-9-]{27}/i)[0];
59 assert.equal(committedClaimId,a);
60
61 const before=sql('SELECT count(*) FROM public.notification_outbox_claim(now(),30)');
62 assert.equal(before,'0','Second connection claimed a locked message');
63
64 first.stdin.write('COMMIT;\n\\q\n');
65 const closed=await new Promise((resolve,reject)=>{
66 const timer=setTimeout(()=>reject(new Error('psql did not complete')),8000);
67 first.on('close',code=>{clearTimeout(timer);resolve(code)});
68 });
69 assert.equal(closed,0,'claim transaction failed');
70 const after=sql('SELECT count(*) FROM public.notification_outbox_claim(now(),30)');
71 assert.equal(after,'0','Message claimed twice after the first commit');
72 const snapshot=sql("SELECT status||':'||attempts FROM public.notification_outbox WHERE id='"+a+"'");
73 assert.equal(snapshot,'LEASED:1');
74 console.log('OUTBOX_CROSS_SESSION_VERIFIED',JSON.stringify({
75 parallelEnqueueSameId:true,parallelEnqueueStoredRows:1,
76 independentWorkerSkippedLockedRow:true,
77 noSecondClaimAfterCommit:true,attemptCount:1,
78 database:'disposable_local_ci_only'
79 }));
80 }finally{
81 if(!finished)first.kill('SIGTERM');
82 }