-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromisify.test.ts
118 lines (108 loc) · 3.09 KB
/
promisify.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
import { promisify } from "./promisify.ts";
import { assert } from "./deps_test.ts";
Deno.test("promisify()", async (t) => {
await t.step("without forceQueued", async (t) => {
await t.step("keep all results", async () => {
const [get, resolve] = promisify<number>();
const pending = get();
resolve(0);
resolve(1);
assert(await pending === 0);
assert(await get() === 1);
resolve(2);
assert(await get() === 2);
resolve(3);
resolve(4);
assert(await get() === 3);
assert(await get() === 4);
resolve(5);
resolve(6);
resolve(7);
assert(await get() === 5);
resolve(8);
assert(await get() === 6);
assert(await get() === 7);
assert(await get() === 8);
});
await t.step("keep nothing", async () => {
const [get, resolve] = promisify<number>({ maxQueued: 0 });
let pending = get();
resolve(1);
assert(await pending === 1);
resolve(2);
resolve(3);
pending = get();
resolve(4);
assert(await pending === 4);
// queueに何も残っていないので、未解決のまま止まり続けることになる
// assert(await get() === 5);
pending = get();
resolve(5);
resolve(6);
assert(await pending === 5);
});
await t.step("keep the latest result", async () => {
const [get, resolve] = promisify<number>({ maxQueued: 1 });
let pending = get();
resolve(1);
assert(await pending === 1);
resolve(2);
resolve(3);
assert(await get() === 3);
resolve(4);
resolve(5);
resolve(6);
assert(await get() === 6);
pending = get();
resolve(7);
resolve(8);
resolve(9);
assert(await pending === 7);
assert(await get() === 9);
});
await t.step("keep the latest and second latest results", async () => {
const [get, resolve] = promisify<number>({ maxQueued: 2 });
resolve(1);
let pending = get();
assert(await pending === 1);
resolve(2);
resolve(3);
assert(await get() === 2);
assert(await get() === 3);
resolve(4);
resolve(5);
resolve(6);
assert(await get() === 5);
resolve(7);
resolve(8);
assert(await get() === 7);
assert(await get() === 8);
pending = get();
resolve(9);
resolve(10);
resolve(11);
resolve(12);
assert(await pending === 9);
assert(await get() === 11);
assert(await get() === 12);
});
});
await t.step("with forceQueued", async (t) => {
await t.step("keep the latest result", async () => {
const [get, resolve] = promisify<number>({ maxQueued: 1 });
resolve(1);
const pending = get();
assert(await pending === 1);
resolve(2);
resolve(20, { forceQueued: true });
resolve(3);
resolve(4);
resolve(30, { forceQueued: true });
resolve(40, { forceQueued: true });
assert(await get() === 20);
assert(await get() === 30);
assert(await get() === 40);
assert(await get() === 4);
});
});
});