Bound backtest dataset preparation and preserve replay identity
This commit is contained in:
parent
cca5d71e6e
commit
6bd671d92a
20 changed files with 1420 additions and 155 deletions
|
|
@ -13,6 +13,7 @@ import { BacktestCancelledError } from '../../apps/stock/orchestrator/src/backte
|
|||
import { PostgresBacktestRunRepository } from '../../apps/stock/orchestrator/src/backtest/PostgresBacktestRunRepository';
|
||||
import { BacktestLeaseLostError } from '../../apps/stock/orchestrator/src/backtest/run-repository';
|
||||
import { RustBacktestAdapter } from '../../apps/stock/orchestrator/src/backtest/RustBacktestAdapter';
|
||||
import { DatasetLimitError } from '../../apps/stock/orchestrator/src/data/dataset-limits';
|
||||
import { loadDataset } from '../../apps/stock/orchestrator/src/data/historical-dataset';
|
||||
import { StorageService } from '../../apps/stock/orchestrator/src/services/StorageService';
|
||||
import { BacktestConfigSchema } from '../../apps/stock/orchestrator/src/types';
|
||||
|
|
@ -294,6 +295,88 @@ describe.skipIf(!url || !redisUrl)('durable backtests on real PostgreSQL and Red
|
|||
);
|
||||
});
|
||||
|
||||
test('oversized snapshots fail before pinning and persisted JSONB size is checked before transfer', async () => {
|
||||
const f = await fixture();
|
||||
const run = await f.api.createRun({ backtestId: f.definition.id });
|
||||
const config = await f.snapshot(run.id);
|
||||
const claim = await f.repository.start(config);
|
||||
const dataset = await loadDataset(f.storage, config);
|
||||
const small = new PostgresBacktestRunRepository(f.postgres, 2000, { maxBars: 1 });
|
||||
await expect(small.freezeDataset(run.id, claim.attemptId, dataset)).rejects.toBeInstanceOf(
|
||||
DatasetLimitError
|
||||
);
|
||||
expect((await f.pool.query('SELECT * FROM backtest_datasets')).rows).toHaveLength(0);
|
||||
expect(
|
||||
(await f.pool.query('SELECT dataset_hash FROM runs WHERE id = $1', [run.id])).rows[0]
|
||||
.dataset_hash
|
||||
).toBeNull();
|
||||
// A stored-text overflow during freeze must roll back both insertion and pinning too.
|
||||
const storedBudget = new PostgresBacktestRunRepository(f.postgres, 2000, { maxStoredBytes: 1 });
|
||||
await expect(
|
||||
storedBudget.freezeDataset(run.id, claim.attemptId, dataset)
|
||||
).rejects.toBeInstanceOf(DatasetLimitError);
|
||||
expect((await f.pool.query('SELECT * FROM backtest_datasets')).rows).toHaveLength(0);
|
||||
await f.repository.freezeDataset(run.id, claim.attemptId, dataset);
|
||||
await expect(storedBudget.loadDataset(run.id, claim.attemptId)).rejects.toBeInstanceOf(
|
||||
DatasetLimitError
|
||||
);
|
||||
expect((await f.repository.loadDataset(run.id, claim.attemptId))?.manifest.hash).toBe(
|
||||
dataset.manifest.hash
|
||||
);
|
||||
});
|
||||
|
||||
test('cancellation after snapshot insertion aborts preparation and rolls back the run pointer and payload', async () => {
|
||||
const f = await fixture();
|
||||
const run = await f.api.createRun({ backtestId: f.definition.id });
|
||||
const config = await f.snapshot(run.id);
|
||||
const claim = await f.repository.start(config);
|
||||
const dataset = await loadDataset(f.storage, config);
|
||||
const controller = new AbortController();
|
||||
const reason = new BacktestCancelledError();
|
||||
const repository = new PostgresBacktestRunRepository({
|
||||
transaction: work =>
|
||||
f.postgres.transaction(client =>
|
||||
work(
|
||||
new Proxy(client, {
|
||||
get(target, key) {
|
||||
if (key !== 'query') return Reflect.get(target, key);
|
||||
return async (sql: string, params?: unknown[]) => {
|
||||
const result = await target.query(sql, params);
|
||||
if (sql.startsWith('INSERT INTO backtest_datasets')) controller.abort(reason);
|
||||
return result;
|
||||
};
|
||||
},
|
||||
})
|
||||
)
|
||||
),
|
||||
});
|
||||
await expect(
|
||||
repository.freezeDataset(run.id, claim.attemptId, dataset, controller.signal)
|
||||
).rejects.toBe(reason);
|
||||
expect((await f.pool.query('SELECT * FROM backtest_datasets')).rows).toHaveLength(0);
|
||||
expect(
|
||||
(await f.pool.query('SELECT dataset_hash FROM runs WHERE id = $1', [run.id])).rows[0]
|
||||
.dataset_hash
|
||||
).toBeNull();
|
||||
await expect(
|
||||
repository.loadDataset(run.id, claim.attemptId, undefined, controller.signal)
|
||||
).rejects.toBe(reason);
|
||||
});
|
||||
|
||||
test('restored snapshots are revalidated rather than trusted by their content-addressed key', async () => {
|
||||
const f = await fixture();
|
||||
const run = await f.api.createRun({ backtestId: f.definition.id });
|
||||
const config = await f.snapshot(run.id);
|
||||
const claim = await f.repository.start(config);
|
||||
const dataset = await loadDataset(f.storage, config);
|
||||
await f.repository.freezeDataset(run.id, claim.attemptId, dataset);
|
||||
await f.pool.query(
|
||||
"UPDATE backtest_datasets SET payload = jsonb_set(payload, '{bars,0,volume}', '0'::jsonb) WHERE hash = $1",
|
||||
[dataset.manifest.hash]
|
||||
);
|
||||
await expect(f.repository.loadDataset(run.id, claim.attemptId)).rejects.toThrow('hash/content');
|
||||
});
|
||||
|
||||
test('an explicit dataset replay rejects a different listing or date range', async () => {
|
||||
const f = await fixture();
|
||||
const run = await f.api.createRun({ backtestId: f.definition.id });
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue