mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: add a process-/thread-safe file-based ResourceLocker
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
This commit is contained in:
committed by
Joachim Van Herwegen
parent
7e5483a36d
commit
fa78bc6856
@@ -11,8 +11,8 @@ import { InternalServerError } from '../../src/util/errors/InternalServerError';
|
||||
import { SingleRootIdentifierStrategy } from '../../src/util/identifiers/SingleRootIdentifierStrategy';
|
||||
import { EqualReadWriteLocker } from '../../src/util/locking/EqualReadWriteLocker';
|
||||
import type { ExpiringReadWriteLocker } from '../../src/util/locking/ExpiringReadWriteLocker';
|
||||
import { MemoryResourceLocker } from '../../src/util/locking/MemoryResourceLocker';
|
||||
import type { ReadWriteLocker } from '../../src/util/locking/ReadWriteLocker';
|
||||
import { SingleThreadedResourceLocker } from '../../src/util/locking/SingleThreadedResourceLocker';
|
||||
import { WrappedExpiringReadWriteLocker } from '../../src/util/locking/WrappedExpiringReadWriteLocker';
|
||||
import { guardedStreamFrom } from '../../src/util/StreamUtil';
|
||||
import { PIM, RDF } from '../../src/util/Vocabularies';
|
||||
@@ -48,7 +48,7 @@ describe('A LockingResourceStore', (): void => {
|
||||
metadata.add(RDF.terms.type, PIM.terms.Storage);
|
||||
await source.setRepresentation({ path: base }, new BasicRepresentation([], metadata));
|
||||
|
||||
locker = new EqualReadWriteLocker(new SingleThreadedResourceLocker());
|
||||
locker = new EqualReadWriteLocker(new MemoryResourceLocker());
|
||||
expiringLocker = new WrappedExpiringReadWriteLocker(locker, 1000);
|
||||
|
||||
store = new LockingResourceStore(source, expiringLocker, strategy);
|
||||
|
||||
@@ -399,4 +399,3 @@ describeIf('docker', 'A server with a RedisLocker', (): void => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
31
test/unit/util/LockUtil.test.ts
Normal file
31
test/unit/util/LockUtil.test.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { setJitterTimeout } from '../../../src/util/LockUtils';
|
||||
|
||||
jest.useFakeTimers();
|
||||
|
||||
describe('LockUtil', (): void => {
|
||||
describe('#setJitterTimout', (): void => {
|
||||
it('works without jitter.', async(): Promise<void> => {
|
||||
let result = '';
|
||||
const promise = setJitterTimeout(1000).then((): void => {
|
||||
result += 'ok';
|
||||
});
|
||||
expect(result).toHaveLength(0);
|
||||
jest.advanceTimersByTime(1000);
|
||||
await expect(promise).resolves.toBeUndefined();
|
||||
expect(result).toBe('ok');
|
||||
});
|
||||
|
||||
it('works with jitter.', async(): Promise<void> => {
|
||||
jest.spyOn(global.Math, 'random').mockReturnValue(1);
|
||||
let elapsed = Date.now();
|
||||
const promise = setJitterTimeout(1000, 100).then((): void => {
|
||||
elapsed = Date.now() - elapsed;
|
||||
});
|
||||
jest.runAllTimers();
|
||||
await expect(promise).resolves.toBeUndefined();
|
||||
expect(elapsed).toBe(1100);
|
||||
// Clean up
|
||||
jest.spyOn(global.Math, 'random').mockRestore();
|
||||
});
|
||||
});
|
||||
});
|
||||
122
test/unit/util/locking/FileSystemResourceLocker.test.ts
Normal file
122
test/unit/util/locking/FileSystemResourceLocker.test.ts
Normal file
@@ -0,0 +1,122 @@
|
||||
import { readdir } from 'fs-extra';
|
||||
import { InternalServerError } from '../../../../src/util/errors/InternalServerError';
|
||||
import { FileSystemResourceLocker } from '../../../../src/util/locking/FileSystemResourceLocker';
|
||||
|
||||
const lockFolder = './.internal/locks/';
|
||||
|
||||
describe('A FileSystemResourceLocker', (): void => {
|
||||
let locker: FileSystemResourceLocker;
|
||||
const identifier = { path: 'http://test.com/foo' };
|
||||
|
||||
beforeEach(async(): Promise<void> => {
|
||||
locker = new FileSystemResourceLocker({ attemptSettings: { retryCount: 19, retryDelay: 100 }});
|
||||
});
|
||||
|
||||
afterEach(async(): Promise<void> => {
|
||||
try {
|
||||
// Release to be sure
|
||||
await locker.release(identifier);
|
||||
} catch {
|
||||
// Do nothing
|
||||
}
|
||||
});
|
||||
|
||||
afterAll(async(): Promise<void> => {
|
||||
await locker.finalize();
|
||||
});
|
||||
|
||||
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 and unlock a resource with a locker with indefinite retry.', async(): Promise<void> => {
|
||||
const locker2 = new FileSystemResourceLocker({ attemptSettings: { retryCount: -1 }});
|
||||
await expect(locker2.acquire(identifier)).resolves.toBeUndefined();
|
||||
await expect(locker2.release(identifier)).resolves.toBeUndefined();
|
||||
await locker2.finalize();
|
||||
});
|
||||
|
||||
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);
|
||||
await expect(locker.release(identifier)).rejects.toThrow('Lock is not acquired/owned by you');
|
||||
});
|
||||
|
||||
it('errors when max retries has been reached.', async(): Promise<void> => {
|
||||
await locker.acquire(identifier);
|
||||
await expect(locker.acquire(identifier)).rejects
|
||||
.toThrow(
|
||||
/Error trying to acquire lock for .*\. The operation did not succeed after the set maximum of tries \(\d+\)\./u,
|
||||
);
|
||||
await locker.release(identifier);
|
||||
});
|
||||
|
||||
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[0]).toBe(1);
|
||||
expect(results).toContain(2);
|
||||
expect(results).toContain(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 ]);
|
||||
});
|
||||
|
||||
it('throws an error when #tryFn() throws an error.', async(): Promise<void> => {
|
||||
await locker.acquire(identifier);
|
||||
await expect(locker.acquire(identifier)).rejects.toThrow(InternalServerError);
|
||||
});
|
||||
|
||||
it('clears the files in de lock directory after calling finalize.', async(): Promise<void> => {
|
||||
await locker.acquire(identifier);
|
||||
await expect(readdir(lockFolder)).resolves.toHaveLength(1);
|
||||
await locker.finalize();
|
||||
await expect(readdir(lockFolder)).rejects.toThrow();
|
||||
});
|
||||
|
||||
it('can create a locker with default AttemptSettings.', async(): Promise<void> => {
|
||||
expect((): FileSystemResourceLocker => new FileSystemResourceLocker()).not.toThrow();
|
||||
});
|
||||
});
|
||||
@@ -1,11 +1,29 @@
|
||||
import type { Logger } from '../../../../src';
|
||||
import { getLoggerFor } from '../../../../src';
|
||||
import { InternalServerError } from '../../../../src/util/errors/InternalServerError';
|
||||
import { SingleThreadedResourceLocker } from '../../../../src/util/locking/SingleThreadedResourceLocker';
|
||||
import { MemoryResourceLocker } from '../../../../src/util/locking/MemoryResourceLocker';
|
||||
|
||||
describe('A SingleThreadedResourceLocker', (): void => {
|
||||
let locker: SingleThreadedResourceLocker;
|
||||
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 SingleThreadedResourceLocker();
|
||||
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> => {
|
||||
Reference in New Issue
Block a user