| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import {RateLimiter} from '../RateLimiter'; |
| b69ab31 | | | 9 | import {nextTick} from '../testUtils'; |
| b69ab31 | | | 10 | import {defer} from '../utils'; |
| b69ab31 | | | 11 | |
| b69ab31 | | | 12 | describe('RateLimiter', () => { |
| b69ab31 | | | 13 | it('immediately invokes if less than max simultaneous requests are running', () => { |
| b69ab31 | | | 14 | const d1 = defer(); |
| b69ab31 | | | 15 | const d2 = defer(); |
| b69ab31 | | | 16 | const d3 = defer(); |
| b69ab31 | | | 17 | const rateLimiter = new RateLimiter(3); |
| b69ab31 | | | 18 | let ran1 = false; |
| b69ab31 | | | 19 | let ran2 = false; |
| b69ab31 | | | 20 | let ran3 = false; |
| b69ab31 | | | 21 | rateLimiter.enqueueRun(async () => { |
| b69ab31 | | | 22 | ran1 = true; |
| b69ab31 | | | 23 | await d1.promise; |
| b69ab31 | | | 24 | }); |
| b69ab31 | | | 25 | rateLimiter.enqueueRun(async () => { |
| b69ab31 | | | 26 | ran2 = true; |
| b69ab31 | | | 27 | await d2.promise; |
| b69ab31 | | | 28 | }); |
| b69ab31 | | | 29 | rateLimiter.enqueueRun(async () => { |
| b69ab31 | | | 30 | ran3 = true; |
| b69ab31 | | | 31 | await d3.promise; |
| b69ab31 | | | 32 | }); |
| b69ab31 | | | 33 | expect(ran1).toBe(true); |
| b69ab31 | | | 34 | expect(ran2).toBe(true); |
| b69ab31 | | | 35 | expect(ran3).toBe(true); |
| b69ab31 | | | 36 | }); |
| b69ab31 | | | 37 | |
| b69ab31 | | | 38 | it('queues requests over max simultaneous until a previous task finishes', async () => { |
| b69ab31 | | | 39 | const d1 = defer(); |
| b69ab31 | | | 40 | const d2 = defer(); |
| b69ab31 | | | 41 | const rateLimiter = new RateLimiter(2); |
| b69ab31 | | | 42 | rateLimiter.enqueueRun(() => d1.promise); |
| b69ab31 | | | 43 | rateLimiter.enqueueRun(() => d2.promise); |
| b69ab31 | | | 44 | |
| b69ab31 | | | 45 | let hasId3Resolved = false; |
| b69ab31 | | | 46 | rateLimiter |
| b69ab31 | | | 47 | .enqueueRun(() => Promise.resolve()) |
| b69ab31 | | | 48 | .then(() => { |
| b69ab31 | | | 49 | hasId3Resolved = true; |
| b69ab31 | | | 50 | }); |
| b69ab31 | | | 51 | expect(hasId3Resolved).toBe(false); |
| b69ab31 | | | 52 | |
| b69ab31 | | | 53 | d2.resolve(undefined); |
| b69ab31 | | | 54 | await nextTick(); |
| b69ab31 | | | 55 | expect(hasId3Resolved).toBe(true); |
| b69ab31 | | | 56 | }); |
| b69ab31 | | | 57 | |
| b69ab31 | | | 58 | it('can be used as a lock with concurrency limit 1', async () => { |
| b69ab31 | | | 59 | const d1 = defer(); |
| b69ab31 | | | 60 | const d2 = defer(); |
| b69ab31 | | | 61 | const d3 = defer(); |
| b69ab31 | | | 62 | const rateLimiter = new RateLimiter(1); |
| b69ab31 | | | 63 | let ran1 = false; |
| b69ab31 | | | 64 | let ran2 = false; |
| b69ab31 | | | 65 | let ran3 = false; |
| b69ab31 | | | 66 | rateLimiter.enqueueRun(async () => { |
| b69ab31 | | | 67 | await d1.promise; |
| b69ab31 | | | 68 | ran1 = true; |
| b69ab31 | | | 69 | }); |
| b69ab31 | | | 70 | rateLimiter.enqueueRun(async () => { |
| b69ab31 | | | 71 | await d2.promise; |
| b69ab31 | | | 72 | ran2 = true; |
| b69ab31 | | | 73 | }); |
| b69ab31 | | | 74 | rateLimiter.enqueueRun(async () => { |
| b69ab31 | | | 75 | await d3.promise; |
| b69ab31 | | | 76 | ran3 = true; |
| b69ab31 | | | 77 | }); |
| b69ab31 | | | 78 | |
| b69ab31 | | | 79 | expect(ran1).toBe(false); |
| b69ab31 | | | 80 | expect(ran2).toBe(false); |
| b69ab31 | | | 81 | expect(ran3).toBe(false); |
| b69ab31 | | | 82 | |
| b69ab31 | | | 83 | d1.resolve(undefined); |
| b69ab31 | | | 84 | await nextTick(); |
| b69ab31 | | | 85 | |
| b69ab31 | | | 86 | expect(ran1).toBe(true); |
| b69ab31 | | | 87 | expect(ran2).toBe(false); |
| b69ab31 | | | 88 | expect(ran3).toBe(false); |
| b69ab31 | | | 89 | |
| b69ab31 | | | 90 | d2.resolve(undefined); |
| b69ab31 | | | 91 | await nextTick(); |
| b69ab31 | | | 92 | |
| b69ab31 | | | 93 | expect(ran1).toBe(true); |
| b69ab31 | | | 94 | expect(ran2).toBe(true); |
| b69ab31 | | | 95 | expect(ran3).toBe(false); |
| b69ab31 | | | 96 | |
| b69ab31 | | | 97 | d3.resolve(undefined); |
| b69ab31 | | | 98 | await nextTick(); |
| b69ab31 | | | 99 | |
| b69ab31 | | | 100 | expect(ran1).toBe(true); |
| b69ab31 | | | 101 | expect(ran2).toBe(true); |
| b69ab31 | | | 102 | expect(ran3).toBe(true); |
| b69ab31 | | | 103 | }); |
| b69ab31 | | | 104 | |
| b69ab31 | | | 105 | it('Handles async work that rejects', async () => { |
| b69ab31 | | | 106 | const d1 = defer(); |
| b69ab31 | | | 107 | const d2 = defer(); |
| b69ab31 | | | 108 | const rateLimiter = new RateLimiter(2); |
| b69ab31 | | | 109 | rateLimiter.enqueueRun(() => d1.promise); |
| b69ab31 | | | 110 | let sawError = false; |
| b69ab31 | | | 111 | rateLimiter |
| b69ab31 | | | 112 | .enqueueRun(async () => { |
| b69ab31 | | | 113 | await d2.promise; |
| b69ab31 | | | 114 | throw new Error(); |
| b69ab31 | | | 115 | }) |
| b69ab31 | | | 116 | .catch(() => { |
| b69ab31 | | | 117 | sawError = true; |
| b69ab31 | | | 118 | }); |
| b69ab31 | | | 119 | |
| b69ab31 | | | 120 | let hasId3Resolved = false; |
| b69ab31 | | | 121 | rateLimiter |
| b69ab31 | | | 122 | .enqueueRun(() => Promise.resolve()) |
| b69ab31 | | | 123 | .then(() => { |
| b69ab31 | | | 124 | hasId3Resolved = true; |
| b69ab31 | | | 125 | }); |
| b69ab31 | | | 126 | expect(hasId3Resolved).toBe(false); |
| b69ab31 | | | 127 | |
| b69ab31 | | | 128 | d2.resolve(undefined); |
| b69ab31 | | | 129 | await nextTick(); |
| b69ab31 | | | 130 | expect(hasId3Resolved).toBe(true); |
| b69ab31 | | | 131 | expect(sawError).toBe(true); |
| b69ab31 | | | 132 | }); |
| b69ab31 | | | 133 | }); |