43 lines
1.6 KiB
TypeScript
43 lines
1.6 KiB
TypeScript
// @vitest-environment node
|
|
|
|
import { describe, expect, it } from 'vitest';
|
|
import { PiProjectWriteLeaseCoordinator } from '../../electron/coding-runtime/pi/write-lease';
|
|
|
|
describe('Pi project write lease', () => {
|
|
it('serializes mutations in one project while allowing other projects to proceed', async () => {
|
|
const coordinator = new PiProjectWriteLeaseCoordinator();
|
|
const first = await coordinator.acquire('project-a', 'write-a1');
|
|
let secondSettled = false;
|
|
const secondFlight = coordinator.acquire('project-a', 'write-a2').then((lease) => {
|
|
secondSettled = true;
|
|
return lease;
|
|
});
|
|
const otherProject = await coordinator.acquire('project-b', 'write-b1');
|
|
|
|
await Promise.resolve();
|
|
expect(secondSettled).toBe(false);
|
|
expect(coordinator.activeCount).toBe(2);
|
|
expect(coordinator.waitingCount('project-a')).toBe(1);
|
|
|
|
first.release();
|
|
const second = await secondFlight;
|
|
expect(second.holderId).toBe('write-a2');
|
|
second.release();
|
|
otherProject.release();
|
|
expect(coordinator.activeCount).toBe(0);
|
|
});
|
|
|
|
it('removes a cancelled waiter without disturbing the active lease', async () => {
|
|
const coordinator = new PiProjectWriteLeaseCoordinator();
|
|
const active = await coordinator.acquire('project-a', 'active');
|
|
const controller = new AbortController();
|
|
const waiting = coordinator.acquire('project-a', 'waiting', controller.signal);
|
|
controller.abort();
|
|
|
|
await expect(waiting).rejects.toThrow('cancelled');
|
|
expect(coordinator.activeCount).toBe(1);
|
|
expect(coordinator.waitingCount()).toBe(0);
|
|
active.release();
|
|
});
|
|
});
|