Files
order-notification-automation/tests/service/instance-lock.test.cjs
T

17 lines
1.6 KiB
JavaScript

'use strict';
const {test}=require('node:test');const assert=require('node:assert/strict');const fs=require('node:fs/promises');const os=require('node:os');const path=require('node:path');const {fork}=require('node:child_process');
const lockModule=path.resolve(__dirname,'../../src/platform/service/instance-lock.cjs');
test('canonical state directory is exclusive and released idempotently',async t=>{
const {acquireInstanceLock}=require(lockModule);const dir=await fs.mkdtemp(path.join(os.tmpdir(),'order-owner-'));t.after(()=>fs.rm(dir,{recursive:true,force:true}));
const owner=await acquireInstanceLock(dir);try{await assert.rejects(acquireInstanceLock(path.join(dir,'.')),e=>e.code==='ORDER_INSTANCE_OWNED');}finally{await owner.release();await owner.release();}
const next=await acquireInstanceLock(dir);await next.release();
});
test('OS releases cross-process state ownership when the owner crashes',async t=>{
const {acquireInstanceLock}=require(lockModule);const dir=await fs.mkdtemp(path.join(os.tmpdir(),'order-owner-crash-'));t.after(()=>fs.rm(dir,{recursive:true,force:true}));
const child=fork(path.join(__dirname,'fixtures/instance-owner.cjs'),[dir],{stdio:['ignore','ignore','pipe','ipc']});t.after(()=>{if(child.exitCode===null)child.kill();});
await new Promise((resolve,reject)=>{child.once('message',resolve);child.once('error',reject);child.once('exit',code=>reject(new Error('owner exited '+code)));});
await assert.rejects(acquireInstanceLock(dir),e=>e.code==='ORDER_INSTANCE_OWNED');
const exited=new Promise(resolve=>child.once('exit',resolve));child.kill('SIGKILL');await exited;
const next=await acquireInstanceLock(dir);await next.release();
});