Bound and deduplicate server image rendering per account

This commit is contained in:
syntaxbullet
2026-09-08 16:18:05 +02:00
parent e3e07404f8
commit ba049efdc8
8 changed files with 101 additions and 12 deletions

View File

@@ -0,0 +1,27 @@
import { expect, test } from 'bun:test';
import { createRenderQueue, RenderBusyError } from './queue';
const result = new Uint8Array([1, 2, 3]);
test('render queue bounds global concurrency and shares only the same owner and content', async () => {
const queue = createRenderQueue({ concurrency: 2, maxPending: 4, maxPerUser: 2 });
let active = 0, peak = 0, calls = 0;
const run = async () => { calls++; active++; peak = Math.max(peak, active); await Bun.sleep(5); active--; return result; };
const first = queue('alice', 'one', run);
expect(queue('alice', 'one', run)).toBe(first);
const all = [first, queue('alice', 'two', run), queue('bob', 'one', run), queue('carol', 'one', run)];
await Promise.all(all); expect(calls).toBe(4); expect(peak).toBe(2);
expect(await queue('alice', 'one', run)).toBe(result); expect(calls).toBe(5);
});
test('render admission enforces per-user and global capacity and recovers after failures', async () => {
const queue = createRenderQueue({ concurrency: 1, maxPending: 2, maxPerUser: 1 });
let release!: () => void;
const first = queue('alice', 'a', async () => { await new Promise<void>(resolve => { release = resolve; }); throw new Error('render failed'); });
const failure = first.catch(error => error);
await expect(queue('alice', 'b', async () => result)).rejects.toBeInstanceOf(RenderBusyError);
const second = queue('bob', 'b', async () => result);
await expect(queue('carol', 'c', async () => result)).rejects.toBeInstanceOf(RenderBusyError);
while (!release) await Bun.sleep(1);
release(); expect((await failure).message).toBe('render failed'); await second;
expect(await queue('alice', 'a', async () => result)).toBe(result);
});