This repository was archived by the owner on Sep 30, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.ts
More file actions
121 lines (111 loc) · 4.05 KB
/
Copy pathindex.test.ts
File metadata and controls
121 lines (111 loc) · 4.05 KB
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
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable no-console */
import { strictEqual, throws } from 'assert';
import keep, { PromiseKeeper } from '.';
const start = process.hrtime();
const debug = (obj: PromiseKeeper<any>) => {
// @ts-ignore
const {
isEmpty, isPending, settledData, pendingData,
} = obj;
console.table({
isEmpty, isPending, settledData: typeof settledData, pendingData: typeof pendingData,
});
};
const sleep = (time: number) => new Promise((resolve) => {
setTimeout(resolve, time);
});
const timeOffset = () => {
const [seconds, nanoseconds] = process.hrtime(start);
const milliseconds = (seconds * 1_000 + nanoseconds / 1_000_000);
return Math.ceil(milliseconds).toLocaleString();
};
const mark = (label: string) => (value: number) => {
console.debug(`${timeOffset()}: ${label}: ${value}`);
return value;
};
Promise.all([
async () => {
const test = 'getSettledOrThrowSync actually throws';
const duration = 10;
let invocationCount = 0;
const expensive = async () => {
const invocation = ++invocationCount;
await sleep(duration);
return invocation;
};
const kept = keep(expensive);
throws(kept.getSettledOrThrowSync, 'newly initialized should not contain any settled data');
await kept.getPending();
kept.purge();
throws(kept.getSettledOrThrowSync, 'following a purge, no settled data should be available');
},
async () => {
const test = 'test general cases';
const baseline = 100;
let invocationCount = 0;
const expensive = async () => {
const invocation = ++invocationCount;
const multiplier = invocation === 1 ? 2 : 1;
await sleep(baseline * multiplier);
return invocation;
};
const kept = keep(expensive);
const first = kept.getPending().then(mark(`${test} / first`));
kept.purge();
const second = kept.getPending().then(mark(`${test} / second`));
strictEqual(await first, 1);
strictEqual(await second, 2);
},
async () => {
const test = 'purge() after slow invocation';
const baseline = 100;
let invocationCount = 0;
const expensive = async () => {
const invocation = ++invocationCount;
const multiplier = invocation === 1 ? 2 : 1;
await sleep(baseline * multiplier);
return invocation;
};
const kept = keep(expensive);
// Settles after invocation2
const invocation1 = kept.getPending().then(mark(`${test} / getPending`));
// Clears the cache now _and_ after invocation1 settles
kept.purge();
// Settles before invocation1
const invocation2 = kept.getPending().then(mark(`${test} / getPending`));
await sleep(baseline);
// Hits the cached invocation2 before purge() destroys it
const cacheOf2 = kept.getPending().then(mark(`${test} / getPending`));
await sleep(baseline);
// Since invocation1 settles _after_ invocation2, the purge() will also
// clear the cached data from invocation2.
const invocation3 = kept.getPending().then(mark(`${test} / getPending`));
strictEqual(await invocation1, 1);
strictEqual(await invocation2, 2);
strictEqual(await cacheOf2, 2);
strictEqual(await invocation3, 3);
},
async () => {
const test = 'getSettled() not slowed down by pending';
const duration = 10;
let invocationCount = 0;
const expensive = async () => {
const invocation = ++invocationCount;
await sleep(duration);
return invocation;
};
const kept = keep(expensive);
kept.keepFresh(duration * 1);
await sleep(duration * 2);
const getSettled1 = kept.getSettled().then(mark(`${test} / getSettled1()`));
const getPending1 = kept.getPending().then(mark(`${test} / getPending1()`));
await sleep(duration * 3);
const getSettled2 = kept.getSettled().then(mark(`${test} / getSettled2()`));
const getPending2 = kept.getPending().then(mark(`${test} / getPending2()`));
strictEqual(await getSettled1, 1);
strictEqual(await getPending1, 1);
strictEqual(await getSettled2, 1);
strictEqual(await getPending2, 2);
},
].map((fn) => fn()));