Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions agents-api/src/__tests__/ready.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,28 +106,35 @@ describe('GET /ready', () => {
});

it('runs database checks in parallel', async () => {
let manageDbStartTime = 0;
let runDbStartTime = 0;
const events: string[] = [];

vi.mocked(healthChecks.checkManageDb).mockImplementation(async () => {
manageDbStartTime = performance.now();
events.push('manageDb:start');
await new Promise((resolve) => setTimeout(resolve, 10));
events.push('manageDb:end');
return true;
});

vi.mocked(healthChecks.checkRunDb).mockImplementation(async () => {
runDbStartTime = performance.now();
events.push('runDb:start');
await new Promise((resolve) => setTimeout(resolve, 10));
events.push('runDb:end');
return true;
});

const startTime = performance.now();
await app.request('/ready');
const elapsed = performance.now() - startTime;

// If run in parallel, both checks should start nearly simultaneously
// and total time should be ~10ms, not ~20ms
expect(Math.abs(manageDbStartTime - runDbStartTime)).toBeLessThan(5);
expect(elapsed).toBeLessThan(50); // Allow some overhead but not 2x sequential time
// If run in parallel, both checks start before either finishes.
// Sequential execution would produce: start, end, start, end.
const manageStart = events.indexOf('manageDb:start');
const runStart = events.indexOf('runDb:start');
const manageEnd = events.indexOf('manageDb:end');
const runEnd = events.indexOf('runDb:end');

expect(manageStart).toBeLessThan(manageEnd);
expect(runStart).toBeLessThan(runEnd);
// Both started before either finished — proves parallelism
expect(manageStart).toBeLessThan(runEnd);
expect(runStart).toBeLessThan(manageEnd);
});
});
Loading