Files
cloudron-box/src/test/locks-test.js
T
Girish Ramakrishnan d0a66f1701 Back to mocha!
sorry i ever left you dear mocha

node:test has two major issues:
* --bail does not work and requires strange modules and incantations.
I was able to work around this with a custom module.

* the test reporter reports _after_ the suite is run. this makes debugging
really hard. the debugs that we print all happen before the test suite summary.
poor design overall.
2026-02-19 13:24:14 +01:00

48 lines
1.3 KiB
JavaScript

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));
});
});