mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00

test: unit test succeeds fix: not quiting loop when releasing unexisting lock refactor: pull wait() function into TimerUtils feat: store all locks inside a single lock folder feat: use md5 hashing for filepath hashes test: coverage back to 100% fix: store locks in proper .internal/locks folder feat: reworked tryfn test: coverage back to 100% buidl: package json types next to lib style: linting dos: add more documentation to Locker classes refactor: SingleThreadedResourceLocker -> MemoryResourceLocker refactor: MultiThreadedResourceLocker -> FileSystemResourceLocker feat: update all file-based backend configs to use the new FileSystemResourceLocker feat: add warning on starting the MemoryResourceLocker in a worker process test: coverage back to 100% fix: finalizer of file.json was configured wrong docs: updated release notes for 5.0.0 refactor: incorporated changes so far refactor: retryFunctions are less complex now test: jitter fix
89 lines
3.3 KiB
TypeScript
89 lines
3.3 KiB
TypeScript
import type { Logger } from '../../../../src';
|
|
import { getLoggerFor } from '../../../../src';
|
|
import { InternalServerError } from '../../../../src/util/errors/InternalServerError';
|
|
import { MemoryResourceLocker } from '../../../../src/util/locking/MemoryResourceLocker';
|
|
|
|
jest.mock('../../../../src/logging/LogUtil', (): any => {
|
|
const logger: Logger =
|
|
{ error: jest.fn(), debug: jest.fn(), warn: jest.fn(), info: jest.fn(), log: jest.fn() } as any;
|
|
return { getLoggerFor: (): Logger => logger };
|
|
});
|
|
const logger: jest.Mocked<Logger> = getLoggerFor('MemoryResourceLocker') as any;
|
|
|
|
jest.mock('cluster', (): any => ({
|
|
isWorker: true,
|
|
}));
|
|
|
|
describe('A MemoryResourceLocker', (): void => {
|
|
let locker: MemoryResourceLocker;
|
|
const identifier = { path: 'http://test.com/foo' };
|
|
beforeEach(async(): Promise<void> => {
|
|
locker = new MemoryResourceLocker();
|
|
});
|
|
|
|
it('logs a warning when constructed on a worker process.', (): void => {
|
|
expect((): MemoryResourceLocker => new MemoryResourceLocker()).toBeDefined();
|
|
expect(logger.warn).toHaveBeenCalled();
|
|
});
|
|
|
|
it('can lock and unlock a resource.', async(): Promise<void> => {
|
|
await expect(locker.acquire(identifier)).resolves.toBeUndefined();
|
|
await expect(locker.release(identifier)).resolves.toBeUndefined();
|
|
});
|
|
|
|
it('can lock a resource again after it was unlocked.', async(): Promise<void> => {
|
|
await expect(locker.acquire(identifier)).resolves.toBeUndefined();
|
|
await expect(locker.release(identifier)).resolves.toBeUndefined();
|
|
await expect(locker.acquire(identifier)).resolves.toBeUndefined();
|
|
});
|
|
|
|
it('errors when unlocking a resource that was not locked.', async(): Promise<void> => {
|
|
await expect(locker.acquire(identifier)).resolves.toBeUndefined();
|
|
await expect(locker.release(identifier)).resolves.toBeUndefined();
|
|
await expect(locker.release(identifier)).rejects.toThrow(InternalServerError);
|
|
});
|
|
|
|
it('blocks lock acquisition until they are released.', async(): Promise<void> => {
|
|
const results: number[] = [];
|
|
const lock1 = locker.acquire(identifier);
|
|
const lock2 = locker.acquire(identifier);
|
|
const lock3 = locker.acquire(identifier);
|
|
|
|
// Note the different order of calls
|
|
const prom2 = lock2.then(async(): Promise<void> => {
|
|
results.push(2);
|
|
return locker.release(identifier);
|
|
});
|
|
const prom3 = lock3.then(async(): Promise<void> => {
|
|
results.push(3);
|
|
return locker.release(identifier);
|
|
});
|
|
const prom1 = lock1.then(async(): Promise<void> => {
|
|
results.push(1);
|
|
return locker.release(identifier);
|
|
});
|
|
await Promise.all([ prom2, prom3, prom1 ]);
|
|
expect(results).toEqual([ 1, 2, 3 ]);
|
|
});
|
|
|
|
it('can acquire different keys simultaneously.', async(): Promise<void> => {
|
|
const results: number[] = [];
|
|
const lock1 = locker.acquire({ path: 'path1' });
|
|
const lock2 = locker.acquire({ path: 'path2' });
|
|
const lock3 = locker.acquire({ path: 'path3' });
|
|
await lock2.then(async(): Promise<void> => {
|
|
results.push(2);
|
|
return locker.release({ path: 'path2' });
|
|
});
|
|
await lock3.then(async(): Promise<void> => {
|
|
results.push(3);
|
|
return locker.release({ path: 'path3' });
|
|
});
|
|
await lock1.then(async(): Promise<void> => {
|
|
results.push(1);
|
|
return locker.release({ path: 'path1' });
|
|
});
|
|
expect(results).toEqual([ 2, 3, 1 ]);
|
|
});
|
|
});
|