fix: ensure worker node cannot be instantiaed without proper arguments
[poolifier.git] / src / pools / worker-node.ts
index 812d14448f6fc4c7b221a74a23958920627f94ac..09e4a7b5cd8831360365a7a88e338e3b921eaf91 100644 (file)
@@ -28,7 +28,7 @@ implements IWorkerNode<Worker, Data> {
   public messageChannel?: MessageChannel
   /** @inheritdoc */
   public usage: WorkerUsage
-  private readonly tasksUsage: Map<string, WorkerUsage>
+  private readonly taskFunctionsUsage: Map<string, WorkerUsage>
   private readonly tasksQueue: Queue<Task<Data>>
   private readonly tasksQueueBackPressureSize: number
 
@@ -40,13 +40,29 @@ implements IWorkerNode<Worker, Data> {
    * @param poolMaxSize - The pool maximum size.
    */
   constructor (worker: Worker, workerType: WorkerType, poolMaxSize: number) {
+    if (worker == null) {
+      throw new Error('Cannot construct a worker node without a worker')
+    }
+    if (workerType == null) {
+      throw new Error('Cannot construct a worker node without a worker type')
+    }
+    if (poolMaxSize == null) {
+      throw new Error(
+        'Cannot construct a worker node without a pool maximum size'
+      )
+    }
+    if (isNaN(poolMaxSize)) {
+      throw new Error(
+        'Cannot construct a worker node with a NaN pool maximum size'
+      )
+    }
     this.worker = worker
     this.info = this.initWorkerInfo(worker, workerType)
     if (workerType === WorkerTypes.thread) {
       this.messageChannel = new MessageChannel()
     }
     this.usage = this.initWorkerUsage()
-    this.tasksUsage = new Map<string, WorkerUsage>()
+    this.taskFunctionsUsage = new Map<string, WorkerUsage>()
     this.tasksQueue = new Queue<Task<Data>>()
     this.tasksQueueBackPressureSize = Math.pow(poolMaxSize, 2)
   }
@@ -82,13 +98,13 @@ implements IWorkerNode<Worker, Data> {
 
   /** @inheritdoc */
   public hasBackPressure (): boolean {
-    return this.tasksQueueSize() >= this.tasksQueueBackPressureSize
+    return this.tasksQueue.size >= this.tasksQueueBackPressureSize
   }
 
   /** @inheritdoc */
   public resetUsage (): void {
     this.usage = this.initWorkerUsage()
-    this.tasksUsage.clear()
+    this.taskFunctionsUsage.clear()
   }
 
   /** @inheritdoc */
@@ -103,23 +119,27 @@ implements IWorkerNode<Worker, Data> {
   }
 
   /** @inheritdoc */
-  public getTaskWorkerUsage (name: string): WorkerUsage | undefined {
+  public getTaskFunctionWorkerUsage (name: string): WorkerUsage | undefined {
     if (!Array.isArray(this.info.taskFunctions)) {
       throw new Error(
-        `Cannot get task worker usage for task function name '${name}' when task function names list is not yet defined`
+        `Cannot get task function worker usage for task function name '${name}' when task function names list is not yet defined`
       )
     }
     if (
-      name === DEFAULT_TASK_NAME &&
       Array.isArray(this.info.taskFunctions) &&
-      this.info.taskFunctions.length > 1
+      this.info.taskFunctions.length < 3
     ) {
+      throw new Error(
+        `Cannot get task function worker usage for task function name '${name}' when task function names list has less than 3 elements`
+      )
+    }
+    if (name === DEFAULT_TASK_NAME) {
       name = this.info.taskFunctions[1]
     }
-    if (!this.tasksUsage.has(name)) {
-      this.tasksUsage.set(name, this.initTaskWorkerUsage(name))
+    if (!this.taskFunctionsUsage.has(name)) {
+      this.taskFunctionsUsage.set(name, this.initTaskFunctionWorkerUsage(name))
     }
-    return this.tasksUsage.get(name)
+    return this.taskFunctionsUsage.get(name)
   }
 
   private initWorkerInfo (worker: Worker, workerType: WorkerType): WorkerInfo {
@@ -167,7 +187,7 @@ implements IWorkerNode<Worker, Data> {
     }
   }
 
-  private initTaskWorkerUsage (name: string): WorkerUsage {
+  private initTaskFunctionWorkerUsage (name: string): WorkerUsage {
     const getTaskQueueSize = (): number => {
       let taskQueueSize = 0
       for (const task of this.tasksQueue) {