|
| 1 | +import { isThrottlingError } from "@aws-sdk/service-error-classification"; |
| 2 | + |
| 3 | +import { DefaultRateLimiter } from "./DefaultRateLimiter"; |
| 4 | + |
| 5 | +jest.mock("@aws-sdk/service-error-classification"); |
| 6 | + |
| 7 | +describe(DefaultRateLimiter.name, () => { |
| 8 | + beforeEach(() => { |
| 9 | + (isThrottlingError as jest.Mock).mockReturnValue(false); |
| 10 | + }); |
| 11 | + |
| 12 | + afterEach(() => { |
| 13 | + jest.clearAllMocks(); |
| 14 | + }); |
| 15 | + |
| 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 | + |
| 43 | + describe("cubicSuccess", () => { |
| 44 | + it.each([ |
| 45 | + [5, 7], |
| 46 | + [6, 9.64893601], |
| 47 | + [7, 10.00003085], |
| 48 | + [8, 10.45328452], |
| 49 | + [9, 13.40869703], |
| 50 | + [10, 21.26626836], |
| 51 | + [11, 36.42599853], |
| 52 | + ])("timestamp: %d, calculatedRate: %d", (timestamp, calculatedRate) => { |
| 53 | + jest.spyOn(Date, "now").mockImplementation(() => 0); |
| 54 | + const rateLimiter = new DefaultRateLimiter(); |
| 55 | + rateLimiter["lastMaxRate"] = 10; |
| 56 | + rateLimiter["lastThrottleTime"] = 5; |
| 57 | + |
| 58 | + jest.spyOn(Date, "now").mockImplementation(() => timestamp * 1000); |
| 59 | + |
| 60 | + const cubicSuccessSpy = jest.spyOn(DefaultRateLimiter.prototype as any, "cubicSuccess"); |
| 61 | + rateLimiter.updateClientSendingRate({}); |
| 62 | + expect(cubicSuccessSpy).toHaveLastReturnedWith(calculatedRate); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + describe("cubicThrottle", () => { |
| 67 | + it.each([ |
| 68 | + [5, 0.112], |
| 69 | + [6, 0.09333333], |
| 70 | + [7, 0.08], |
| 71 | + [8, 0.07], |
| 72 | + [9, 0.06222222], |
| 73 | + ])("timestamp: %d, calculatedRate: %d", (timestamp, calculatedRate) => { |
| 74 | + jest.spyOn(Date, "now").mockImplementation(() => 0); |
| 75 | + const rateLimiter = new DefaultRateLimiter(); |
| 76 | + rateLimiter["lastMaxRate"] = 10; |
| 77 | + rateLimiter["lastThrottleTime"] = 5; |
| 78 | + |
| 79 | + (isThrottlingError as jest.Mock).mockReturnValueOnce(true); |
| 80 | + jest.spyOn(Date, "now").mockImplementation(() => timestamp * 1000); |
| 81 | + const cubicThrottleSpy = jest.spyOn(DefaultRateLimiter.prototype as any, "cubicThrottle"); |
| 82 | + rateLimiter.updateClientSendingRate({}); |
| 83 | + expect(cubicThrottleSpy).toHaveLastReturnedWith(calculatedRate); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + it("updateClientSendingRate", () => { |
| 88 | + jest.spyOn(Date, "now").mockImplementation(() => 0); |
| 89 | + const rateLimiter = new DefaultRateLimiter(); |
| 90 | + |
| 91 | + const testCases: [boolean, number, number, number][] = [ |
| 92 | + [false, 0.2, 0, 0.5], |
| 93 | + [false, 0.4, 0, 0.5], |
| 94 | + [false, 0.6, 4.8, 0.5], |
| 95 | + [false, 0.8, 4.8, 0.5], |
| 96 | + [false, 1, 4.16, 0.5], |
| 97 | + [false, 1.2, 4.16, 0.6912], |
| 98 | + [false, 1.4, 4.16, 1.0976], |
| 99 | + [false, 1.6, 5.632, 1.6384], |
| 100 | + [false, 1.8, 5.632, 2.3328], |
| 101 | + [true, 2, 4.3264, 3.02848], |
| 102 | + [false, 2.2, 4.3264, 3.486639], |
| 103 | + [false, 2.4, 4.3264, 3.821874], |
| 104 | + [false, 2.6, 5.66528, 4.053386], |
| 105 | + [false, 2.8, 5.66528, 4.200373], |
| 106 | + [false, 3.0, 4.333056, 4.282037], |
| 107 | + [true, 3.2, 4.333056, 2.997426], |
| 108 | + [false, 3.4, 4.333056, 3.452226], |
| 109 | + ]; |
| 110 | + |
| 111 | + testCases.forEach(([isThrottlingErrorReturn, timestamp, measuredTxRate, fillRate]) => { |
| 112 | + (isThrottlingError as jest.Mock).mockReturnValue(isThrottlingErrorReturn); |
| 113 | + jest.spyOn(Date, "now").mockImplementation(() => timestamp * 1000); |
| 114 | + |
| 115 | + rateLimiter.updateClientSendingRate({}); |
| 116 | + expect(rateLimiter["measuredTxRate"]).toEqual(measuredTxRate); |
| 117 | + expect(parseFloat(rateLimiter["fillRate"].toFixed(6))).toEqual(fillRate); |
| 118 | + }); |
| 119 | + }); |
| 120 | +}); |
0 commit comments