refactor: Rename associated resource to subject resource

This commit is contained in:
Joachim Van Herwegen
2021-10-07 16:38:25 +02:00
parent 13c49045d4
commit 7c7fee5f5c
20 changed files with 93 additions and 93 deletions

View File

@@ -14,7 +14,7 @@ function emptyFn(): void {
describe('A LockingResourceStore', (): void => {
const auxiliaryId = { path: 'http://test.com/foo.dummy' };
const associatedId = { path: 'http://test.com/foo' };
const subjectId = { path: 'http://test.com/foo' };
const data = { data: 'data!' } as any;
let store: LockingResourceStore;
let locker: ExpiringReadWriteLocker;
@@ -72,7 +72,7 @@ describe('A LockingResourceStore', (): void => {
auxiliaryStrategy = {
isAuxiliaryIdentifier: jest.fn((id: ResourceIdentifier): any => id.path.endsWith('.dummy')),
getAssociatedIdentifier: jest.fn((id: ResourceIdentifier): any => ({ path: id.path.slice(0, -6) })),
getSubjectIdentifier: jest.fn((id: ResourceIdentifier): any => ({ path: id.path.slice(0, -6) })),
} as any;
store = new LockingResourceStore(source, locker, auxiliaryStrategy);
@@ -85,68 +85,68 @@ describe('A LockingResourceStore', (): void => {
}
it('acquires a lock on the container when adding a representation.', async(): Promise<void> => {
await store.addResource(associatedId, data);
await store.addResource(subjectId, data);
expect(locker.withWriteLock).toHaveBeenCalledTimes(1);
expect((locker.withWriteLock as jest.Mock).mock.calls[0][0]).toEqual(associatedId);
expect((locker.withWriteLock as jest.Mock).mock.calls[0][0]).toEqual(subjectId);
expect(source.addResource).toHaveBeenCalledTimes(1);
expect(source.addResource).toHaveBeenLastCalledWith(associatedId, data, undefined);
expect(source.addResource).toHaveBeenLastCalledWith(subjectId, data, undefined);
expect(order).toEqual([ 'lock write', 'addResource', 'unlock write' ]);
order = [];
await expect(store.addResource(auxiliaryId, data)).resolves.toBeUndefined();
expect(locker.withWriteLock).toHaveBeenCalledTimes(2);
expect((locker.withWriteLock as jest.Mock).mock.calls[1][0]).toEqual(associatedId);
expect((locker.withWriteLock as jest.Mock).mock.calls[1][0]).toEqual(subjectId);
expect(source.addResource).toHaveBeenCalledTimes(2);
expect(source.addResource).toHaveBeenLastCalledWith(auxiliaryId, data, undefined);
expect(order).toEqual([ 'lock write', 'addResource', 'unlock write' ]);
});
it('acquires a lock on the resource when setting its representation.', async(): Promise<void> => {
await store.setRepresentation(associatedId, data);
await store.setRepresentation(subjectId, data);
expect(locker.withWriteLock).toHaveBeenCalledTimes(1);
expect((locker.withWriteLock as jest.Mock).mock.calls[0][0]).toEqual(associatedId);
expect((locker.withWriteLock as jest.Mock).mock.calls[0][0]).toEqual(subjectId);
expect(source.setRepresentation).toHaveBeenCalledTimes(1);
expect(source.setRepresentation).toHaveBeenLastCalledWith(associatedId, data, undefined);
expect(source.setRepresentation).toHaveBeenLastCalledWith(subjectId, data, undefined);
expect(order).toEqual([ 'lock write', 'setRepresentation', 'unlock write' ]);
order = [];
await expect(store.setRepresentation(auxiliaryId, data)).resolves.toBeUndefined();
expect(locker.withWriteLock).toHaveBeenCalledTimes(2);
expect((locker.withWriteLock as jest.Mock).mock.calls[1][0]).toEqual(associatedId);
expect((locker.withWriteLock as jest.Mock).mock.calls[1][0]).toEqual(subjectId);
expect(source.setRepresentation).toHaveBeenCalledTimes(2);
expect(source.setRepresentation).toHaveBeenLastCalledWith(auxiliaryId, data, undefined);
expect(order).toEqual([ 'lock write', 'setRepresentation', 'unlock write' ]);
});
it('acquires a lock on the resource when deleting it.', async(): Promise<void> => {
await store.deleteResource(associatedId);
await store.deleteResource(subjectId);
expect(locker.withWriteLock).toHaveBeenCalledTimes(1);
expect((locker.withWriteLock as jest.Mock).mock.calls[0][0]).toEqual(associatedId);
expect((locker.withWriteLock as jest.Mock).mock.calls[0][0]).toEqual(subjectId);
expect(source.deleteResource).toHaveBeenCalledTimes(1);
expect(source.deleteResource).toHaveBeenLastCalledWith(associatedId, undefined);
expect(source.deleteResource).toHaveBeenLastCalledWith(subjectId, undefined);
expect(order).toEqual([ 'lock write', 'deleteResource', 'unlock write' ]);
order = [];
await expect(store.deleteResource(auxiliaryId)).resolves.toBeUndefined();
expect(locker.withWriteLock).toHaveBeenCalledTimes(2);
expect((locker.withWriteLock as jest.Mock).mock.calls[1][0]).toEqual(associatedId);
expect((locker.withWriteLock as jest.Mock).mock.calls[1][0]).toEqual(subjectId);
expect(source.deleteResource).toHaveBeenCalledTimes(2);
expect(source.deleteResource).toHaveBeenLastCalledWith(auxiliaryId, undefined);
expect(order).toEqual([ 'lock write', 'deleteResource', 'unlock write' ]);
});
it('acquires a lock on the resource when modifying its representation.', async(): Promise<void> => {
await store.modifyResource(associatedId, data as Patch);
await store.modifyResource(subjectId, data as Patch);
expect(locker.withWriteLock).toHaveBeenCalledTimes(1);
expect((locker.withWriteLock as jest.Mock).mock.calls[0][0]).toEqual(associatedId);
expect((locker.withWriteLock as jest.Mock).mock.calls[0][0]).toEqual(subjectId);
expect(source.modifyResource).toHaveBeenCalledTimes(1);
expect(source.modifyResource).toHaveBeenLastCalledWith(associatedId, data, undefined);
expect(source.modifyResource).toHaveBeenLastCalledWith(subjectId, data, undefined);
expect(order).toEqual([ 'lock write', 'modifyResource', 'unlock write' ]);
order = [];
await expect(store.modifyResource(auxiliaryId, data as Patch)).resolves.toBeUndefined();
expect(locker.withWriteLock).toHaveBeenCalledTimes(2);
expect((locker.withWriteLock as jest.Mock).mock.calls[1][0]).toEqual(associatedId);
expect((locker.withWriteLock as jest.Mock).mock.calls[1][0]).toEqual(subjectId);
expect(source.modifyResource).toHaveBeenCalledTimes(2);
expect(source.modifyResource).toHaveBeenLastCalledWith(auxiliaryId, data, undefined);
expect(order).toEqual([ 'lock write', 'modifyResource', 'unlock write' ]);
@@ -157,15 +157,15 @@ describe('A LockingResourceStore', (): void => {
order.push('bad get');
throw new Error('dummy');
};
await expect(store.getRepresentation(associatedId, {})).rejects.toThrow('dummy');
await expect(store.getRepresentation(subjectId, {})).rejects.toThrow('dummy');
expect(locker.withReadLock).toHaveBeenCalledTimes(1);
expect((locker.withReadLock as jest.Mock).mock.calls[0][0]).toEqual(associatedId);
expect((locker.withReadLock as jest.Mock).mock.calls[0][0]).toEqual(subjectId);
expect(order).toEqual([ 'lock read', 'bad get', 'unlock read' ]);
});
it('releases the lock on the resource when data has been read.', async(): Promise<void> => {
// Read all data from the representation
const representation = await store.getRepresentation(associatedId, {});
const representation = await store.getRepresentation(subjectId, {});
representation.data.on('data', (): any => true);
registerEventOrder(representation.data, 'end');
@@ -174,13 +174,13 @@ describe('A LockingResourceStore', (): void => {
// Verify the lock was acquired and released at the right time
expect(locker.withReadLock).toHaveBeenCalledTimes(1);
expect((locker.withReadLock as jest.Mock).mock.calls[0][0]).toEqual(associatedId);
expect((locker.withReadLock as jest.Mock).mock.calls[0][0]).toEqual(subjectId);
expect(source.getRepresentation).toHaveBeenCalledTimes(1);
expect(source.getRepresentation).toHaveBeenLastCalledWith(associatedId, {}, undefined);
expect(source.getRepresentation).toHaveBeenLastCalledWith(subjectId, {}, undefined);
expect(order).toEqual([ 'lock read', 'getRepresentation', 'end', 'unlock read' ]);
});
it('acquires the lock on the associated resource when reading an auxiliary resource.', async(): Promise<void> => {
it('acquires the lock on the subject resource when reading an auxiliary resource.', async(): Promise<void> => {
// Read all data from the representation
const representation = await store.getRepresentation(auxiliaryId, {});
representation.data.on('data', (): any => true);
@@ -191,7 +191,7 @@ describe('A LockingResourceStore', (): void => {
// Verify the lock was acquired and released at the right time
expect(locker.withReadLock).toHaveBeenCalledTimes(1);
expect((locker.withReadLock as jest.Mock).mock.calls[0][0]).toEqual(associatedId);
expect((locker.withReadLock as jest.Mock).mock.calls[0][0]).toEqual(subjectId);
expect(source.getRepresentation).toHaveBeenCalledTimes(1);
expect(source.getRepresentation).toHaveBeenLastCalledWith(auxiliaryId, {}, undefined);
expect(order).toEqual([ 'lock read', 'getRepresentation', 'end', 'unlock read' ]);
@@ -199,7 +199,7 @@ describe('A LockingResourceStore', (): void => {
it('destroys the resource and releases the lock when the readable errors.', async(): Promise<void> => {
// Make the representation error
const representation = await store.getRepresentation(associatedId, {});
const representation = await store.getRepresentation(subjectId, {});
setImmediate((): any => representation.data.emit('error', new Error('Error on the readable')));
registerEventOrder(representation.data, 'error');
registerEventOrder(representation.data, 'close');
@@ -209,7 +209,7 @@ describe('A LockingResourceStore', (): void => {
// Verify the lock was acquired and released at the right time
expect(locker.withReadLock).toHaveBeenCalledTimes(1);
expect((locker.withReadLock as jest.Mock).mock.calls[0][0]).toEqual(associatedId);
expect((locker.withReadLock as jest.Mock).mock.calls[0][0]).toEqual(subjectId);
expect(source.getRepresentation).toHaveBeenCalledTimes(1);
expect(representation.data.destroy).toHaveBeenCalledTimes(1);
expect(order).toEqual([ 'lock read', 'getRepresentation', 'error', 'unlock read', 'close' ]);
@@ -217,7 +217,7 @@ describe('A LockingResourceStore', (): void => {
it('releases the lock on the resource when readable is destroyed.', async(): Promise<void> => {
// Make the representation close
const representation = await store.getRepresentation(associatedId, {});
const representation = await store.getRepresentation(subjectId, {});
representation.data.destroy();
registerEventOrder(representation.data, 'close');
@@ -226,14 +226,14 @@ describe('A LockingResourceStore', (): void => {
// Verify the lock was acquired and released at the right time
expect(locker.withReadLock).toHaveBeenCalledTimes(1);
expect((locker.withReadLock as jest.Mock).mock.calls[0][0]).toEqual(associatedId);
expect((locker.withReadLock as jest.Mock).mock.calls[0][0]).toEqual(subjectId);
expect(source.getRepresentation).toHaveBeenCalledTimes(1);
expect(order).toEqual([ 'lock read', 'getRepresentation', 'close', 'unlock read' ]);
});
it('releases the lock only once when multiple events are triggered.', async(): Promise<void> => {
// Read all data from the representation and trigger an additional close event
const representation = await store.getRepresentation(associatedId, {});
const representation = await store.getRepresentation(subjectId, {});
representation.data.on('data', (): any => true);
representation.data.prependListener('end', (): any => {
order.push('end');
@@ -245,13 +245,13 @@ describe('A LockingResourceStore', (): void => {
// Verify the lock was acquired and released at the right time
expect(locker.withReadLock).toHaveBeenCalledTimes(1);
expect((locker.withReadLock as jest.Mock).mock.calls[0][0]).toEqual(associatedId);
expect((locker.withReadLock as jest.Mock).mock.calls[0][0]).toEqual(subjectId);
expect(source.getRepresentation).toHaveBeenCalledTimes(1);
expect(order).toEqual([ 'lock read', 'getRepresentation', 'end', 'unlock read' ]);
});
it('releases the lock on the resource when readable times out.', async(): Promise<void> => {
const representation = await store.getRepresentation(associatedId, {});
const representation = await store.getRepresentation(subjectId, {});
registerEventOrder(representation.data, 'close');
registerEventOrder(representation.data, 'error');
@@ -262,7 +262,7 @@ describe('A LockingResourceStore', (): void => {
// Verify the lock was acquired and released at the right time
expect(locker.withReadLock).toHaveBeenCalledTimes(1);
expect((locker.withReadLock as jest.Mock).mock.calls[0][0]).toEqual(associatedId);
expect((locker.withReadLock as jest.Mock).mock.calls[0][0]).toEqual(subjectId);
expect(source.getRepresentation).toHaveBeenCalledTimes(1);
expect(representation.data.destroy).toHaveBeenCalledTimes(1);
expect(representation.data.destroy).toHaveBeenLastCalledWith(new Error('timeout'));
@@ -276,23 +276,23 @@ describe('A LockingResourceStore', (): void => {
return new Promise(emptyFn);
});
const prom = store.getRepresentation(associatedId, {});
const prom = store.getRepresentation(subjectId, {});
timeoutTrigger.emit('timeout');
await expect(prom).rejects.toThrow('timeout');
expect(locker.withReadLock).toHaveBeenCalledTimes(1);
expect((locker.withReadLock as jest.Mock).mock.calls[0][0]).toEqual(associatedId);
expect((locker.withReadLock as jest.Mock).mock.calls[0][0]).toEqual(subjectId);
expect(source.getRepresentation).toHaveBeenCalledTimes(1);
expect(order).toEqual([ 'lock read', 'useless get', 'timeout', 'unlock read' ]);
});
it('resourceExists should only acquire and release the read lock.', async(): Promise<void> => {
await store.resourceExists(associatedId);
await store.resourceExists(subjectId);
expect(locker.withReadLock).toHaveBeenCalledTimes(1);
expect(locker.withWriteLock).toHaveBeenCalledTimes(0);
expect(source.resourceExists).toHaveBeenCalledTimes(1);
expect(source.resourceExists).toHaveBeenLastCalledWith(associatedId, undefined);
expect(source.resourceExists).toHaveBeenLastCalledWith(subjectId, undefined);
expect(order).toEqual([ 'lock read', 'resourceExists', 'unlock read' ]);
});
});