refactor: cleanup interface scope
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Tue, 29 Aug 2023 19:00:31 +0000 (21:00 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Tue, 29 Aug 2023 19:00:31 +0000 (21:00 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/pools/abstract-pool.ts
src/pools/worker-node.ts
src/pools/worker.ts
src/utility-types.ts
tests/pools/abstract/worker-node.test.js

index 0d28cffe494a670eeab0425e0ff18b08f849a0fa..493c5ccc0e33cb20a7b9d41739e48f13ea50ee78 100644 (file)
@@ -655,13 +655,13 @@ export abstract class AbstractPool<
       this.checkValidTasksQueueOptions(tasksQueueOptions)
       this.opts.tasksQueueOptions =
         this.buildTasksQueueOptions(tasksQueueOptions)
-      this.setTasksQueueMaxSize(this.opts.tasksQueueOptions.size as number)
+      this.setTasksQueueSize(this.opts.tasksQueueOptions.size as number)
     } else if (this.opts.tasksQueueOptions != null) {
       delete this.opts.tasksQueueOptions
     }
   }
 
-  private setTasksQueueMaxSize (size: number): void {
+  private setTasksQueueSize (size: number): void {
     for (const workerNode of this.workerNodes) {
       workerNode.tasksQueueBackPressureSize = size
     }
index f9b4016c29b078143d6f56d9acfcaf24b06e688d..387d05f964270545b5b92f0e841768dea20a23df 100644 (file)
@@ -53,19 +53,7 @@ implements IWorkerNode<Worker, Data> {
    * @param tasksQueueBackPressureSize - The tasks queue back pressure size.
    */
   constructor (worker: Worker, tasksQueueBackPressureSize: number) {
-    if (worker == null) {
-      throw new TypeError('Cannot construct a worker node without a worker')
-    }
-    if (tasksQueueBackPressureSize == null) {
-      throw new TypeError(
-        'Cannot construct a worker node without a tasks queue back pressure size'
-      )
-    }
-    if (!Number.isSafeInteger(tasksQueueBackPressureSize)) {
-      throw new TypeError(
-        'Cannot construct a worker node with a tasks queue back pressure size that is not an integer'
-      )
-    }
+    this.checkWorkerNodeArguments(worker, tasksQueueBackPressureSize)
     this.worker = worker
     this.info = this.initWorkerInfo(worker)
     this.usage = this.initWorkerUsage()
@@ -270,4 +258,28 @@ implements IWorkerNode<Worker, Data> {
       }
     }
   }
+
+  private checkWorkerNodeArguments (
+    worker: Worker,
+    tasksQueueBackPressureSize: number
+  ): void {
+    if (worker == null) {
+      throw new TypeError('Cannot construct a worker node without a worker')
+    }
+    if (tasksQueueBackPressureSize == null) {
+      throw new TypeError(
+        'Cannot construct a worker node without a tasks queue back pressure size'
+      )
+    }
+    if (!Number.isSafeInteger(tasksQueueBackPressureSize)) {
+      throw new TypeError(
+        'Cannot construct a worker node with a tasks queue back pressure size that is not an integer'
+      )
+    }
+    if (tasksQueueBackPressureSize <= 0) {
+      throw new RangeError(
+        'Cannot construct a worker node with a tasks queue back pressure size that is not a positive integer'
+      )
+    }
+  }
 }
index be134f05f539b87bf254bbfb71e68c90e360946c..5cd58b28be9b2ee3ef44be54af4e2fb6bdcddca2 100644 (file)
@@ -132,7 +132,7 @@ export interface WorkerInfo {
   /**
    * Worker type.
    */
-  type: WorkerType
+  readonly type: WorkerType
   /**
    * Dynamic flag.
    */
@@ -199,6 +199,12 @@ export interface IWorker {
   readonly once: (event: 'exit', handler: ExitHandler<this>) => void
 }
 
+/**
+ * Worker node event callback.
+ *
+ * @param workerId - The worker id.
+ * @internal
+ */
 export type WorkerNodeEventCallback = (workerId: number) => void
 
 /**
@@ -220,7 +226,7 @@ export interface IWorkerNode<Worker extends IWorker, Data = unknown> {
   /**
    * Worker usage statistics.
    */
-  usage: WorkerUsage
+  readonly usage: WorkerUsage
   /**
    * Message channel (worker_threads only).
    */
@@ -232,14 +238,10 @@ export interface IWorkerNode<Worker extends IWorker, Data = unknown> {
   tasksQueueBackPressureSize: number
   /**
    * Callback invoked when worker node tasks queue is back pressured.
-   *
-   * @param workerId - The worker id.
    */
   onBackPressure?: WorkerNodeEventCallback
   /**
    * Callback invoked when worker node tasks queue is empty.
-   *
-   * @param workerId - The worker id.
    */
   onEmptyQueue?: WorkerNodeEventCallback
   /**
index 12067c077e06599339a0b01e0ebc51df42178713..e1fb311e45b4eb8ed0e5d33cda3b3806d7526f5b 100644 (file)
@@ -47,13 +47,19 @@ export interface TaskPerformance {
 }
 
 /**
- * Performance statistics computation.
+ * Worker task performance statistics computation settings.
  *
  * @internal
  */
 export interface WorkerStatistics {
-  runTime: boolean
-  elu: boolean
+  /**
+   * Whether the worker computes the task runtime or not.
+   */
+  readonly runTime: boolean
+  /**
+   * Whether the worker computes the task event loop utilization (ELU) or not.
+   */
+  readonly elu: boolean
 }
 
 /**
index df4e03fead8e71319bbab00178297d9e80ea9060..4b325f4303dbe2692cc8a0437e55180158df3a2b 100644 (file)
@@ -29,6 +29,21 @@ describe('Worker node test suite', () => {
         'Cannot construct a worker node with a tasks queue back pressure size that is not an integer'
       )
     )
+    expect(() => new WorkerNode(threadWorker, 0.2)).toThrowError(
+      new TypeError(
+        'Cannot construct a worker node with a tasks queue back pressure size that is not an integer'
+      )
+    )
+    expect(() => new WorkerNode(threadWorker, 0)).toThrowError(
+      new RangeError(
+        'Cannot construct a worker node with a tasks queue back pressure size that is not a positive integer'
+      )
+    )
+    expect(() => new WorkerNode(threadWorker, -1)).toThrowError(
+      new RangeError(
+        'Cannot construct a worker node with a tasks queue back pressure size that is not a positive integer'
+      )
+    )
     expect(threadWorkerNode).toBeInstanceOf(WorkerNode)
     expect(threadWorkerNode.worker).toBe(threadWorker)
     expect(threadWorkerNode.info).toStrictEqual({