Skip to content

Commit

Permalink
feat(wait_group): throw RangeError for invalid delta
Browse files Browse the repository at this point in the history
  • Loading branch information
lambdalisue committed Aug 13, 2024
1 parent 8ed0442 commit f3db52a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 3 additions & 0 deletions wait_group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ export class WaitGroup {
* @param delta The number to add to the counter. It can be positive or negative.
*/
add(delta: number): void {
if (!Number.isSafeInteger(delta)) {
throw new RangeError(`delta must be a safe integer, got ${delta}`);
}
this.#count += delta;
if (this.#count === 0) {
this.#notify.notifyAll();
Expand Down
13 changes: 12 additions & 1 deletion wait_group_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertEquals, assertRejects } from "@std/assert";
import { assertEquals, assertRejects, assertThrows } from "@std/assert";
import { deadline, delay } from "@std/async";
import { WaitGroup } from "./wait_group.ts";

Expand Down Expand Up @@ -83,4 +83,15 @@ Deno.test("WaitGroup", async (t) => {
);
},
);

await t.step(
"'add' throws RangeError if delta is not a safe integer",
() => {
const wg = new WaitGroup();
assertThrows(() => wg.add(NaN), RangeError);
assertThrows(() => wg.add(Infinity), RangeError);
assertThrows(() => wg.add(-Infinity), RangeError);
assertThrows(() => wg.add(1.1), RangeError);
},
);
});

0 comments on commit f3db52a

Please sign in to comment.