diff --git a/.yarn/versions/7ca11cf5.yml b/.yarn/versions/7ca11cf5.yml new file mode 100644 index 00000000..64d70248 --- /dev/null +++ b/.yarn/versions/7ca11cf5.yml @@ -0,0 +1,5 @@ +releases: + "@fast-check/worker": patch + +declined: + - "@fast-check/jest" diff --git a/packages/worker/src/internals/MainThreadRunner.ts b/packages/worker/src/internals/MainThreadRunner.ts index 6940d81a..99e87f15 100644 --- a/packages/worker/src/internals/MainThreadRunner.ts +++ b/packages/worker/src/internals/MainThreadRunner.ts @@ -1,7 +1,8 @@ import fc from 'fast-check'; import { type PropertyArbitraries, type WorkerProperty } from './SharedTypes.js'; -import { BasicPool, PooledWorker } from './worker-pool/BasicPool.js'; -import { Lock } from './worker-pool/Lock.js'; +import { BasicPool } from './worker-pool/BasicPool.js'; +import { Lock } from './lock/Lock.js'; +import { PooledWorker } from './worker-pool/IWorkerPool.js'; /** * Create a property able to run in the main thread and firing workers whenever required diff --git a/packages/worker/src/internals/worker-pool/Lock.ts b/packages/worker/src/internals/lock/Lock.ts similarity index 100% rename from packages/worker/src/internals/worker-pool/Lock.ts rename to packages/worker/src/internals/lock/Lock.ts diff --git a/packages/worker/src/internals/worker-pool/BasicPool.ts b/packages/worker/src/internals/worker-pool/BasicPool.ts index c24efea1..6a27c92c 100644 --- a/packages/worker/src/internals/worker-pool/BasicPool.ts +++ b/packages/worker/src/internals/worker-pool/BasicPool.ts @@ -1,17 +1,5 @@ import { Worker } from 'node:worker_threads'; - -export type OnSuccessCallback = (value: TSuccess) => void; -export type OnErrorCallback = (error: unknown) => void; - -/** - * Worker API - */ -export type PooledWorker = { - isAvailable: () => boolean; - isFaulty: () => boolean; - register: (payload: TPayload, onSuccess: OnSuccessCallback, onFailure: OnErrorCallback) => void; - terminateIfStillRunning: () => Promise; -}; +import type { OnErrorCallback, OnSuccessCallback, IWorkerPool, PooledWorker } from './IWorkerPool.js'; /** * Worker internal API @@ -37,7 +25,7 @@ export type WorkerToPoolMessage = { runId: number } & ( * Basic pool for workers, providing the ability to spawn new workers, * get the first available one and terminate them all */ -export class BasicPool { +export class BasicPool implements IWorkerPool { private readonly workers: InternalPooledWorker[] = []; /** @@ -47,9 +35,6 @@ export class BasicPool { */ constructor(private readonly workerFileUrl: URL, private readonly workerId: number) {} - /** - * Spawn a new instance of worker ready to handle new tasks - */ public async spawnNewWorker(): Promise> { let runIdInWorker = -1; let ready = false; @@ -161,16 +146,10 @@ export class BasicPool { return pooledWorker; } - /** - * Get the first available worker of the pool if any - */ public getFirstAvailableWorker(): PooledWorker | undefined { return this.workers.find((w) => w.isAvailable()); } - /** - * Terminate all registered workers and drop them definitely for the pool - */ public terminateAllWorkers(): Promise { const dropped = this.workers.splice(0, this.workers.length); // clear all workers return Promise.all(dropped.map((w) => w.worker.terminate())).then(() => undefined); diff --git a/packages/worker/src/internals/worker-pool/IWorkerPool.ts b/packages/worker/src/internals/worker-pool/IWorkerPool.ts new file mode 100644 index 00000000..301efb4b --- /dev/null +++ b/packages/worker/src/internals/worker-pool/IWorkerPool.ts @@ -0,0 +1,45 @@ +export type OnSuccessCallback = (value: TSuccess) => void; +export type OnErrorCallback = (error: unknown) => void; + +/** + * Worker API + */ +export type PooledWorker = { + isAvailable: () => boolean; + isFaulty: () => boolean; + register: (payload: TPayload, onSuccess: OnSuccessCallback, onFailure: OnErrorCallback) => void; + terminateIfStillRunning: () => Promise; +}; + +/** + * Message exchanged from the pool to the worker + */ +export type PoolToWorkerMessage = { runId: number; payload: TPayload }; + +/** + * Message exchanged from the worker to the pool + */ +export type WorkerToPoolMessage = { runId: number } & ( + | { success: true; output: TSuccess } + | { success: false; error: unknown } +); + +/** + * Worker pool interface + */ +export interface IWorkerPool { + /** + * Spawn a new instance of worker ready to handle new tasks + */ + spawnNewWorker(): Promise>; + + /** + * Get the first available worker of the pool if any + */ + getFirstAvailableWorker(): PooledWorker | undefined; + + /** + * Terminate all registered workers and drop them definitely for the pool + */ + terminateAllWorkers(): Promise; +} diff --git a/packages/worker/test/internals/worker-pool/Lock.spec.ts b/packages/worker/test/internals/lock/Lock.spec.ts similarity index 93% rename from packages/worker/test/internals/worker-pool/Lock.spec.ts rename to packages/worker/test/internals/lock/Lock.spec.ts index 4673c46e..30822bb3 100644 --- a/packages/worker/test/internals/worker-pool/Lock.spec.ts +++ b/packages/worker/test/internals/lock/Lock.spec.ts @@ -1,4 +1,4 @@ -import { AcquiredLock, Lock } from '../../../src/internals/worker-pool/Lock.js'; +import { AcquiredLock, Lock } from '../../../src/internals/lock/Lock.js'; describe('Lock', () => { it('should be able to take the first lock', async () => {