forked from Shared/esg
Add health check and retry logic and circuit breaker
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
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',
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user