-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorkerpool.test.ts
140 lines (123 loc) · 3.98 KB
/
Workerpool.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
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
import { describe, it } from "https://deno.land/[email protected]/testing/bdd.ts";
import {
assertSpyCalls,
spy,
} from "https://deno.land/[email protected]/testing/mock.ts";
import { Executable } from "./Executable.ts";
import { ExecutableWorker } from "./ExecutableWorker.ts";
import { Task } from "./Task.ts";
import { Workerpool } from "./Workerpool.ts";
import { type Class, comlink, type SetOptional } from "./deps.ts";
export type ArrowFunction = (...args: unknown[]) => unknown;
type MemoryMutexTask<TPayload> = Task<TPayload> & { active?: boolean };
type PreparePoolOptions<TPayload, TResult> = {
concurrency: number;
tasks: SetOptional<Task<TPayload>, "executionCount">[];
workers: Class<Executable<TPayload, TResult>>[];
};
describe("Workerpool", () => {
// In-memory FIFO awaiable queue.
const createMockQueue = async <TPayload, TResult = unknown>({
concurrency,
tasks,
workers,
}: PreparePoolOptions<TPayload, TResult>) =>
await new Promise<Workerpool<TPayload, TResult>>((resolve) => {
const queue: MemoryMutexTask<TPayload>[] = [];
const pool = new Workerpool<TPayload, TResult>({
concurrency,
workers,
enqueue: (task: MemoryMutexTask<TPayload>) => {
if (queue.includes(task)) {
task.active = false;
} else {
queue.push(task);
}
},
dequeue: () => {
const task = queue.find(({ active }) => !active);
if (task) {
task.active = true;
return task;
}
},
onTaskFinished: (_error, _result, { task }) => {
const index = queue.indexOf(task);
if (index > -1) {
queue.splice(index, 1);
}
},
onStateChange: (state) => {
if (state !== "drained") return;
if (queue.length > 0) {
throw new Error(`Drained with ${queue.length} tasks remaining.`);
}
resolve(pool);
},
});
for (const task of tasks) {
pool.enqueue(task);
}
pool.start();
});
class workerA implements Executable<ArrowFunction, void> {
async execute(cb?: ArrowFunction) {
await new Promise((resolve) => setTimeout(resolve, Math.random() * 200));
cb?.();
}
}
it("should process all tasks", async () => {
const callback = spy(() => {});
await createMockQueue({
concurrency: 2,
workers: [workerA],
tasks: [
{ name: "workerA", payload: callback },
{ name: "workerA", payload: callback },
{ name: "workerA", payload: callback },
{ name: "workerA", payload: callback },
],
});
assertSpyCalls(callback, 4);
});
it("should swap workers when concurrency is reached", async () => {
class workerB extends workerA {}
const callback = spy(() => {});
await createMockQueue({
concurrency: 1,
workers: [workerA, workerB],
tasks: [
{ name: "workerA", payload: callback },
{ name: "workerB", payload: callback },
{ name: "workerA", payload: callback },
{ name: "workerB", payload: callback },
],
});
assertSpyCalls(callback, 4);
});
// Temporarily ignored, see https://github.com/GoogleChromeLabs/comlink/issues/598
it.ignore("should support web workers", async () => {
let counter = 0;
const callback = comlink.proxy(() => {
counter++;
});
class workerC extends ExecutableWorker<ArrowFunction> {
constructor() {
super(new URL("./__test__/example-worker.ts", import.meta.url).href);
}
}
const pool = await createMockQueue({
concurrency: 2,
workers: [workerC],
tasks: [
{ name: "workerC", payload: callback },
{ name: "workerC", payload: callback },
{ name: "workerC", payload: callback },
{ name: "workerC", payload: callback },
],
});
pool.pause();
assertEquals(counter, 2);
});
});