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

58 lines
9.4 KiB
JavaScript

'use strict';
const {test}=require('node:test'),assert=require('node:assert/strict'),fs=require('node:fs/promises'),os=require('node:os'),path=require('node:path'),net=require('node:net');
async function port(){const s=net.createServer();await new Promise(r=>s.listen(0,'127.0.0.1',r));const p=s.address().port;await new Promise(r=>s.close(r));return p;}
async function until(fn,timeout=15000){const end=Date.now()+timeout;while(Date.now()<end){if(await fn())return;await new Promise(r=>setTimeout(r,30));}assert.fail('condition did not become true');}
test('real CLI retry acknowledges a fresh attempt before awaiting delayed port configuration', {timeout:20000},async t=>{
const {ServiceSupervisor}=require('../../src/platform/service/supervisor.cjs'),root=await fs.mkdtemp(path.join(os.tmpdir(),'guard-cli-retry-')),stateDir=path.join(root,'OrderNotificationAutomation'),config=path.join(root,'ports.json'),listenPort=await port();let guard,release,entered=false,invalid=false,delay=false,cli,exit;
t.after(async()=>{release?.([listenPort]);cli?.kill();await guard?.stop();await fs.rm(root,{recursive:true,force:true});});
await fs.writeFile(config,JSON.stringify({port:listenPort}));
guard=new ServiceSupervisor({stateDir,entry:path.resolve('tests/fixtures/reload-ports-service.cjs'),portsProvider:()=>invalid?['invalid']:delay?new Promise(resolve=>{entered=true;release=resolve;}):[listenPort],env:{ORDER_FIXTURE_CONFIG:config},timing:{intervalMs:40,pingTimeoutMs:80,startupGraceMs:2000,stopGraceMs:500}});
await guard.start();await until(()=>guard.snapshot().status==='running');invalid=true;await assert.rejects(guard.restart());assert.equal(guard.snapshot().status,'failed');invalid=false;delay=true;
const completed=new Promise(resolve=>{cli=require('node:child_process').execFile(process.execPath,[path.resolve('scripts/service.cjs'),'restart'],{env:{...process.env,LOCALAPPDATA:root},windowsHide:true,timeout:12000},(error,stdout,stderr)=>{exit={error,stdout,stderr};resolve(exit);});});
await until(()=>entered);const during=guard.snapshot();await new Promise(r=>setTimeout(r,1200));const earlyExit=exit;release([listenPort]);delay=false;
const result=await completed;await until(()=>guard.snapshot().status==='running');
assert.equal(during.status,'starting');assert.equal(during.reason,'manual_restart');assert.equal(earlyExit,undefined,'CLI must wait for this attempt rather than fail on the previous attempt');assert.equal(result.error,null,result.stderr);assert.match(result.stdout,/服务已重启/);
invalid=true;await assert.rejects(guard.restart());invalid=false;delay=true;entered=false;
const reply=await require('../../src/platform/service/control.cjs').requestControl({stateDir,action:'restart'});await until(()=>entered);release([listenPort]);delay=false;await until(()=>guard.snapshot().status==='running');
assert.equal(reply.state.status,'starting','control acknowledgement must follow publishing the new attempt');assert.equal(reply.state.reason,'manual_restart');
});
test('each real child launch reloads ports and refuses an unknown occupant before spawning', {timeout:30000},async t=>{
const {ServiceSupervisor}=require('../../src/platform/service/supervisor.cjs'),stateDir=await fs.mkdtemp(path.join(os.tmpdir(),'guard-reload-')),config=path.join(stateDir,'ports.json'),a=await port(),b=await port();let guard,owner;
t.after(async()=>{await guard?.stop();if(owner?.listening)await new Promise(r=>owner.close(r));await fs.rm(stateDir,{recursive:true,force:true});});
await fs.writeFile(config,JSON.stringify({port:a}));let reads=0;
guard=new ServiceSupervisor({stateDir,entry:path.resolve('tests/fixtures/reload-ports-service.cjs'),ports:[a],portsProvider:async()=>{reads++;return [JSON.parse(await fs.readFile(config,'utf8')).port];},env:{ORDER_FIXTURE_CONFIG:config},timing:{intervalMs:50,pingTimeoutMs:100,startupGraceMs:2000,stopGraceMs:1000}});
await guard.start();await until(()=>guard.snapshot().status==='running');await fs.writeFile(config,JSON.stringify({port:b}));await guard.restart();await until(()=>guard.snapshot().status==='running');assert.deepEqual(guard.ports,[b]);assert.equal(reads,2);
owner=net.createServer();await new Promise(r=>owner.listen(a,'127.0.0.1',r));await fs.writeFile(config,JSON.stringify({port:a}));await assert.rejects(guard.restart(),e=>e.code==='EADDRINUSE');assert.equal(guard.child,null);assert.equal(owner.listening,true);assert.equal(guard.snapshot().status,'failed');assert.equal(guard.snapshot().reason,'port_conflict');
await fs.writeFile(config,JSON.stringify({port:'invalid'}));await assert.rejects(guard.restart(),/端口/);assert.equal(guard.child,null);assert.equal(guard.snapshot().status,'failed');assert.equal(guard.snapshot().reason,'config_invalid');
await fs.writeFile(config,JSON.stringify({port:b}));await guard.restart();await until(()=>guard.snapshot().status==='running');assert.deepEqual(guard.ports,[b]);assert.equal(owner.listening,true);
});
test('external guard recovers a crashed child and a live but unresponsive child, then stops without respawning',{timeout:60000},async t=>{
const {ServiceSupervisor}=require('../../src/platform/service/supervisor.cjs'),{requestControl,readSupervisor}=require('../../src/platform/service/control.cjs');
const stateDir=await fs.mkdtemp(path.join(os.tmpdir(),'order-guard-')),listenPort=await port(),countFile=path.join(stateDir,'count');let guard;
t.after(async()=>{await guard?.stop();await fs.rm(stateDir,{recursive:true,force:true});});
guard=new ServiceSupervisor({stateDir,entry:path.resolve('tests/fixtures/managed-service.cjs'),ports:[listenPort],env:{ORDER_FIXTURE_PORT:String(listenPort),ORDER_FIXTURE_COUNT:countFile},timing:{intervalMs:50,pingTimeoutMs:80,startupGraceMs:1800,failureThreshold:2,stopGraceMs:500,backoffMs:[30,50,80],cooldownMs:500,windowMs:30000,maxRestarts:8}});
await guard.start();await until(()=>guard.snapshot().status==='running'&&guard.snapshot().restartCount>=2,30000);
const state=guard.snapshot();assert.ok(state.childPid>0);assert.ok(state.heartbeatAt);assert.ok(Number(await fs.readFile(countFile,'utf8'))>=3);
assert.equal(await fs.readFile(countFile+'.heartbeat','utf8')===await fs.readFile(path.join(stateDir,'supervisor','control.key'),'utf8'),false,'HTTP heartbeats must never send the management token');
const {controlAddress}=require('../../src/platform/service/control.cjs');
for(const value of [null,42,'text',[],true,{token:{toString:null},action:'stop'}]){
const reply=await new Promise((resolve,reject)=>{const s=net.createConnection(controlAddress(stateDir));s.setTimeout(600,()=>{s.destroy();reject(Error('malformed control request was not rejected'));});s.on('error',reject);s.on('connect',()=>s.write(JSON.stringify(value)+'\n'));s.on('data',data=>{s.destroy();resolve(JSON.parse(data.toString('utf8')));});});assert.equal(reply.status,value&&typeof value==='object'&&!Array.isArray(value)?403:400);
}
assert.equal((await requestControl({stateDir,action:'status'})).state.childPid,state.childPid);
assert.ok(readSupervisor(stateDir).events.some(e=>e.kind==='unresponsive'));
await assert.rejects(requestControl({stateDir,action:'stop',token:'bad-token'}),e=>e.status===403);
assert.equal((await requestControl({stateDir,action:'status'})).state.childPid,state.childPid);
const duplicate=new ServiceSupervisor({stateDir,entry:path.resolve('tests/fixtures/managed-service.cjs'),ports:[listenPort]});await assert.rejects(duplicate.start(),e=>e.code==='EADDRINUSE');assert.equal(guard.snapshot().childPid,state.childPid);
await requestControl({stateDir,action:'restart'});await until(()=>guard.snapshot().status==='running'&&guard.snapshot().childInstanceId!==state.childInstanceId);assert.ok(guard.snapshot().restartCount>=3);
await guard.stop();const generations=await fs.readFile(countFile,'utf8');await new Promise(r=>setTimeout(r,250));assert.equal(await fs.readFile(countFile,'utf8'),generations);assert.equal(readSupervisor(stateDir).state.status,'stopped');
});
test('unknown port owners remain running, and repeated real exits enter cooldown',{timeout:60000},async t=>{
const {ServiceSupervisor}=require('../../src/platform/service/supervisor.cjs'),stateDir=await fs.mkdtemp(path.join(os.tmpdir(),'order-guard-conflict-')),listenPort=await port(),owner=net.createServer(socket=>socket.end('owner'));await new Promise(r=>owner.listen(listenPort,'127.0.0.1',r));let guard;
t.after(async()=>{await guard?.stop();if(owner.listening)await new Promise(r=>owner.close(r));await fs.rm(stateDir,{recursive:true,force:true});});
const blocked=new ServiceSupervisor({stateDir,ports:[listenPort]});await assert.rejects(blocked.start(),e=>e.code==='EADDRINUSE');assert.equal(owner.listening,true);await new Promise(r=>owner.close(r));
guard=new ServiceSupervisor({stateDir,entry:path.resolve('tests/fixtures/managed-service.cjs'),ports:[listenPort],env:{ORDER_FIXTURE_PORT:String(listenPort),ORDER_FIXTURE_COUNT:path.join(stateDir,'count'),ORDER_FIXTURE_ALWAYS_CRASH:'1'},timing:{intervalMs:40,pingTimeoutMs:50,startupGraceMs:1000,backoffMs:[30],maxRestarts:1,cooldownMs:1000,windowMs:30000,stopGraceMs:500}});await guard.start();await until(()=>guard.snapshot().status==='cooldown');assert.ok(guard.snapshot().nextRestartAt);await guard.stop();
});
test('restart backoff is bounded and repeated failures enter a cooldown',()=>{
const {restartPlan}=require('../../src/platform/service/supervisor.cjs');assert.deepEqual([0,1,2,20].map(failures=>restartPlan({failures,recent:0}).delayMs),[2000,5000,10000,60000]);assert.equal(restartPlan({failures:9,recent:8}).cooldown,true);assert.equal(restartPlan({failures:9,recent:8}).delayMs,300000);
});