import { describe, it, before, after } from 'mocha'; import BoxError from '../boxerror.js'; import common from './common.js'; import assert from 'node:assert/strict'; import safe from 'safetydance'; import locks from '../locks.js'; describe('Locks', function () { 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')); assert.equal(error.reason, 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(); assert.ok((endTime - startTime) > (9900)); assert.ok((endTime - startTime) < (10100)); }); });