|
| 1 | +import { expect } from "chai"; |
| 2 | +import "mocha"; |
| 3 | +import { ExtensionContext, Memento } from "vscode"; |
| 4 | +import { InvocationRateLimiter } from "../../helpers"; |
| 5 | + |
| 6 | +describe("Invocation rate limiter", () => { |
| 7 | + function createInvocationRateLimiter<T>(funcIdentifier: string, func: () => Promise<T>): InvocationRateLimiter<T> { |
| 8 | + return new InvocationRateLimiter(new MockExtensionContext(), funcIdentifier, func); |
| 9 | + } |
| 10 | + |
| 11 | + it("initially invokes function", async () => { |
| 12 | + let numTimesFuncCalled = 0; |
| 13 | + const invocationRateLimiter = createInvocationRateLimiter("funcid", async () => { |
| 14 | + numTimesFuncCalled++; |
| 15 | + }); |
| 16 | + await invocationRateLimiter.invokeFunctionIfIntervalElapsed(100); |
| 17 | + expect(numTimesFuncCalled).to.equal(1); |
| 18 | + }); |
| 19 | + |
| 20 | + it("doesn't invoke function within time period", async () => { |
| 21 | + let numTimesFuncCalled = 0; |
| 22 | + const invocationRateLimiter = createInvocationRateLimiter("funcid", async () => { |
| 23 | + numTimesFuncCalled++; |
| 24 | + }); |
| 25 | + await invocationRateLimiter.invokeFunctionIfIntervalElapsed(100); |
| 26 | + await invocationRateLimiter.invokeFunctionIfIntervalElapsed(100); |
| 27 | + expect(numTimesFuncCalled).to.equal(1); |
| 28 | + }); |
| 29 | + |
| 30 | + it("invoke function again after 0s time period has elapsed", async () => { |
| 31 | + let numTimesFuncCalled = 0; |
| 32 | + const invocationRateLimiter = createInvocationRateLimiter("funcid", async () => { |
| 33 | + numTimesFuncCalled++; |
| 34 | + }); |
| 35 | + await invocationRateLimiter.invokeFunctionIfIntervalElapsed(0); |
| 36 | + await invocationRateLimiter.invokeFunctionIfIntervalElapsed(0); |
| 37 | + expect(numTimesFuncCalled).to.equal(2); |
| 38 | + }); |
| 39 | + |
| 40 | + it("invoke function again after 1s time period has elapsed", async () => { |
| 41 | + let numTimesFuncCalled = 0; |
| 42 | + const invocationRateLimiter = createInvocationRateLimiter("funcid", async () => { |
| 43 | + numTimesFuncCalled++; |
| 44 | + }); |
| 45 | + await invocationRateLimiter.invokeFunctionIfIntervalElapsed(1); |
| 46 | + await new Promise((resolve, _reject) => setTimeout(() => resolve(), 1000)); |
| 47 | + await invocationRateLimiter.invokeFunctionIfIntervalElapsed(1); |
| 48 | + expect(numTimesFuncCalled).to.equal(2); |
| 49 | + }); |
| 50 | + |
| 51 | + it("invokes functions with different rate limiters", async () => { |
| 52 | + let numTimesFuncACalled = 0; |
| 53 | + const invocationRateLimiterA = createInvocationRateLimiter("funcid", async () => { |
| 54 | + numTimesFuncACalled++; |
| 55 | + }); |
| 56 | + let numTimesFuncBCalled = 0; |
| 57 | + const invocationRateLimiterB = createInvocationRateLimiter("funcid", async () => { |
| 58 | + numTimesFuncBCalled++; |
| 59 | + }); |
| 60 | + await invocationRateLimiterA.invokeFunctionIfIntervalElapsed(100); |
| 61 | + await invocationRateLimiterB.invokeFunctionIfIntervalElapsed(100); |
| 62 | + expect(numTimesFuncACalled).to.equal(1); |
| 63 | + expect(numTimesFuncBCalled).to.equal(1); |
| 64 | + }); |
| 65 | +}); |
| 66 | + |
| 67 | +class MockExtensionContext implements ExtensionContext { |
| 68 | + subscriptions: { dispose(): unknown; }[] = []; |
| 69 | + workspaceState: Memento = new MockMemento(); |
| 70 | + globalState: Memento = new MockMemento(); |
| 71 | + extensionPath: string = ""; |
| 72 | + asAbsolutePath(_relativePath: string): string { |
| 73 | + throw new Error("Method not implemented."); |
| 74 | + } |
| 75 | + storagePath: string = ""; |
| 76 | + globalStoragePath: string = ""; |
| 77 | + logPath: string = ""; |
| 78 | +} |
| 79 | + |
| 80 | +class MockMemento implements Memento { |
| 81 | + map = new Map<any, any>(); |
| 82 | + |
| 83 | + /** |
| 84 | + * Return a value. |
| 85 | + * |
| 86 | + * @param key A string. |
| 87 | + * @param defaultValue A value that should be returned when there is no |
| 88 | + * value (`undefined`) with the given key. |
| 89 | + * @return The stored value or the defaultValue. |
| 90 | + */ |
| 91 | + get<T>(key: string, defaultValue?: T): T { |
| 92 | + return this.map.has(key) ? this.map.get(key) : defaultValue; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Store a value. The value must be JSON-stringifyable. |
| 97 | + * |
| 98 | + * @param key A string. |
| 99 | + * @param value A value. MUST not contain cyclic references. |
| 100 | + */ |
| 101 | + async update(key: string, value: any): Promise<void> { |
| 102 | + this.map.set(key, value); |
| 103 | + } |
| 104 | +} |
0 commit comments