-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathqueue.test.ts
78 lines (71 loc) · 1.57 KB
/
queue.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
import createQueue from "./queue.ts";
import { assert, assertEquals, assertRejects } from "testing/asserts.ts";
Deno.test("queue", async () => {
const logs: string[] = [];
const queue = createQueue();
const returns = await Promise.all([
queue(async () => {
logs.push("enter a");
await wait(10);
logs.push("return a");
return "a";
}),
queue(async () => {
logs.push("enter b");
await wait(20);
logs.push("return b");
return "b";
}),
queue(() => {
logs.push("enter c");
logs.push("return c");
return "c";
}),
await queue(async () => {
logs.push("enter d");
await wait(10);
logs.push("return d");
return "d";
}),
]);
assertEquals(returns, ["a", "b", "c", "d"]);
assertEquals(logs, [
"enter a",
"return a",
"enter b",
"return b",
"enter c",
"return c",
"enter d",
"return d",
]);
});
Deno.test("queue (reject)", async () => {
const queue = createQueue();
assertRejects(() =>
queue(() => {
throw Error();
})
);
const a = queue(async () => {
await wait(10);
return 1;
});
assertRejects(() =>
queue(async () => {
await wait(10);
throw Error();
})
);
const c = queue(async () => {
await wait(10);
return 3;
});
const done = queue.done;
assert(done instanceof Promise);
assertEquals(await a, 1);
assertEquals(await c, 3);
assert(queue.done === true);
assertEquals(await done, undefined);
});
const wait = (ms: number) => new Promise((res) => setTimeout(res, ms));