Files
cloudron-box/src/test/locks-test.js
T

50 lines
1.4 KiB
JavaScript
Raw Normal View History

2024-12-07 14:35:45 +01:00
/* global it, describe, before, after */
import BoxError from '../boxerror.js';
2026-02-14 15:43:24 +01:00
import common from './common.js';
import expect from 'expect.js';
import safe from 'safetydance';
import locks from '../locks.js';
2024-12-07 14:35:45 +01:00
describe('Locks', function () {
this.timeout(20000);
const { setup, cleanup } = common;
before(setup);
after(cleanup);
it('can release all locks', async function () {
await locks.releaseAll();
});
it('acquire lock foo', async function () {
await locks.acquire('foo');
});
it('cannot reacquire lock foo', async function () {
const [error] = await safe(locks.acquire('foo'));
expect(error.reason).to.be(BoxError.BAD_STATE);
});
it('cannot reacquire after release', async function () {
await locks.release('foo');
await locks.acquire('foo');
});
it('task gets a lock', async function () {
locks.setTaskId('42');
await locks.acquire('tasklock');
});
it('can wait for a lock', async function () {
setTimeout(() => locks.releaseByTaskId('42'), 3000); // release in 3 seconds
const startTime = Date.now();
await locks.wait('tasklock'); // retries only in 10s
const endTime = Date.now();
expect(endTime - startTime).to.be.greaterThan(9900);
expect(endTime - startTime).to.be.lessThan(10100);
});
});