Files
esg/test/unit/distributed-lock.spec.ts
T

28 lines
1.1 KiB
TypeScript

import { DistributedLockService } from '../../src/providers/services/distributed-lock.service';
describe('DistributedLockService', () => {
it('runs the operation under an acquired lease and releases it', async () => {
const service = new DistributedLockService({} as never);
const lease = {
lockKey: 'provider-token:TejaratNou',
ownerId: 'owner-1',
fencingToken: 1,
};
const acquire = jest.spyOn(service, 'acquire').mockResolvedValue(lease);
const release = jest.spyOn(service, 'release').mockResolvedValue(undefined);
await expect(service.withLock(lease.lockKey, async () => 'ok')).resolves.toBe('ok');
expect(acquire).toHaveBeenCalled();
expect(release).toHaveBeenCalledWith(lease);
});
it('fails after the configured wait when the lock is held elsewhere', async () => {
const service = new DistributedLockService({} as never);
jest.spyOn(service, 'acquire').mockResolvedValue(null);
await expect(service.withLock('busy', async () => 'never', { waitMs: 2 })).rejects.toMatchObject({
code: 'ELOCKED',
});
});
});