52 lines
1.8 KiB
TypeScript
52 lines
1.8 KiB
TypeScript
import {
|
|
nativeWorkerRequestSchema,
|
|
type NativeWorkerMessage,
|
|
} from '../../apps/stock/orchestrator/src/backtest/native-worker-protocol';
|
|
|
|
function send(message: NativeWorkerMessage | { invalid: boolean }): Promise<void> {
|
|
return new Promise((resolve, reject) => {
|
|
process.send!(message, (error: Error | null) => (error ? reject(error) : resolve()));
|
|
});
|
|
}
|
|
|
|
process.once('message', async (input: unknown) => {
|
|
const { config } = nativeWorkerRequestSchema.parse(input);
|
|
// Fixture controls are run labels, not unsupported production strategy parameters.
|
|
const behavior = config.name?.startsWith('fixture:')
|
|
? config.name.slice('fixture:'.length)
|
|
: undefined;
|
|
if (!behavior) {
|
|
// The same adapter can recover and run the actual Rust engine after a failed worker.
|
|
await import('../../apps/stock/orchestrator/src/backtest/native-worker');
|
|
process.emit('message', input);
|
|
return;
|
|
}
|
|
await send({ type: 'progress', progress: 0, message: `Fixture worker ready:${process.pid}` });
|
|
switch (behavior) {
|
|
case 'block':
|
|
// Intentionally block this process. Messages, timers, and cooperative stop cannot run.
|
|
while (true) {
|
|
Math.sqrt(Math.random());
|
|
}
|
|
case 'crash':
|
|
process.kill(process.pid, 'SIGKILL');
|
|
break;
|
|
case 'malformed':
|
|
await send({ invalid: true });
|
|
break;
|
|
case 'invalid-result':
|
|
await send({ type: 'result', result: '{"not":"a backtest result"}' });
|
|
process.disconnect?.();
|
|
break;
|
|
case 'reported-error':
|
|
await send({ type: 'error', message: 'Fixture native failure' });
|
|
break;
|
|
case 'no-result':
|
|
process.exit(0);
|
|
case 'result-then-block':
|
|
await send({ type: 'result', result: '{}' });
|
|
while (true) {
|
|
Math.sqrt(Math.random());
|
|
}
|
|
}
|
|
});
|