fix(orchestrator): own startup and shutdown resources (CL16)
This commit is contained in:
parent
21666242f4
commit
8546fce4a9
8 changed files with 384 additions and 117 deletions
150
test/integration/orchestrator-lifecycle.test.ts
Normal file
150
test/integration/orchestrator-lifecycle.test.ts
Normal file
|
|
@ -0,0 +1,150 @@
|
|||
import { asValue, type AwilixContainer } from 'awilix';
|
||||
import { expect, mock, test } from 'bun:test';
|
||||
import { ServiceContainerBuilder } from '@stock-bot/di';
|
||||
import type { StockAppConfig } from '@stock-bot/stock-config';
|
||||
import { startOrchestratorServer } from '../../apps/stock/orchestrator/src/runtime';
|
||||
import { createContainer } from '../../apps/stock/orchestrator/src/simple-container';
|
||||
import type { OrchestratorServices } from '../../apps/stock/orchestrator/src/types';
|
||||
import { logger } from '../fixtures/execution';
|
||||
|
||||
const config = { services: { orchestrator: { defaultMode: 'backtest' } } } as StockAppConfig;
|
||||
|
||||
async function resources(options: { registrationFailure?: Error; cleanupFailure?: Error } = {}) {
|
||||
// Real owned builder/lifecycle; replace disabled adapters with controlled acquired resources.
|
||||
// No client opens a socket and no global module mocking affects other tests.
|
||||
const owned = await new ServiceContainerBuilder()
|
||||
.withOptions({
|
||||
enableCache: false,
|
||||
enableQueue: false,
|
||||
enableMongoDB: false,
|
||||
enablePostgres: false,
|
||||
enableQuestDB: false,
|
||||
enableBrowser: false,
|
||||
enableProxy: false,
|
||||
})
|
||||
.buildOwned();
|
||||
const events: string[] = [];
|
||||
const closed = new Map<string, number>();
|
||||
const adapter = (name: string) => ({
|
||||
async disconnect() {
|
||||
events.push(name);
|
||||
closed.set(name, (closed.get(name) ?? 0) + 1);
|
||||
if (name === 'questdb' && options.cleanupFailure) {
|
||||
throw options.cleanupFailure;
|
||||
}
|
||||
},
|
||||
});
|
||||
const postgres = adapter('postgres');
|
||||
const queue = {
|
||||
getQueue() {
|
||||
if (options.registrationFailure) {
|
||||
const runner = services.custom!.BacktestRunner!;
|
||||
const shutdown = runner.shutdown.bind(runner);
|
||||
runner.shutdown = mock(async () => {
|
||||
events.push('runner');
|
||||
await shutdown();
|
||||
});
|
||||
throw options.registrationFailure;
|
||||
}
|
||||
},
|
||||
shutdown: mock(async () => {
|
||||
events.push('queue');
|
||||
}),
|
||||
};
|
||||
const services = { logger, postgres, queueManager: queue } as unknown as OrchestratorServices;
|
||||
// The fixture supplies lifecycle-only adapters, not production database/query APIs.
|
||||
(owned.container as AwilixContainer).register({
|
||||
serviceContainer: asValue(services),
|
||||
cache: asValue(adapter('cache')),
|
||||
mongoClient: asValue(adapter('mongo')),
|
||||
postgresClient: asValue(postgres),
|
||||
questdbClient: asValue(adapter('questdb')),
|
||||
queueManager: asValue(queue),
|
||||
});
|
||||
return { owned, events, closed, services, queue };
|
||||
}
|
||||
|
||||
test('registration failure closes app work once then every shared resource, retaining original error', async () => {
|
||||
const failure = new Error('queue registration failed');
|
||||
const f = await resources({
|
||||
registrationFailure: failure,
|
||||
cleanupFailure: new Error('close failed'),
|
||||
});
|
||||
await expect(createContainer(config, {}, async () => f.owned)).rejects.toBe(failure);
|
||||
expect(f.events).toEqual(['runner', 'queue', 'questdb', 'postgres', 'mongo', 'cache']);
|
||||
expect(f.services.custom!.BacktestRunner!.shutdown).toHaveBeenCalledTimes(1);
|
||||
await f.owned.shutdown();
|
||||
expect([...f.closed.values()]).toEqual([1, 1, 1, 1]);
|
||||
expect(f.queue.shutdown).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
test('HTTP bind failure releases initialized runtime and preserves the bind error', async () => {
|
||||
const failure = new Error('address already in use');
|
||||
const f = await resources();
|
||||
await expect(
|
||||
startOrchestratorServer(
|
||||
() => createContainer(config, {}, async () => f.owned),
|
||||
() => {
|
||||
throw failure;
|
||||
}
|
||||
)
|
||||
).rejects.toBe(failure);
|
||||
expect(f.events).toEqual(['queue', 'questdb', 'postgres', 'mongo', 'cache']);
|
||||
expect(f.queue.shutdown).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
test('normal repeated shutdown waits for durable work before closing PostgreSQL', async () => {
|
||||
const f = await resources();
|
||||
const { runtime, server } = await startOrchestratorServer(
|
||||
() => createContainer(config, {}, async () => f.owned),
|
||||
services => ({ services })
|
||||
);
|
||||
expect(server.services).toBe(runtime.services);
|
||||
expect(f.events).toEqual([]);
|
||||
let finish!: () => void;
|
||||
const drain = new Promise<void>(resolve => {
|
||||
finish = resolve;
|
||||
});
|
||||
runtime.services.custom!.BacktestRunner!.shutdown = mock(async () => {
|
||||
await drain;
|
||||
expect(f.closed.has('postgres')).toBe(false);
|
||||
f.events.push('terminal-write');
|
||||
});
|
||||
const first = runtime.shutdown();
|
||||
expect(runtime.shutdown()).toBe(first);
|
||||
await Promise.resolve();
|
||||
expect(f.events).toEqual([]);
|
||||
finish();
|
||||
await first;
|
||||
await runtime.shutdown();
|
||||
expect(f.events).toEqual(['terminal-write', 'queue', 'questdb', 'postgres', 'mongo', 'cache']);
|
||||
expect(f.queue.shutdown).toHaveBeenCalledTimes(1);
|
||||
expect([...f.closed.values()]).toEqual([1, 1, 1, 1]);
|
||||
});
|
||||
|
||||
test('app cleanup failure still closes shared resources and is reported', async () => {
|
||||
const f = await resources();
|
||||
const runtime = await createContainer(config, {}, async () => f.owned);
|
||||
runtime.services.custom!.BacktestRunner!.shutdown = mock(async () => {
|
||||
throw new Error('runner close failed');
|
||||
});
|
||||
await expect(runtime.shutdown()).rejects.toThrow('Orchestrator shutdown failed');
|
||||
expect(f.events).toEqual(['queue', 'questdb', 'postgres', 'mongo', 'cache']);
|
||||
await expect(runtime.shutdown()).rejects.toThrow('Orchestrator shutdown failed');
|
||||
expect(f.queue.shutdown).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
test('service aggregate resolution failure releases the acquired shared owner', async () => {
|
||||
const f = await resources();
|
||||
const failure = new Error('service aggregate failed');
|
||||
const resolve = f.owned.container.resolve.bind(f.owned.container);
|
||||
f.owned.container.resolve = ((key: string, options?: object) => {
|
||||
if (key === 'serviceContainer') {
|
||||
throw failure;
|
||||
}
|
||||
return resolve(key, options);
|
||||
}) as typeof f.owned.container.resolve;
|
||||
await expect(createContainer(config, {}, async () => f.owned)).rejects.toBe(failure);
|
||||
expect(f.queue.shutdown).toHaveBeenCalledTimes(1);
|
||||
expect([...f.closed.values()]).toEqual([1, 1, 1, 1]);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue