|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as assert from 'assert'; |
| 7 | +import { LocationCache } from '../src/wizard/LocationCache'; |
| 8 | + |
| 9 | +suite('LocationCache', () => { |
| 10 | + let cache: LocationCache<string[]>; |
| 11 | + |
| 12 | + setup(() => { |
| 13 | + cache = new LocationCache(); |
| 14 | + }); |
| 15 | + |
| 16 | + test('returns data from loader on cache miss', async () => { |
| 17 | + const result = await cache.getOrLoad('key1', () => Promise.resolve(['eastus', 'westus'])); |
| 18 | + assert.deepStrictEqual(result, ['eastus', 'westus']); |
| 19 | + }); |
| 20 | + |
| 21 | + test('returns cached data on subsequent calls without calling loader again', async () => { |
| 22 | + let callCount = 0; |
| 23 | + const loader = () => { |
| 24 | + callCount++; |
| 25 | + return Promise.resolve(['eastus']); |
| 26 | + }; |
| 27 | + |
| 28 | + const result1 = await cache.getOrLoad('key1', loader); |
| 29 | + const result2 = await cache.getOrLoad('key1', loader); |
| 30 | + |
| 31 | + assert.deepStrictEqual(result1, ['eastus']); |
| 32 | + assert.deepStrictEqual(result2, ['eastus']); |
| 33 | + assert.strictEqual(callCount, 1, 'loader should only be called once'); |
| 34 | + }); |
| 35 | + |
| 36 | + test('uses separate entries for different keys', async () => { |
| 37 | + await cache.getOrLoad('sub1|false', () => Promise.resolve(['eastus'])); |
| 38 | + await cache.getOrLoad('sub2|false', () => Promise.resolve(['westus'])); |
| 39 | + |
| 40 | + const result1 = await cache.getOrLoad('sub1|false', () => Promise.reject(new Error('should not call'))); |
| 41 | + const result2 = await cache.getOrLoad('sub2|false', () => Promise.reject(new Error('should not call'))); |
| 42 | + |
| 43 | + assert.deepStrictEqual(result1, ['eastus']); |
| 44 | + assert.deepStrictEqual(result2, ['westus']); |
| 45 | + }); |
| 46 | + |
| 47 | + test('deduplicates concurrent in-flight requests for the same key', async () => { |
| 48 | + let callCount = 0; |
| 49 | + let resolve: (value: string[]) => void; |
| 50 | + const loader = () => { |
| 51 | + callCount++; |
| 52 | + return new Promise<string[]>(r => { resolve = r; }); |
| 53 | + }; |
| 54 | + |
| 55 | + const p1 = cache.getOrLoad('key1', loader); |
| 56 | + const p2 = cache.getOrLoad('key1', loader); |
| 57 | + |
| 58 | + // Both should be waiting on the same promise |
| 59 | + assert.strictEqual(callCount, 1, 'loader should only be called once for concurrent requests'); |
| 60 | + |
| 61 | + resolve!(['eastus']); |
| 62 | + const [result1, result2] = await Promise.all([p1, p2]); |
| 63 | + |
| 64 | + assert.deepStrictEqual(result1, ['eastus']); |
| 65 | + assert.deepStrictEqual(result2, ['eastus']); |
| 66 | + }); |
| 67 | + |
| 68 | + test('clear removes all cached entries', async () => { |
| 69 | + let callCount = 0; |
| 70 | + const loader = () => { |
| 71 | + callCount++; |
| 72 | + return Promise.resolve(['eastus']); |
| 73 | + }; |
| 74 | + |
| 75 | + await cache.getOrLoad('key1', loader); |
| 76 | + assert.strictEqual(callCount, 1); |
| 77 | + |
| 78 | + cache.clear(); |
| 79 | + |
| 80 | + await cache.getOrLoad('key1', loader); |
| 81 | + assert.strictEqual(callCount, 2, 'loader should be called again after clear'); |
| 82 | + }); |
| 83 | + |
| 84 | + test('clear prevents in-flight requests from repopulating the cache', async () => { |
| 85 | + let resolve: (value: string[]) => void; |
| 86 | + const loader = () => new Promise<string[]>(r => { resolve = r; }); |
| 87 | + |
| 88 | + const p1 = cache.getOrLoad('key1', loader); |
| 89 | + |
| 90 | + // Clear while the request is still in-flight |
| 91 | + cache.clear(); |
| 92 | + |
| 93 | + // Resolve the stale request |
| 94 | + resolve!(['stale']); |
| 95 | + await p1; |
| 96 | + |
| 97 | + // The stale result should NOT have been cached, so a new loader fires |
| 98 | + let callCount = 0; |
| 99 | + await cache.getOrLoad('key1', () => { callCount++; return Promise.resolve(['fresh']); }); |
| 100 | + assert.strictEqual(callCount, 1, 'loader should be called because stale result was not cached'); |
| 101 | + }); |
| 102 | + |
| 103 | + test('expired entries are refreshed (injectable clock)', async () => { |
| 104 | + let time = 1000; |
| 105 | + const clock = () => time; |
| 106 | + const ttlCache = new LocationCache<string[]>(100, clock); |
| 107 | + |
| 108 | + let callCount = 0; |
| 109 | + const loader = () => { |
| 110 | + callCount++; |
| 111 | + return Promise.resolve([`result-${callCount}`]); |
| 112 | + }; |
| 113 | + |
| 114 | + const result1 = await ttlCache.getOrLoad('key1', loader); |
| 115 | + assert.deepStrictEqual(result1, ['result-1']); |
| 116 | + |
| 117 | + // Advance past TTL |
| 118 | + time = 1200; |
| 119 | + |
| 120 | + const result2 = await ttlCache.getOrLoad('key1', loader); |
| 121 | + assert.deepStrictEqual(result2, ['result-2']); |
| 122 | + assert.strictEqual(callCount, 2, 'loader should be called again after expiry'); |
| 123 | + }); |
| 124 | + |
| 125 | + test('entries without TTL never expire', async () => { |
| 126 | + let callCount = 0; |
| 127 | + |
| 128 | + await cache.getOrLoad('key1', () => { callCount++; return Promise.resolve(['eastus']); }); |
| 129 | + await cache.getOrLoad('key1', () => { callCount++; return Promise.resolve(['westus']); }); |
| 130 | + |
| 131 | + assert.strictEqual(callCount, 1); |
| 132 | + }); |
| 133 | + |
| 134 | + test('loader error does not poison the cache', async () => { |
| 135 | + let shouldFail = true; |
| 136 | + const loader = () => { |
| 137 | + if (shouldFail) { |
| 138 | + return Promise.reject(new Error('network error')); |
| 139 | + } |
| 140 | + return Promise.resolve(['eastus']); |
| 141 | + }; |
| 142 | + |
| 143 | + await assert.rejects(() => cache.getOrLoad('key1', loader), /network error/); |
| 144 | + |
| 145 | + shouldFail = false; |
| 146 | + const result = await cache.getOrLoad('key1', loader); |
| 147 | + assert.deepStrictEqual(result, ['eastus']); |
| 148 | + }); |
| 149 | + |
| 150 | + test('loader error is propagated to all concurrent waiters', async () => { |
| 151 | + let reject: (err: Error) => void; |
| 152 | + const loader = () => new Promise<string[]>((_, r) => { reject = r; }); |
| 153 | + |
| 154 | + const p1 = cache.getOrLoad('key1', loader); |
| 155 | + const p2 = cache.getOrLoad('key1', loader); |
| 156 | + |
| 157 | + reject!(new Error('boom')); |
| 158 | + |
| 159 | + await assert.rejects(() => p1, /boom/); |
| 160 | + await assert.rejects(() => p2, /boom/); |
| 161 | + }); |
| 162 | + |
| 163 | + test('after error, a new loader call succeeds', async () => { |
| 164 | + let callCount = 0; |
| 165 | + const failLoader = () => { callCount++; return Promise.reject(new Error('fail')); }; |
| 166 | + const okLoader = () => { callCount++; return Promise.resolve(['eastus']); }; |
| 167 | + |
| 168 | + await assert.rejects(() => cache.getOrLoad('key1', failLoader), /fail/); |
| 169 | + const result = await cache.getOrLoad('key1', okLoader); |
| 170 | + |
| 171 | + assert.deepStrictEqual(result, ['eastus']); |
| 172 | + assert.strictEqual(callCount, 2); |
| 173 | + }); |
| 174 | + |
| 175 | + test('synchronous throw from loader is handled', async () => { |
| 176 | + const loader = (): Promise<string[]> => { throw new Error('sync boom'); }; |
| 177 | + |
| 178 | + await assert.rejects(() => cache.getOrLoad('key1', loader), /sync boom/); |
| 179 | + |
| 180 | + // Cache should not be poisoned |
| 181 | + const result = await cache.getOrLoad('key1', () => Promise.resolve(['eastus'])); |
| 182 | + assert.deepStrictEqual(result, ['eastus']); |
| 183 | + }); |
| 184 | +}); |
0 commit comments