I have written a mutex implementation for javascript / typescript, but I'm struggling with the question of how to test it. Here's the implementation:
class Mutex {
private current = Promise.resolve();
async acquire() {
let release: () => void;
const next = new Promise<void>(resolve => {
release = () => { resolve(); };
});
const waiter = this.current.then(() => release);
this.current = next;
return await waiter;
}
}
Usage:
const mut = new Mutex();
async function usesMutex() {
const unlock = await mut.acquire();
try {
await doSomeStuff();
await doOtherStuff();
} finally {
unlock();
}
}
I'm not sure if there's any easy way to create the sort of timing issues that would lead to a test failure if the mutex doesn't work as expected. Any advice would be very much appreciated.
from Testing a javascript mutex implementation
No comments:
Post a Comment