-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromise.test.ts
56 lines (52 loc) · 1.77 KB
/
promise.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
import { strict, throws } from './deps.test.ts';
import { keeper } from './promise.ts';
import { sleep } from './control.ts';
/**
* Mimic an expensive or time intensive function.
*/
const expensive = (duration = 10, state = 0) => async () => {
++state;
await sleep(duration);
return state;
};
Deno.test('keeper', async () => {
const kept = keeper(expensive());
// newly initialized should not contain any stale data
throws(kept.stale);
// since cache is stale, .get() will invoke the function
strict(await kept.get(), 1);
// result will be red from cache
strict(await kept.get(), 1);
// force a refresh
const secondInvocation = kept.fresh();
// read the stale cache while the refresh invocation resolves
strict(kept.stale(), 1);
// .get() will wait for the pending promise to resolve
strict(await kept.get(), 2);
// confirm the fresh promise is also the same
strict(await secondInvocation, 2);
// confirm the cache is updated
strict(kept.stale(), 2);
// stack two fresh() calls
const thirdInvocation = kept.fresh();
const fourthInvocation = kept.fresh();
// retrieve what is in the cache
strict(kept.stale(), 2);
// .get() will attach to the latest invocation
strict(await kept.get(), 4);
// the 3rd invocation will still resolve to 3
strict(await thirdInvocation, 3);
// and the 4th invocation resolves to 4
strict(await fourthInvocation, 4);
});
Deno.test('keeper + interval', async () => {
const kept = keeper(expensive());
strict(await kept.get(), 1);
kept.start(100);
strict(typeof kept.timer !== 'undefined' && kept.timer >= 0, true);
await sleep(400); // 300 would be exact, but trying to avoid premature test fails
strict(kept.stale() >= 3, true);
kept.stop();
await sleep(100);
strict(kept.stale() >= 3, true);
});