-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
32 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,34 @@ | ||
import { assert } from "https://deno.land/std@0.177.0/testing/asserts.ts"; | ||
import { createThrottle } from "./createThrottle.ts"; | ||
import { assert } from "assert"; | ||
|
||
Deno.test("should allow requests within limit", () => { | ||
import { createThrottle } from "@/src/createThrottle.ts"; | ||
|
||
Deno.test("should allow requests within limit", async () => { | ||
const throttle = createThrottle({ limit: 5, interval: 1000 }); | ||
|
||
for (let i = 0; i < 5; i++) { | ||
assert(throttle.check(), `request ${i + 1} should be allowed`); | ||
await throttle.wait(); | ||
assert(true, `request ${i + 1} should be allowed`); | ||
} | ||
}); | ||
|
||
Deno.test("should block requests exceeding limit", async () => { | ||
const throttle = createThrottle({ limit: 2, interval: 1000 }); | ||
|
||
assert(throttle.check(), "first request should be allowed"); | ||
assert(throttle.check(), "second request should be allowed"); | ||
assert(!throttle.check(), "third request should be blocked"); | ||
await throttle.wait(); | ||
await throttle.wait(); | ||
|
||
let blocked = false; | ||
|
||
try { | ||
await throttle.wait(); | ||
} catch { | ||
blocked = true; | ||
} | ||
|
||
assert(blocked, "third request should be blocked"); | ||
|
||
// Wait for the interval to pass | ||
await new Promise((resolve) => setTimeout(resolve, 1000)); | ||
await throttle.wait(); | ||
|
||
assert( | ||
throttle.check(), | ||
"fourth request should be allowed after interval", | ||
); | ||
assert(true, "fourth request should be allowed after interval"); | ||
}); |