Skip to content

Commit f3db52a

Browse files
committed
feat(wait_group): throw RangeError for invalid delta
1 parent 8ed0442 commit f3db52a

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

wait_group.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ export class WaitGroup {
3636
* @param delta The number to add to the counter. It can be positive or negative.
3737
*/
3838
add(delta: number): void {
39+
if (!Number.isSafeInteger(delta)) {
40+
throw new RangeError(`delta must be a safe integer, got ${delta}`);
41+
}
3942
this.#count += delta;
4043
if (this.#count === 0) {
4144
this.#notify.notifyAll();

wait_group_test.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { assertEquals, assertRejects } from "@std/assert";
1+
import { assertEquals, assertRejects, assertThrows } from "@std/assert";
22
import { deadline, delay } from "@std/async";
33
import { WaitGroup } from "./wait_group.ts";
44

@@ -83,4 +83,15 @@ Deno.test("WaitGroup", async (t) => {
8383
);
8484
},
8585
);
86+
87+
await t.step(
88+
"'add' throws RangeError if delta is not a safe integer",
89+
() => {
90+
const wg = new WaitGroup();
91+
assertThrows(() => wg.add(NaN), RangeError);
92+
assertThrows(() => wg.add(Infinity), RangeError);
93+
assertThrows(() => wg.add(-Infinity), RangeError);
94+
assertThrows(() => wg.add(1.1), RangeError);
95+
},
96+
);
8697
});

0 commit comments

Comments
 (0)