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

48 lines
1.3 KiB
JavaScript
Raw Normal View History

2026-02-19 13:24:14 +01:00
import { describe, it, before, after } from 'mocha';
import BoxError from '../boxerror.js';
2026-02-14 15:43:24 +01:00
import common from './common.js';
2026-02-18 22:21:54 +01:00
import assert from 'node:assert/strict';
import safe from 'safetydance';
import locks from '../locks.js';
2024-12-07 14:35:45 +01:00
2026-02-19 13:24:14 +01:00
describe('Locks', function () {
2024-12-07 14:35:45 +01:00
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'));
2026-02-18 22:21:54 +01:00
assert.equal(error.reason, BoxError.BAD_STATE);
2024-12-07 14:35:45 +01:00
});
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();
2026-02-18 22:21:54 +01:00
assert.ok((endTime - startTime) > (9900));
assert.ok((endTime - startTime) < (10100));
2024-12-07 14:35:45 +01:00
});
});