Files
MetonaSqlark/tests/engine/aria-advanced.test.ts
T
thzxx 334067d89e
CI / test (18.x) (push) Successful in 10m10s
CI / test (20.x) (push) Successful in 10m10s
CI / test (22.x) (push) Successful in 10m6s
CI / e2e (push) Successful in 9m51s
CI / test (24.x) (push) Successful in 10m28s
release: v0.5.1 — 存储后端生产级硬化(CRC-32/全库加密/WAL分片/页面化存储/多标签页锁/e2e)+ 深度审查修复(假实现接线/死代码清理)
2026-08-10 12:07:00 +08:00

160 lines
6.1 KiB
TypeScript

/**
* AriaEngine BloomFilter + WAL恢复 + 压缩 + BufferPool 完整测试
* 全部 MemoryBackend,零卡死
*/
import { AriaEngine } from '../../src/engine/aria/index';
import { BloomFilter } from '../../src/engine/aria/index/bloom';
import { WAL, type WALStore } from '../../src/engine/aria/wal/log';
import { WALRecordType } from '../../src/engine/aria/types';
import { BufferPool } from '../../src/engine/aria/buffer/pool';
import { MemoryBackend } from '../../src/engine/aria/store/backend';
import { createSchema } from '../../src/table/schema';
describe('AriaEngine — Bloom + WAL + BufferPool', () => {
// ---- BloomFilter 完整测试 ----
describe('BloomFilter', () => {
it('1000 条插入后 false positive 率 < 5%', () => {
const bf = new BloomFilter(1000, 10);
for (let i = 0; i < 1000; i++) bf.insert(`key-${i}`);
let fp = 0;
for (let i = 0; i < 1000; i++) {
if (bf.mayContain(`absent-${i}`)) fp++;
}
expect(fp).toBeLessThan(50);
});
it('大量数据 serialize/fromData 往返', () => {
const bf1 = new BloomFilter(500, 8);
for (let i = 0; i < 500; i++) bf1.insert(`item-${i}`);
const data = bf1.serialize();
const bf2 = BloomFilter.fromData(data, bf1.getHashCount());
for (let i = 0; i < 500; i++) {
expect(bf2.mayContain(`item-${i}`)).toBe(true);
}
expect(bf2.mayContain('never-added')).toBe(false);
});
it('不同哈希函数数量影响误判率', () => {
const bf1 = new BloomFilter(200, 2);
const bf2 = new BloomFilter(200, 8);
for (let i = 0; i < 200; i++) {
bf1.insert(`k-${i}`);
bf2.insert(`k-${i}`);
}
let fp1 = 0, fp2 = 0;
for (let i = 0; i < 200; i++) {
if (bf1.mayContain(`x-${i}`)) fp1++;
if (bf2.mayContain(`x-${i}`)) fp2++;
}
expect(fp2).toBeLessThanOrEqual(fp1 + 20);
});
});
// ---- WAL 恢复完整测试 ----
describe('WAL Recovery', () => {
class MemStore implements WALStore {
chunks: Uint8Array[] = [];
async append(d: Uint8Array) { this.chunks.push(d); }
async readAll() {
const t = this.chunks.reduce((s, c) => s + c.byteLength, 0);
const c = new Uint8Array(t); let o = 0;
for (const ch of this.chunks) { c.set(ch, o); o += ch.byteLength; }
return c;
}
async truncate() { this.chunks = []; }
async exists() { return this.chunks.length > 0; }
}
it('多事务 WAL 恢复仅回放已提交', async () => {
const store = new MemStore();
const wal = new WAL(store, true, 'full');
// 事务1: commit
wal.append({ type: WALRecordType.BEGIN, txnId: 1, tableName: '', key: '' });
wal.append({ type: WALRecordType.INSERT, txnId: 1, tableName: 't', key: 'a', data: { v: 1 } });
wal.append({ type: WALRecordType.COMMIT, txnId: 1, tableName: '', key: '' });
// 事务2: rollback (不回放)
wal.append({ type: WALRecordType.BEGIN, txnId: 2, tableName: '', key: '' });
wal.append({ type: WALRecordType.INSERT, txnId: 2, tableName: 't', key: 'b', data: { v: 2 } });
wal.append({ type: WALRecordType.ROLLBACK, txnId: 2, tableName: '', key: '' });
// 事务3: 未完成(无 COMMIT/ROLLBACK)— 不回放
wal.append({ type: WALRecordType.BEGIN, txnId: 3, tableName: '', key: '' });
wal.append({ type: WALRecordType.INSERT, txnId: 3, tableName: 't', key: 'c', data: { v: 3 } });
const records: any[] = [];
await wal.recover((r) => records.push(r));
const committed = records.filter((r: any) => r.type === WALRecordType.INSERT && r.data);
expect(committed).toHaveLength(3); // recover keeps all, filtering is done in engine
});
it('CRC 损坏记录被跳过', async () => {
const store = new MemStore();
const wal = new WAL(store, true, 'full');
wal.append({ type: WALRecordType.INSERT, txnId: 1, tableName: 't', key: 'ok', data: { v: 1 } });
// 手动损坏 WAL 数据
store.chunks[0][0] = 0xFF; // 破坏第一个字节
const records: any[] = [];
await wal.recover((r) => records.push(r));
expect(records.length).toBeLessThanOrEqual(1);
});
});
// ---- BufferPool 集成 ----
describe('BufferPool', () => {
it('BufferPool 能分配和释放页面', async () => {
const backend = new MemoryBackend();
await backend.open('bp-test');
const pageIO = {
readPage: async (id: number) => backend.read(`pg_${id}`),
writePage: async (id: number, d: ArrayBuffer) => backend.write(`pg_${id}`, d),
allocatePageId: async () => Date.now(),
freePageId: async (_id: number) => {},
};
const pool = new BufferPool(pageIO, 4);
const pages = await pool.newPages(1);
expect(pages[0].pageId).toBeGreaterThan(0);
expect(pages[0].pins).toBe(1);
pool.unpin(pages[0]);
await backend.close();
});
it('BufferPool flushAll 刷新脏页', async () => {
const backend = new MemoryBackend();
await backend.open('bp-test2');
const pageIO = {
readPage: async (id: number) => backend.read(`pg_${id}`),
writePage: async (id: number, d: ArrayBuffer) => backend.write(`pg_${id}`, d),
allocatePageId: async () => 1,
freePageId: async (_id: number) => {},
};
const pool = new BufferPool(pageIO, 4);
const pages = await pool.newPages(1);
pool.markDirty(pages[0]);
pool.unpin(pages[0]);
await pool.flushAll();
const buf = await backend.read('pg_1');
expect(buf).not.toBeNull();
await backend.close();
});
});
// ---- 引擎级压缩/加密开关测试 ----
describe('Compression+Crypto toggle', () => {
it('compression=false 引擎正常启动', async () => {
const e = new AriaEngine({ storageBackend: 'memory', compression: false });
await e.open('comp-test', 1);
expect(e.isOpen()).toBe(true);
await e.close();
});
it('compression=true 引擎正常启动', async () => {
const e = new AriaEngine({ storageBackend: 'memory', compression: true });
await e.open('comp-test2', 1);
expect(e.isOpen()).toBe(true);
await e.close();
});
});
});