Skip to content

Commit 6dec8c4

Browse files
committed
chore: improved test
1 parent 41c3e4f commit 6dec8c4

1 file changed

Lines changed: 17 additions & 8 deletions

File tree

Workerpool.test.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { proxy } from "https://cdn.skypack.dev/comlink?dts";
1+
import { proxy } from "comlink";
22
import { assertEquals } from "https://deno.land/std@0.155.0/testing/asserts.ts";
33
import { describe, it } from "https://deno.land/std@0.155.0/testing/bdd.ts";
44
import {
55
assertSpyCalls,
6-
stub,
6+
spy,
77
} from "https://deno.land/std@0.155.0/testing/mock.ts";
88
import { Class, SetOptional } from "type-fest";
99
import { Executable } from "./Executable.ts";
@@ -54,14 +54,21 @@ describe("Workerpool", () => {
5454
}
5555

5656
success?.(task, result);
57+
},
58+
onStateChange: (state) => {
59+
if (state !== "drained") return;
5760

58-
if (queue.length === 0) {
59-
resolve(pool);
61+
if (queue.length > 0) {
62+
throw new Error(`Drained with ${queue.length} tasks remaining.`);
6063
}
64+
65+
resolve(pool);
6166
},
6267
});
6368

64-
tasks.forEach((task) => pool.enqueue(task));
69+
for (const task of tasks) {
70+
pool.enqueue(task);
71+
}
6572

6673
pool.start();
6774
});
@@ -74,7 +81,7 @@ describe("Workerpool", () => {
7481
}
7582

7683
it("should process all tasks", async () => {
77-
const callback = stub({ callback: () => {} }, "callback");
84+
const callback = spy(() => {});
7885
await createMockQueue({
7986
concurrency: 2,
8087
workers: [workerA],
@@ -92,7 +99,7 @@ describe("Workerpool", () => {
9299
it("should swap workers when concurrency is reached", async () => {
93100
class workerB extends workerA {}
94101

95-
const callback = stub({ callback: () => {} }, "callback");
102+
const callback = spy(() => {});
96103
await createMockQueue({
97104
concurrency: 1,
98105
workers: [workerA, workerB],
@@ -121,11 +128,13 @@ describe("Workerpool", () => {
121128
}
122129

123130
const pool = await createMockQueue({
124-
concurrency: 1,
131+
concurrency: 2,
125132
workers: [workerC],
126133
tasks: [
127134
{ name: "workerC", payload: callback },
128135
{ name: "workerC", payload: callback },
136+
{ name: "workerC", payload: callback },
137+
{ name: "workerC", payload: callback },
129138
],
130139
});
131140

0 commit comments

Comments
 (0)