|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +import assert from "assert"; |
| 6 | +import type { SinonFakeTimers } from "sinon"; |
| 7 | +import sinon from "sinon"; |
| 8 | + |
| 9 | +import RateLimiter, { RateLimiterState } from "../../../../src/core/upload/rate_limiter"; |
| 10 | + |
| 11 | +const sandbox = sinon.createSandbox(); |
| 12 | +const now = new Date(); |
| 13 | + |
| 14 | + |
| 15 | +describe("RateLimiter", function() { |
| 16 | + let clock: SinonFakeTimers; |
| 17 | + |
| 18 | + beforeEach(function() { |
| 19 | + clock = sandbox.useFakeTimers(now.getTime()); |
| 20 | + }); |
| 21 | + |
| 22 | + afterEach(function () { |
| 23 | + clock.restore(); |
| 24 | + }); |
| 25 | + |
| 26 | + it("rate limiter correctly resets in case elapsed time return an error", function () { |
| 27 | + const rateLimiter = new RateLimiter( |
| 28 | + 1000, /* interval */ |
| 29 | + 3, /* maxCount */ |
| 30 | + ); |
| 31 | + |
| 32 | + // Reach the count for the current interval. |
| 33 | + assert.deepStrictEqual(rateLimiter.getState(), { state: RateLimiterState.Incrementing }); |
| 34 | + assert.deepStrictEqual(rateLimiter.getState(), { state: RateLimiterState.Incrementing }); |
| 35 | + assert.deepStrictEqual(rateLimiter.getState(), { state: RateLimiterState.Incrementing }); |
| 36 | + |
| 37 | + sinon.replaceGetter(rateLimiter, "elapsed", () => NaN); |
| 38 | + assert.deepStrictEqual(rateLimiter.getState(), { state: RateLimiterState.Incrementing }); |
| 39 | + }); |
| 40 | + |
| 41 | + it("rate limiter correctly resets in case interval is over", function () { |
| 42 | + const rateLimiter = new RateLimiter( |
| 43 | + 1000, /* interval */ |
| 44 | + 3, /* maxCount */ |
| 45 | + ); |
| 46 | + |
| 47 | + // Reach the count for the current interval. |
| 48 | + assert.deepStrictEqual(rateLimiter.getState(), { state: RateLimiterState.Incrementing }); |
| 49 | + assert.deepStrictEqual(rateLimiter.getState(), { state: RateLimiterState.Incrementing }); |
| 50 | + assert.deepStrictEqual(rateLimiter.getState(), { state: RateLimiterState.Incrementing }); |
| 51 | + |
| 52 | + // Fake the time passing over the current interval |
| 53 | + sinon.replaceGetter(rateLimiter, "elapsed", () => 1000 * 2); |
| 54 | + assert.deepStrictEqual(rateLimiter.getState(), { state: RateLimiterState.Incrementing }); |
| 55 | + }); |
| 56 | + |
| 57 | + it("rate limiter returns throttled state when it is throttled", function () { |
| 58 | + const rateLimiter = new RateLimiter( |
| 59 | + 1000, /* interval */ |
| 60 | + 3, /* maxCount */ |
| 61 | + ); |
| 62 | + |
| 63 | + // Reach the count for the current interval. |
| 64 | + assert.deepStrictEqual(rateLimiter.getState(), { state: RateLimiterState.Incrementing }); |
| 65 | + assert.deepStrictEqual(rateLimiter.getState(), { state: RateLimiterState.Incrementing }); |
| 66 | + assert.deepStrictEqual(rateLimiter.getState(), { state: RateLimiterState.Incrementing }); |
| 67 | + |
| 68 | + // Try one more time and we should be throttled. |
| 69 | + const nextState = rateLimiter.getState(); |
| 70 | + assert.strictEqual(nextState.state, RateLimiterState.Throttled); |
| 71 | + assert.ok(nextState.remainingTime as number <= 1000 && nextState.remainingTime as number > 0); |
| 72 | + }); |
| 73 | + |
| 74 | + it("rate limiter returns stopped state when it is stopped", function () { |
| 75 | + const rateLimiter = new RateLimiter( |
| 76 | + 1000, /* interval */ |
| 77 | + 3, /* maxCount */ |
| 78 | + ); |
| 79 | + |
| 80 | + // Don't reach the count for the current interval. |
| 81 | + assert.deepStrictEqual(rateLimiter.getState(), { state: RateLimiterState.Incrementing }); |
| 82 | + |
| 83 | + // Stop the rate limiter |
| 84 | + rateLimiter.stop(); |
| 85 | + |
| 86 | + // Try one more time and we should be stopped. |
| 87 | + const nextState = rateLimiter.getState(); |
| 88 | + assert.strictEqual(nextState.state, RateLimiterState.Stopped); |
| 89 | + assert.ok(nextState.remainingTime as number <= 1000 && nextState.remainingTime as number > 0); |
| 90 | + }); |
| 91 | +}); |
0 commit comments