perf: streamline rule reads and unblock background discovery
This commit is contained in:
1 parent
b14fa2f4d1
commit
b17b975812
19 files changed
+263
-42
No files matched your search
@@ -0,0 +1,67 @@
|
||||
'use strict';
|
||||
const test=require('node:test'),assert=require('node:assert/strict');
|
||||
const {OrderExecutionCoordinator}=require('../../src/modules/order-execution/coordinator.cjs');
|
||||
const now='2026-10-26T02:00:00.000Z';
|
||||
function deferred(){let resolve,reject;const promise=new Promise((yes,no)=>{resolve=yes;reject=no;});return {promise,resolve,reject};}
|
||||
function fixture(t){
|
||||
const records=new Map(),connection={id:'c1',owner:'admin',status:'connected'};
|
||||
const store={
|
||||
all:kind=>[...(records.get(kind)?.values()||[])].map(value=>structuredClone(value)),
|
||||
get:(kind,id)=>structuredClone(records.get(kind)?.get(id)||null),
|
||||
put(kind,value){if(!records.has(kind))records.set(kind,new Map());records.get(kind).set(value.id,structuredClone(value));},
|
||||
transaction:work=>work(),settings:()=>({processingEnabled:true}),
|
||||
clock:()=>new Date(now),schedule:()=>({times:['11:00','17:00'],weekends:false}),calendar:()=>[],
|
||||
snapshot:()=>({window:{available:false}}),
|
||||
};
|
||||
const execution=new OrderExecutionCoordinator({store,workspace:{snapshot:()=>({connections:[connection]})},archive:{},clock:()=>new Date(now),systemPrincipal:{user:{username:'admin',role:'admin'},sessionId:'isolated-test'}});
|
||||
t.after(()=>execution.stop());
|
||||
return {execution,store,notify:runId=>execution.notifyDiscovery({connectionId:connection.id,runId,rows:[],startedAt:now,finishedAt:now})};
|
||||
}
|
||||
|
||||
test('overlapping background ticks share the running discovery pass without duplicate scans',async t=>{
|
||||
const f=fixture(t),entered=deferred(),release=deferred(),calls=[];
|
||||
f.notify('first');f.notify('second');
|
||||
f.execution.scan=async(input,principal,hint)=>{
|
||||
calls.push(hint.runId);
|
||||
if(calls.length===1){entered.resolve();await release.promise;}
|
||||
f.store.put('execution-scans',{id:'c1',status:'complete',startedAt:now,finishedAt:now});
|
||||
};
|
||||
const first=f.execution.background();await entered.promise;
|
||||
const overlap=Array.from({length:4},()=>f.execution.background());
|
||||
const callsWhileBlocked=[...calls];
|
||||
// Arrivals during a pass remain durable and are processed by the next pass.
|
||||
f.notify('third');release.resolve();await Promise.all([first,...overlap]);
|
||||
assert.deepEqual(callsWhileBlocked,['first'],'a slow capture must not queue the same discovery again');
|
||||
assert.deepEqual(calls,['first','second']);
|
||||
assert.equal(f.store.all('execution-discovery-runs').find(hint=>hint.runId==='third').status,'pending');
|
||||
await f.execution.background();
|
||||
assert.deepEqual(calls,['first','second','third']);
|
||||
assert.ok(f.store.all('execution-discovery-runs').every(hint=>hint.status==='complete'));
|
||||
});
|
||||
|
||||
test('background guard releases after an unexpected failure so the next tick can recover',async t=>{
|
||||
const f=fixture(t),entered=deferred(),release=deferred();let calls=0;
|
||||
f.execution.performBackground=async()=>{calls++;entered.resolve();await release.promise;throw Error('isolated background failure');};
|
||||
const first=f.execution.background();await entered.promise;
|
||||
const second=f.execution.background();release.resolve();
|
||||
const results=await Promise.allSettled([first,second]);
|
||||
assert.equal(calls,1);assert.ok(results.every(result=>result.status==='rejected'));
|
||||
f.execution.performBackground=async()=>{calls++;};
|
||||
await f.execution.background();assert.equal(calls,2);
|
||||
});
|
||||
|
||||
test('stop waits for the background pass to persist its final discovery receipt',async t=>{
|
||||
const f=fixture(t),entered=deferred(),release=deferred();
|
||||
f.notify('shutdown');
|
||||
f.execution.scan=async()=>{entered.resolve();await release.promise;};
|
||||
const running=f.execution.background();await entered.promise;
|
||||
let stopped=false;
|
||||
const stopping=f.execution.stop().then(()=>{stopped=true;});
|
||||
// Allow a drain-only stop to resolve while its background continuation is pending.
|
||||
await new Promise(resolve=>setImmediate(resolve));
|
||||
const premature=stopped;
|
||||
release.resolve();await Promise.all([running,stopping]);
|
||||
assert.equal(premature,false,'shutdown must not close the store before final background writes');
|
||||
assert.equal(f.store.all('execution-discovery-runs')[0].status,'complete');
|
||||
assert.equal(f.store.get('execution-discoveries','c1').status,'complete');
|
||||
});
|
||||
Reference in new issue
Block a user