fix: bound hourly step awaits by the per-step wait limit (#56)
Backend verify / commits (pull_request) Successful in 32s
Backend verify / security (pull_request) Successful in 12s
Backend verify / verify (pull_request) Successful in 5m45s

This commit is contained in:
2026-10-12 22:17:53 +03:00
parent b9b76bab91
commit 52fa3a36ba
2 changed files with 138 additions and 34 deletions
+57
View File
@@ -96,6 +96,19 @@ describe('hourly attempt lifecycle (SQLite)', () => {
throw new Error('hourly attempt did not settle');
}
// Sequential per-step bounds: advance past each bound until the attempt
// closes. Covers walks whose awaits hang before any durable step row exists.
async function runBoundedWalk(scheduler: TbankHourlyScheduler) {
await scheduler.tick();
for (let i = 0; i < 12; i++) {
await pump(5);
await jest.advanceTimersByTimeAsync(16 * 60 * 1000);
const row = await hourlyRow();
if (row && row.state !== 'running') return row;
}
throw new Error('hourly attempt did not settle');
}
beforeAll(async () => {
await prisma.$connect();
await prisma.user.create({
@@ -259,6 +272,50 @@ describe('hourly attempt lifecycle (SQLite)', () => {
assert.ok(stuck, 'the wait bound does not cancel the upstream call');
});
it('settles the attempt when a portfolio dispatch never resolves', async () => {
portfolio.request.mockImplementation(() => new Promise(() => undefined));
const row = await runBoundedWalk(createScheduler());
const progress = row.progress as HourlyProgressView;
expect(progress.result).toBe('partial');
const problems = progress.sections.filter(
(entry) => entry.section === 'portfolio',
);
expect(problems).toHaveLength(2);
expect(problems[0].errorCode).toBe('TBANK_UNAVAILABLE');
});
it('settles the attempt when the membership dispatch never resolves', async () => {
membership.request.mockImplementation(() => new Promise(() => undefined));
const row = await runBoundedWalk(createScheduler());
const progress = row.progress as HourlyProgressView;
expect(progress.result).toBe('partial');
expect(progress.sections).toHaveLength(1);
expect(progress.sections[0].section).toBe('membership');
expect(progress.sections[0].errorCode).toBe('TBANK_UNAVAILABLE');
});
it('bounds a membership step promise that never resolves', async () => {
membership.request.mockImplementation(async () => {
const row = await child('running', null, 'membership');
return { id: row.id, done: new Promise(() => undefined) };
});
const row = await runBoundedWalk(createScheduler());
const progress = row.progress as HourlyProgressView;
expect(progress.result).toBe('partial');
const membershipSection = progress.sections.find(
(entry) => entry.section === 'membership',
);
expect(membershipSection?.errorCode).toBe('DEADLINE');
});
it('settles the attempt when a post-wait refresh never resolves', async () => {
events.get.mockImplementation(() => new Promise(() => undefined));
const row = await runBoundedWalk(createScheduler());
const progress = row.progress as HourlyProgressView;
expect(progress.result).toBe('succeeded');
expect(progress.sections).toEqual([]);
});
it('recovers a lost settle and unblocks the next attempt at its term', async () => {
const settleSpy = jest
.spyOn(repository, 'settleHourly')