Skip to content

Commit

Permalink
feat(semaphore): throw RangeError for invalid size
Browse files Browse the repository at this point in the history
  • Loading branch information
lambdalisue committed Aug 13, 2024
1 parent 8006363 commit 8ed0442
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 5 deletions.
8 changes: 5 additions & 3 deletions semaphore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ export class Semaphore {
* Creates a new semaphore with the specified limit.
*
* @param size The maximum number of times the semaphore can be acquired before blocking.
* @throws Error if the size is less than 1.
* @throws {RangeError} if the size is not a positive safe integer.
*/
constructor(size: number) {
if (size < 0) {
throw new Error("The size must be greater than 0");
if (size <= 0 || !Number.isSafeInteger(size)) {
throw new RangeError(
`size must be a positive safe integer, got ${size}`,
);
}
this.#rest = size + 1;
}
Expand Down
78 changes: 76 additions & 2 deletions semaphore_test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { assertEquals } from "@std/assert";
import { assertEquals, assertThrows } from "@std/assert";
import { Semaphore } from "./semaphore.ts";

Deno.test("Semaphore", async (t) => {
await t.step(
"regulates the number of workers concurrently running",
"regulates the number of workers concurrently running (n=5)",
async () => {
let nworkers = 0;
const results: number[] = [];
Expand Down Expand Up @@ -32,4 +32,78 @@ Deno.test("Semaphore", async (t) => {
]);
},
);

await t.step(
"regulates the number of workers concurrently running (n=1)",
async () => {
let nworkers = 0;
const results: number[] = [];
const sem = new Semaphore(1);
const worker = () => {
return sem.lock(async () => {
nworkers++;
results.push(nworkers);
await new Promise((resolve) => setTimeout(resolve, 10));
nworkers--;
});
};
await Promise.all([...Array(10)].map(() => worker()));
assertEquals(nworkers, 0);
assertEquals(results, [
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
]);
},
);

await t.step(
"regulates the number of workers concurrently running (n=10)",
async () => {
let nworkers = 0;
const results: number[] = [];
const sem = new Semaphore(10);
const worker = () => {
return sem.lock(async () => {
nworkers++;
results.push(nworkers);
await new Promise((resolve) => setTimeout(resolve, 10));
nworkers--;
});
};
await Promise.all([...Array(10)].map(() => worker()));
assertEquals(nworkers, 0);
assertEquals(results, [
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
]);
},
);

await t.step(
"throws RangeError if size is not a positive safe integer",
() => {
assertThrows(() => new Semaphore(NaN), RangeError);
assertThrows(() => new Semaphore(Infinity), RangeError);
assertThrows(() => new Semaphore(-Infinity), RangeError);
assertThrows(() => new Semaphore(-1), RangeError);
assertThrows(() => new Semaphore(1.1), RangeError);
assertThrows(() => new Semaphore(0), RangeError);
},
);
});

0 comments on commit 8ed0442

Please sign in to comment.