mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
test: Divide current tests into integration and unit tests for LockingResourceStore
This commit is contained in:
committed by
Joachim Van Herwegen
parent
dacfb74a6a
commit
f4afc3ce16
@@ -1,6 +1,23 @@
|
||||
import type { EventEmitter } from 'events';
|
||||
import streamifyArray from 'streamify-array';
|
||||
import { WrappedExpiringResourceLocker } from '../../../../src/util/locking/WrappedExpiringResourceLocker';
|
||||
|
||||
describe('A WrappedExpiringResourceLocker', (): void => {
|
||||
let order: string[];
|
||||
|
||||
beforeEach(async(): Promise<void> => {
|
||||
order = [];
|
||||
});
|
||||
|
||||
const registerEventOrder = async(eventSource: EventEmitter, event: string): Promise<void> => {
|
||||
await new Promise((resolve): any => {
|
||||
eventSource.prependListener(event, (): any => {
|
||||
order.push(event);
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
it('emits an error event when releasing the lock errors.', async(): Promise<void> => {
|
||||
jest.useFakeTimers();
|
||||
|
||||
@@ -27,4 +44,44 @@ describe('A WrappedExpiringResourceLocker', (): void => {
|
||||
expect(errorCallback).toHaveBeenCalledTimes(1);
|
||||
expect(errorCallback).toHaveBeenLastCalledWith(new Error('Release error'));
|
||||
});
|
||||
|
||||
it('releases the lock on the resource when data has been read.', async(): Promise<void> => {
|
||||
// Mock the inner ResourceLocker.
|
||||
const release = jest.fn(async(): Promise<any> => order.push('release'));
|
||||
const lock = { release };
|
||||
const locker = {
|
||||
acquire: jest.fn(async(): Promise<any> => {
|
||||
order.push('acquire');
|
||||
return lock;
|
||||
}),
|
||||
};
|
||||
|
||||
const expiringLocker = new WrappedExpiringResourceLocker(locker, 1000);
|
||||
const expiringLock = await expiringLocker.acquire({} as any);
|
||||
|
||||
// Mimic the behavior of a LockingResourceStore to test the expiringLock methods called.
|
||||
const source = streamifyArray([ 1, 2, 3 ]);
|
||||
// eslint-disable-next-line jest/valid-expect-in-promise
|
||||
new Promise((resolve): void => {
|
||||
source.on('end', resolve);
|
||||
source.on('close', resolve);
|
||||
}).then((): any => expiringLock.release(), null);
|
||||
const readable = Object.create(source, {
|
||||
read: {
|
||||
value(size: number): any {
|
||||
expiringLock.renew();
|
||||
return source.read(size);
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
// Read all data from the "representation"
|
||||
readable.on('data', (): any => true);
|
||||
await registerEventOrder(readable, 'end');
|
||||
|
||||
// Verify the lock was acquired and released at the right time
|
||||
expect(locker.acquire).toHaveBeenCalledTimes(1);
|
||||
expect(lock.release).toHaveBeenCalledTimes(1);
|
||||
expect(order).toEqual([ 'acquire', 'end', 'release' ]);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user