Skip to content

Commit 7f4053e

Browse files
committed
test: rateLimiter.getSendToken
1 parent 97e9438 commit 7f4053e

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

packages/middleware-retry/src/DefaultRateLimiter.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,33 @@ describe(DefaultRateLimiter.name, () => {
1313
jest.clearAllMocks();
1414
});
1515

16+
describe("getSendToken", () => {
17+
beforeEach(() => {
18+
jest.useFakeTimers();
19+
});
20+
21+
afterEach(() => {
22+
jest.useRealTimers();
23+
});
24+
25+
it.each([
26+
[0.5, 892.8571428571428],
27+
[1, 1785.7142857142856],
28+
[2, 2000],
29+
])("timestamp: %d, delay: %d", async (timestamp, delay) => {
30+
jest.spyOn(Date, "now").mockImplementation(() => 0);
31+
const rateLimiter = new DefaultRateLimiter();
32+
33+
(isThrottlingError as jest.Mock).mockReturnValueOnce(true);
34+
jest.spyOn(Date, "now").mockImplementation(() => timestamp * 1000);
35+
rateLimiter.updateClientSendingRate({});
36+
37+
rateLimiter.getSendToken();
38+
jest.runAllTimers();
39+
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), delay);
40+
});
41+
});
42+
1643
describe("cubicSuccess", () => {
1744
it.each([
1845
[5, 7],

packages/middleware-retry/src/DefaultRateLimiter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export class DefaultRateLimiter implements RateLimiter {
6464

6565
this.refillTokenBucket();
6666
if (amount > this.currentCapacity) {
67-
const delay = (amount - this.currentCapacity) / this.fillRate;
67+
const delay = ((amount - this.currentCapacity) / this.fillRate) * 1000;
6868
await new Promise((resolve) => setTimeout(resolve, delay));
6969
}
7070
this.currentCapacity = this.currentCapacity - amount;

0 commit comments

Comments
 (0)