Skip to content

Commit

Permalink
Typos, add tests for #52
Browse files Browse the repository at this point in the history
  • Loading branch information
DirtyHairy committed Sep 2, 2022
1 parent fe6c607 commit ca09cc6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ revoked. In consequence, the mutex may not be available even after `cancel()` ha

You can wait until the mutex is available without locking it by calling `waitForUnlock()`.
This will return a promise that resolve once the mutex can be acquired again. This operation
will not lock the mutex, and there is no gurantee that the mutex will still be available
will not lock the mutex, and there is no guarantee that the mutex will still be available
once a async barrier has been encountered.

Promise style:
Expand Down Expand Up @@ -372,7 +372,7 @@ semaphore.setValue();

### Cancelling pending locks.

Pending locks can be cancelled by calling `cancel()` on the sempahore. This will reject
Pending locks can be cancelled by calling `cancel()` on the semaphore. This will reject
all pending locks with `E_CANCELED`:

Promise style:
Expand Down Expand Up @@ -425,7 +425,7 @@ revoked. In consequence, the semaphore may not be available even after `cancel()

You can wait until the semaphore is available without locking it by calling `waitForUnlock()`.
This will return a promise that resolve once the semaphore can be acquired again. This operation
will not lock the semaphore, and there is no gurantee that the semaphore will still be available
will not lock the semaphore, and there is no guarantee that the semaphore will still be available
once a async barrier has been encountered.

Promise style:
Expand Down
18 changes: 18 additions & 0 deletions test/mutex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,24 @@ export const mutexSuite = (factory: (cancelError?: Error) => MutexInterface): vo

assert.strictEqual(taskCalls, 2);
});

test('waitForUnlock only unblocks when the mutex can actually be acquired again', async () => {
mutex.acquire();
mutex.acquire();

let flag = false;
mutex.waitForUnlock().then(() => (flag = true));

mutex.release();
await clock.tickAsync(0);

assert.strictEqual(flag, false);

mutex.release();
await clock.tickAsync(0);

assert.strictEqual(flag, true);
});
};

suite('Mutex', () => mutexSuite((e) => new Mutex(e)));
18 changes: 18 additions & 0 deletions test/semaphoreSuite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,24 @@ export const semaphoreSuite = (factory: (maxConcurrency: number, err?: Error) =>
assert.deepStrictEqual([flag1, flag2], [true, true]);
});

test('waitForUnlock only unblocks when the semaphore can actually be acquired again', async () => {
semaphore.acquire(2);
semaphore.acquire(2);

let flag = false;
semaphore.waitForUnlock().then(() => (flag = true));

semaphore.release(2);
await clock.tickAsync(0);

assert.strictEqual(flag, false);

semaphore.release(2);
await clock.tickAsync(0);

assert.strictEqual(flag, true);
});

test('trying to acquire with a negative weight throws', () => {
assert.throws(() => semaphore.acquire(-1));
});
Expand Down

0 comments on commit ca09cc6

Please sign in to comment.