-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
17 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
|
@@ -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(); | ||
}); | ||
|
@@ -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], | ||
|
@@ -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], | ||
|
@@ -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 }, | ||
], | ||
}); | ||
|
||
|