docs(tests): explain model preview dependencies and loading completion

This commit is contained in:
2026-11-12 17:44:25 +08:00
parent ea3df9877d
commit 535d963eaa
4 changed files with 8 additions and 0 deletions
+2
View File
@@ -75,6 +75,7 @@ test('cancelled active work retains its concurrency slot; its late cleanup canno
const first = deferred(), second = deferred(), entered = []; let calls = 0;
const loader = createScopedLoader({ concurrency: 1, maxQueued: 1, current: () => true, async *load(query) { const index = ++calls; entered.push(index); await (index === 1 ? first.promise : second.promise); yield chunk(query); } });
const a = loader.acquire(scope); await tick(); const cancelled = assert.rejects(a.ready, { code: 'load_aborted' }); a.release(); await cancelled;
// a 已经拒绝,但其生成器仍挂在 first;同键 b 必须以新代次排队,不能提前占第二个执行槽。
const b = loader.acquire(scope); await tick(); assert.equal(calls, 1); assert.equal(b.getSnapshot().status, 'queued');
assert.throws(() => loader.acquire({ ...scope, canvasId: 'third' }), { code: 'load_capacity' });
first.resolve(); await tick(); assert.equal(calls, 2); const peer = loader.acquire(scope); assert.equal(peer.getSnapshot().generation, b.getSnapshot().generation);
@@ -93,6 +94,7 @@ test('read cache requires explicit source validation and is bounded by byte LRU
async *load(query) { calls.set(query.canvasId, (calls.get(query.canvasId) ?? 0) + 1); yield chunk(query); } });
// 缓存用例统一领取、等待及释放;只有显式 allow-validated 才允许考虑已缓存结果。
const read = async (canvasId, cached = false) => { const handle = loader.acquire({ ...scope, canvasId }, { cache: cached ? 'allow-validated' : 'fresh' }); await handle.ready; handle.release(); };
// 每项声明 2 字节、总预算 4:访问 a 后再放 c 应淘汰 b;容量是协议 byteLength 计数,不是进程堆内存测量。
await read('a'); await read('b'); await read('a', true); assert.equal(calls.get('a'), 1); await read('c'); await read('b', true); assert.equal(calls.get('b'), 2);
accepted = false; await read('b', true); assert.equal(calls.get('b'), 3); accepted = true; time = 201; await read('b', true); assert.equal(calls.get('b'), 4);
time = 0; await read('b', true); assert.equal(calls.get('b'), 5); loader.close();