Skip to content

Commit

Permalink
chore: improved test
Browse files Browse the repository at this point in the history
  • Loading branch information
vicary committed Sep 14, 2022
1 parent 41c3e4f commit 6dec8c4
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions Workerpool.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { proxy } from "https://cdn.skypack.dev/comlink?dts";
import { proxy } from "comlink";
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,
stub,
spy,
} from "https://deno.land/[email protected]/testing/mock.ts";
import { Class, SetOptional } from "type-fest";
import { Executable } from "./Executable.ts";
Expand Down Expand Up @@ -54,14 +54,21 @@ describe("Workerpool", () => {
}

success?.(task, result);
},
onStateChange: (state) => {
if (state !== "drained") return;

if (queue.length === 0) {
resolve(pool);
if (queue.length > 0) {
throw new Error(`Drained with ${queue.length} tasks remaining.`);
}

resolve(pool);
},
});

tasks.forEach((task) => pool.enqueue(task));
for (const task of tasks) {
pool.enqueue(task);
}

pool.start();
});
Expand All @@ -74,7 +81,7 @@ describe("Workerpool", () => {
}

it("should process all tasks", async () => {
const callback = stub({ callback: () => {} }, "callback");
const callback = spy(() => {});
await createMockQueue({
concurrency: 2,
workers: [workerA],
Expand All @@ -92,7 +99,7 @@ describe("Workerpool", () => {
it("should swap workers when concurrency is reached", async () => {
class workerB extends workerA {}

const callback = stub({ callback: () => {} }, "callback");
const callback = spy(() => {});
await createMockQueue({
concurrency: 1,
workers: [workerA, workerB],
Expand Down Expand Up @@ -121,11 +128,13 @@ describe("Workerpool", () => {
}

const pool = await createMockQueue({
concurrency: 1,
concurrency: 2,
workers: [workerC],
tasks: [
{ name: "workerC", payload: callback },
{ name: "workerC", payload: callback },
{ name: "workerC", payload: callback },
{ name: "workerC", payload: callback },
],
});

Expand Down

0 comments on commit 6dec8c4

Please sign in to comment.