Skip to content

Commit

Permalink
Merge branch 'master' into priority
Browse files Browse the repository at this point in the history
  • Loading branch information
dmurvihill committed Feb 2, 2024
2 parents 7aacdc7 + 6a55acb commit c546c3f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
18 changes: 18 additions & 0 deletions test/semaphoreSuite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,24 @@ export const semaphoreSuite = (factory: (maxConcurrency: number, err?: Error) =>
assert.deepStrictEqual(values, [0, 0, +1, -1]);
});

test('acquire allows light items to run eventually', async () => {
let done = false;
async function lightLoop() {
while (!done) {
const [,release] = await semaphore.acquire(1);
await new Promise((resolve) => { setTimeout(resolve, 10); });
release();
}
}
lightLoop();
await clock.tickAsync(5);
lightLoop();
semaphore.acquire(2).then(() => { done = true; });
await clock.tickAsync(10);
await clock.tickAsync(10);
assert.strictEqual(done, true);
});

test('acquire blocks when the semaphore has reached zero until it is released again', async () => {
const values: Array<number> = [];

Expand Down
16 changes: 11 additions & 5 deletions test/withTimeout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,14 @@ suite('withTimeout', () => {

test('waitForUnlock times out', async () => {
mutex.acquire();
const unlockPromise = mutex.waitForUnlock();
let state = 'PENDING';

mutex.waitForUnlock()
.then(() => { state = 'RESOLVED'; })
.catch(() => { state = 'REJECTED'; });
await clock.tickAsync(120);

assert.rejects(unlockPromise, error);
assert.strictEqual(state, 'REJECTED');
});
});

Expand Down Expand Up @@ -255,11 +258,14 @@ suite('withTimeout', () => {

test('waitForUnlock times out', async () => {
semaphore.acquire(2);
const unlockPromise = semaphore.waitForUnlock();
let state = 'PENDING';

await clock.tickAsync(120);
semaphore.waitForUnlock()
.then(() => { state = 'RESOLVED'; })
.catch(() => { state = 'REJECTED'; });

assert.rejects(unlockPromise, error);
await clock.tickAsync(120);
assert.strictEqual(state, 'REJECTED');
});
});

Expand Down

0 comments on commit c546c3f

Please sign in to comment.