Repository navigation
Conversation
RubenVerborgh
left a comment
There was a problem hiding this comment.
Very clean code, but I think I have spotted a crucial bug 😉
| private async lockedRun<T>(identifier: ResourceIdentifier, func: () => Promise<T>): Promise<T> { | ||
| const lock = await this.locks.acquire(identifier); | ||
| try { | ||
| return func(); |
There was a problem hiding this comment.
Don't we need to await func()?
There was a problem hiding this comment.
I was halfway through my explanation of why it's the same, but no, due to the finally statement this needs to be an await indeed.
| /** | ||
| * A resource locker making use of the `async-lock` library. | ||
| */ | ||
| export class AsyncLockResourceLocker implements ResourceLocker { |
There was a problem hiding this comment.
Is this implementation multi-worker-safe, or even thread-safe?
We might want to expose that information as two boolean properties.
There was a problem hiding this comment.
Multiple threads in Node means multiple independent processes right? Which would mean multiple instances of this object so they would be completely independent (and thus the locking would not work between the workers). Unless workers threads work differently than I though.
There was a problem hiding this comment.
It depends on the locking mechanism used. Workers are a multi-process mechanism indeed.
I think we probably want a threadSafe flag or so (but we will likely not need the two, as I said earlier).
Suggestion to perhaps rename the class to SingleThreadedResourceLocker or something (the lib used for implementation doesn't really matter).
| expect(lock).toEqual(expect.objectContaining({ release: expect.any(Function) })); | ||
| }); | ||
|
|
||
| it('blocks lock acquisition until they are released.', async(): Promise<void> => { |
There was a problem hiding this comment.
Is this really what we are testing?
There was a problem hiding this comment.
Haha, yes it is. But it took me some time to get it; I had missed the swapped orders of the locks. Perhaps add some comments to emphasize that?
| import { ResourceLocker } from '../../../src/storage/ResourceLocker'; | ||
| import { ResourceStore } from '../../../src/storage/ResourceStore'; | ||
|
|
||
| describe('A LockingResourceStore', (): void => { |
There was a problem hiding this comment.
Also need to test an error condition (both sync and async)
| }); | ||
|
|
||
| it('acquires a lock on the resource when getting it.', async(): Promise<void> => { | ||
| await store.getRepresentation({ path: 'path' }, null); |
There was a problem hiding this comment.
We should also test the asynchronous behavior; i.e., release should only be called after await, and I don't think it is at the moment (as per my comment above).
There was a problem hiding this comment.
Yea, this is something I was also thinking about. But I couldn't actually figure out how to do this cleanly 😅
There was a problem hiding this comment.
Ah well, delayedResolve does it indeed, but what I usually do is storing the resolve functions, and then calling them in a controlled way. Something like:
const promise = store.getRepresentation({ path: 'path' }, null);
expect(locker.acquire).toHaveBeenCalledTimes(1);
expect(locker.acquire).toHaveBeenLastCalledWith({ path: 'path' });
expect(source.getRepresentation).toHaveBeenCalledTimes(1);
expect(lock.release).toHaveBeenCalledTimes(0); // <== not released yet!
await tellSourceToResolveRepresentationPromise(); // <== assuming `delayedResolve` stores the promises you need rather than calling them itself
expect(lock.release).toHaveBeenCalledTimes(1); // <== only now it is released!
expect(await promise).toEqual(whateverYouReturned); // <== currently not checked
Such active control makes it a bit easier.
But the explicit order approach also works.
| return this.lockedRun(identifier, async(): Promise<void> => this.source.deleteResource(identifier, conditions)); | ||
| } | ||
|
|
||
| public async getRepresentation(identifier: ResourceIdentifier, preferences: RepresentationPreferences, conditions?: Conditions): Promise<Representation> { |
There was a problem hiding this comment.
Isn't there a max-line-length eslint rule?
If not, I would suggest enabling it to something like 200, as visualizing this line on GH is an example why it would be needed :-)
There was a problem hiding this comment.
That line is actually 157 characters long so would have to be lower. (The one above is 117).
There was a problem hiding this comment.
Ah, typo, I meant 120 :-)
There was a problem hiding this comment.
That is going to be a separate PR though since I'm pretty sure several classes are going to need changes for that :D
|
@RubenVerborgh should be more robust now |
| }); | ||
|
|
||
| it('acquires a lock on the resource when getting it.', async(): Promise<void> => { | ||
| await store.getRepresentation({ path: 'path' }, null); |
There was a problem hiding this comment.
Ah well, delayedResolve does it indeed, but what I usually do is storing the resolve functions, and then calling them in a controlled way. Something like:
const promise = store.getRepresentation({ path: 'path' }, null);
expect(locker.acquire).toHaveBeenCalledTimes(1);
expect(locker.acquire).toHaveBeenLastCalledWith({ path: 'path' });
expect(source.getRepresentation).toHaveBeenCalledTimes(1);
expect(lock.release).toHaveBeenCalledTimes(0); // <== not released yet!
await tellSourceToResolveRepresentationPromise(); // <== assuming `delayedResolve` stores the promises you need rather than calling them itself
expect(lock.release).toHaveBeenCalledTimes(1); // <== only now it is released!
expect(await promise).toEqual(whateverYouReturned); // <== currently not checked
Such active control makes it a bit easier.
But the explicit order approach also works.
This is mostly so there's a ResourceLocker which is needed for some Patch functionality.