-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathtest.ts
341 lines (295 loc) · 9.05 KB
/
test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import test from 'ava';
import delay from 'delay';
import serializeJavascript from 'serialize-javascript';
import memoize, {memoizeDecorator, memoizeClear} from './index.js';
test('memoize', t => {
let index = 0;
const fixture = (a?: unknown, b?: unknown) => index++;
const memoized = memoize(fixture);
t.is(memoized(), 0);
t.is(memoized(), 0);
t.is(memoized(), 0);
t.is(memoized(undefined), 0);
t.is(memoized(undefined), 0);
t.is(memoized('foo'), 1);
t.is(memoized('foo'), 1);
t.is(memoized('foo'), 1);
t.is(memoized('foo', 'bar'), 1);
t.is(memoized('foo', 'bar'), 1);
t.is(memoized('foo', 'bar'), 1);
t.is(memoized(1), 2);
t.is(memoized(1), 2);
t.is(memoized(null), 3);
t.is(memoized(null), 3);
t.is(memoized(fixture), 4);
t.is(memoized(fixture), 4);
t.is(memoized(true), 5);
t.is(memoized(true), 5);
// Ensure that functions are stored by reference and not by "value" (e.g. their `.toString()` representation)
t.is(memoized(() => index++), 6);
t.is(memoized(() => index++), 7);
});
test('cacheKey option', t => {
let index = 0;
const fixture = (..._arguments: any) => index++;
const memoized = memoize(fixture, {cacheKey: ([firstArgument]) => String(firstArgument)});
t.is(memoized(1), 0);
t.is(memoized(1), 0);
t.is(memoized('1'), 0);
t.is(memoized('2'), 1);
t.is(memoized(2), 1);
});
test('memoize with multiple non-primitive arguments', t => {
let index = 0;
const memoized = memoize((a?: unknown, b?: unknown, c?: unknown) => index++, {cacheKey: JSON.stringify});
t.is(memoized(), 0);
t.is(memoized(), 0);
t.is(memoized({foo: true}, {bar: false}), 1);
t.is(memoized({foo: true}, {bar: false}), 1);
t.is(memoized({foo: true}, {bar: false}, {baz: true}), 2);
t.is(memoized({foo: true}, {bar: false}, {baz: true}), 2);
});
test('memoize with regexp arguments', t => {
let index = 0;
const memoized = memoize((a?: unknown) => index++, {cacheKey: serializeJavascript});
t.is(memoized(), 0);
t.is(memoized(), 0);
t.is(memoized(/Sindre Sorhus/), 1);
t.is(memoized(/Sindre Sorhus/), 1);
t.is(memoized(/Elvin Peng/), 2);
t.is(memoized(/Elvin Peng/), 2);
});
test('memoize with Symbol arguments', t => {
let index = 0;
const argument1 = Symbol('fixture1');
const argument2 = Symbol('fixture2');
const memoized = memoize((a?: unknown) => index++);
t.is(memoized(), 0);
t.is(memoized(), 0);
t.is(memoized(argument1), 1);
t.is(memoized(argument1), 1);
t.is(memoized(argument2), 2);
t.is(memoized(argument2), 2);
});
test('maxAge option', async t => {
let index = 0;
const fixture = (a?: unknown) => index++;
const memoized = memoize(fixture, {maxAge: 100});
t.is(memoized(1), 0);
t.is(memoized(1), 0);
await delay(50);
t.is(memoized(1), 0);
await delay(200);
t.is(memoized(1), 1);
});
test('maxAge option deletes old items', async t => {
let index = 0;
const fixture = (a?: unknown) => index++;
const cache = new Map<number, number>();
const deleted: number[] = [];
const _delete = cache.delete.bind(cache);
cache.delete = item => {
deleted.push(item);
return _delete(item);
};
const memoized = memoize(fixture, {maxAge: 100, cache});
t.is(memoized(1), 0);
t.is(memoized(1), 0);
t.is(cache.has(1), true);
await delay(50);
t.is(memoized(1), 0);
t.is(deleted.length, 0);
await delay(200);
t.is(memoized(1), 1);
t.is(deleted.length, 1);
t.is(deleted[0], 1);
});
test('maxAge items are deleted even if function throws', async t => {
let index = 0;
const fixture = (a?: unknown) => {
if (index === 1) {
throw new Error('failure');
}
return index++;
};
const cache = new Map();
const memoized = memoize(fixture, {maxAge: 100, cache});
t.is(memoized(1), 0);
t.is(memoized(1), 0);
t.is(cache.size, 1);
await delay(50);
t.is(memoized(1), 0);
await delay(200);
t.throws(() => {
memoized(1);
}, {message: 'failure'});
t.is(cache.size, 0);
});
test('cache option', t => {
let index = 0;
const fixture = (..._arguments: any) => index++;
const memoized = memoize(fixture, {
cache: new WeakMap(),
cacheKey: <ReturnValue>([firstArgument]: [ReturnValue]): ReturnValue => firstArgument,
});
const foo = {};
const bar = {};
t.is(memoized(foo), 0);
t.is(memoized(foo), 0);
t.is(memoized(bar), 1);
t.is(memoized(bar), 1);
});
test('promise support', async t => {
let index = 0;
const memoized = memoize(async (a?: unknown) => index++);
t.is(await memoized(), 0);
t.is(await memoized(), 0);
t.is(await memoized(10), 1);
});
test('preserves the original function name', t => {
t.is(memoize(function foo() {}).name, 'foo'); // eslint-disable-line func-names, @typescript-eslint/no-empty-function
});
test('.clear()', t => {
let index = 0;
const fixture = () => index++;
const memoized = memoize(fixture);
t.is(memoized(), 0);
t.is(memoized(), 0);
memoizeClear(memoized);
t.is(memoized(), 1);
t.is(memoized(), 1);
});
test('prototype support', t => {
class Unicorn {
index = 0;
foo() {
return this.index++;
}
}
Unicorn.prototype.foo = memoize(Unicorn.prototype.foo);
const unicorn = new Unicorn();
t.is(unicorn.foo(), 0);
t.is(unicorn.foo(), 0);
t.is(unicorn.foo(), 0);
});
test('memoizeDecorator()', t => {
let returnValue = 1;
const returnValue2 = 101;
class TestClass {
@memoizeDecorator()
counter() {
return returnValue++;
}
@memoizeDecorator()
counter2() {
return returnValue2;
}
}
const alpha = new TestClass();
t.is(alpha.counter(), 1);
t.is(alpha.counter(), 1, 'The method should be memoized');
t.is(alpha.counter2(), 101, 'The method should be memoized separately from the other one');
const beta = new TestClass();
t.is(beta.counter(), 2, 'The method should not be memoized across instances');
});
test('memoizeClear() throws when called with a plain function', t => {
t.throws(() => {
memoizeClear(() => {}); // eslint-disable-line @typescript-eslint/no-empty-function
}, {
message: 'Can\'t clear a function that was not memoized!',
instanceOf: TypeError,
});
});
test('memoizeClear() throws when called on an unclearable cache', t => {
const fixture = () => 1;
const memoized = memoize(fixture, {
cache: new WeakMap(),
});
t.throws(() => {
memoizeClear(memoized);
}, {
message: 'The cache Map can\'t be cleared!',
instanceOf: TypeError,
});
});
test('maxAge - cache item expires after specified duration', async t => {
let index = 0;
const fixture = () => index++;
const memoized = memoize(fixture, {maxAge: 100});
t.is(memoized(), 0); // Initial call, cached
t.is(memoized(), 0); // Subsequent call, still cached
await delay(150); // Wait for longer than maxAge
t.is(memoized(), 1); // Cache expired, should compute again
});
test('maxAge - cache expiration timing is accurate', async t => {
let index = 0;
const fixture = () => index++;
const memoized = memoize(fixture, {maxAge: 100});
t.is(memoized(), 0);
await delay(90); // Wait for slightly less than maxAge
t.is(memoized(), 0); // Should still be cached
await delay(20); // Total delay now exceeds maxAge
t.is(memoized(), 1); // Should recompute as cache has expired
});
test('maxAge - expired items are not present in cache', async t => {
let index = 0;
const fixture = () => index++;
const cache = new Map();
const memoized = memoize(fixture, {maxAge: 100, cache});
memoized(); // Call to cache the result
await delay(150); // Wait for cache to expire
memoized(); // Recompute and recache
t.is(cache.size, 1); // Only one item should be in the cache
});
test('maxAge - complex arguments and cache expiration', async t => {
let index = 0;
const fixture = object => index++;
const memoized = memoize(fixture, {maxAge: 100, cacheKey: JSON.stringify});
const argument = {key: 'value'};
t.is(memoized(argument), 0);
await delay(150);
t.is(memoized(argument), 1); // Argument is the same, but should recompute due to expiration
});
test('maxAge - concurrent calls return cached value', async t => {
let index = 0;
const fixture = () => index++;
const memoized = memoize(fixture, {maxAge: 100});
t.is(memoized(), 0);
await delay(50); // Delay less than maxAge
t.is(memoized(), 0); // Should return cached value
});
test('maxAge - different arguments have separate expirations', async t => {
let index = 0;
const fixture = x => index++;
const memoized = memoize(fixture, {maxAge: 100});
t.is(memoized('a'), 0);
await delay(150); // Expire the cache for 'a'
t.is(memoized('b'), 1); // 'b' should be a separate cache entry
t.is(memoized('a'), 2); // 'a' should be recomputed
});
test('maxAge - zero maxAge means no caching', t => {
let index = 0;
const fixture = () => index++;
const memoized = memoize(fixture, {maxAge: 0});
t.is(memoized(), 0);
t.is(memoized(), 1); // No caching, should increment
});
test('maxAge - immediate expiration', async t => {
let index = 0;
const fixture = () => index++;
const memoized = memoize(fixture, {maxAge: 1});
t.is(memoized(), 0);
await delay(10);
t.is(memoized(), 1); // Cache should expire immediately
});
test('maxAge - high concurrency', async t => {
let index = 0;
const fixture = () => index++;
const memoized = memoize(fixture, {maxAge: 50});
// Simulate concurrent calls
for (let job = 0; job < 10_000; job++) {
memoized();
}
await delay(100);
t.is(memoized(), 1);
});