forked from unimatrix/web
15 lines
2 KiB
JavaScript
15 lines
2 KiB
JavaScript
import test from 'node:test';import assert from 'node:assert/strict';
|
|
import {BankFixture} from './postbank-fixture.mjs';import {CentralScheduler} from '../node/central-scheduler.mjs';
|
|
test('Concurrent schedulers lease one due slot; manual reruns cannot force another run',async()=>{
|
|
const db=new BankFixture();let calls=0,release,entered;const started=new Promise(r=>{entered=r;}),wait=new Promise(r=>{release=r;});
|
|
const tasks=[{key:'test_due',slot:()=>10,async run(){calls++;entered();await wait;}}];
|
|
try{const a=new CentralScheduler(db,tasks),b=new CentralScheduler(db,tasks),running=a.runDue();await started;assert.equal((await b.runDue()).tasks[0].state,'not_due_or_running');release();assert.equal((await running).tasks[0].state,'completed');assert.equal((await b.runDue()).tasks[0].state,'not_due_or_running');assert.equal(calls,1);}finally{db.close();}
|
|
});
|
|
test('Failures release the lease without marking a slot complete; retry does not block other jobs',async()=>{
|
|
const db=new BankFixture();let broken=true,other=0;
|
|
const scheduler=new CentralScheduler(db,[{key:'retry',slot:()=>20,async run(){if(broken)throw Error('private details must not be stored');}},{key:'healthy',slot:()=>20,async run(){other++;}}]);
|
|
try{const first=await scheduler.runDue();assert.equal(first.tasks[0].state,'retry_pending');assert.equal(other,1);const stored=await db.first("SELECT * FROM pb_scheduler_tasks WHERE task_key='retry'");assert.equal(stored.last_slot,-1);assert.equal(stored.lease_until,0);assert(!stored.last_error.includes('private details'));broken=false;assert.equal((await scheduler.runDue()).tasks[0].state,'completed');assert.equal(other,1);}finally{db.close();}
|
|
});
|
|
test('Disabled and future tasks do not acquire a run record',async()=>{
|
|
const db=new BankFixture();try{const scheduler=new CentralScheduler(db,[{key:'future',slot:()=>null,run:()=>{throw Error('Must not run');}}]);assert.deepEqual(await scheduler.runDue(),{tasks:[]});assert.equal((await db.first('SELECT COUNT(*) n FROM pb_scheduler_tasks')).n,0);}finally{db.close();}
|
|
});
|

Staff