diff --git a/wait_group.ts b/wait_group.ts index b98d165..2f9bb46 100644 --- a/wait_group.ts +++ b/wait_group.ts @@ -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(); diff --git a/wait_group_test.ts b/wait_group_test.ts index 786cb20..bcbbca2 100644 --- a/wait_group_test.ts +++ b/wait_group_test.ts @@ -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"; @@ -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); + }, + ); });