test: worker node tasks queue bucket initialization
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 8 May 2024 00:35:42 +0000 (02:35 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 8 May 2024 00:35:42 +0000 (02:35 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/pools/utils.ts
tests/pools/abstract-pool.test.mjs
tests/pools/worker-node.test.mjs

index b33726a69f3f72113061d8fb918983ef75cbfa59..b7b38ac4a874f279f316929a80514f306b60390a 100644 (file)
@@ -187,6 +187,21 @@ export const checkWorkerNodeArguments = (
       'Cannot construct a worker node with a tasks queue back pressure size option that is not a positive integer'
     )
   }
+  if (opts.tasksQueueBucketSize == null) {
+    throw new TypeError(
+      'Cannot construct a worker node without a tasks queue bucket size option'
+    )
+  }
+  if (!Number.isSafeInteger(opts.tasksQueueBucketSize)) {
+    throw new TypeError(
+      'Cannot construct a worker node with a tasks queue bucket size option that is not an integer'
+    )
+  }
+  if (opts.tasksQueueBucketSize <= 0) {
+    throw new RangeError(
+      'Cannot construct a worker node with a tasks queue bucket size option that is not a positive integer'
+    )
+  }
 }
 
 /**
index 3158166b655dec28e913536d3e56fa880acdbd7c..6a65f3803d0825b29052942873e13ff5edbe7460 100644 (file)
@@ -789,6 +789,7 @@ describe('Abstract pool test suite', () => {
       expect(workerNode.tasksQueue).toBeInstanceOf(PriorityQueue)
       expect(workerNode.tasksQueue.size).toBe(0)
       expect(workerNode.tasksQueue.maxSize).toBe(0)
+      expect(workerNode.tasksQueue.k).toBe(numberOfWorkers * 2)
     }
     await pool.destroy()
     pool = new DynamicThreadPool(
@@ -801,6 +802,7 @@ describe('Abstract pool test suite', () => {
       expect(workerNode.tasksQueue).toBeInstanceOf(PriorityQueue)
       expect(workerNode.tasksQueue.size).toBe(0)
       expect(workerNode.tasksQueue.maxSize).toBe(0)
+      expect(workerNode.tasksQueue.k).toBe(numberOfWorkers * 2)
     }
     await pool.destroy()
   })
index b77af2fda507fb5401758e479094495dddd805fb..d6339080b7b001862609ce1fe48366e9791f84f1 100644 (file)
@@ -13,12 +13,12 @@ describe('Worker node test suite', () => {
   const threadWorkerNode = new WorkerNode(
     WorkerTypes.thread,
     './tests/worker-files/thread/testWorker.mjs',
-    { tasksQueueBackPressureSize: 12 }
+    { tasksQueueBackPressureSize: 12, tasksQueueBucketSize: 6 }
   )
   const clusterWorkerNode = new WorkerNode(
     WorkerTypes.cluster,
     './tests/worker-files/cluster/testWorker.cjs',
-    { tasksQueueBackPressureSize: 12 }
+    { tasksQueueBackPressureSize: 12, tasksQueueBucketSize: 6 }
   )
 
   it('Worker node instantiation', () => {
@@ -120,6 +120,71 @@ describe('Worker node test suite', () => {
         'Cannot construct a worker node with a tasks queue back pressure size option that is not a positive integer'
       )
     )
+    expect(
+      () =>
+        new WorkerNode(
+          WorkerTypes.thread,
+          './tests/worker-files/thread/testWorker.mjs',
+          {
+            tasksQueueBackPressureSize: 12
+          }
+        )
+    ).toThrow(
+      new TypeError(
+        'Cannot construct a worker node without a tasks queue bucket size option'
+      )
+    )
+    expect(
+      () =>
+        new WorkerNode(
+          WorkerTypes.thread,
+          './tests/worker-files/thread/testWorker.mjs',
+          {
+            tasksQueueBackPressureSize: 12,
+            tasksQueueBucketSize: 'invalidTasksQueueBucketSize'
+          }
+        )
+    ).toThrow(
+      new TypeError(
+        'Cannot construct a worker node with a tasks queue bucket size option that is not an integer'
+      )
+    )
+    expect(
+      () =>
+        new WorkerNode(
+          WorkerTypes.thread,
+          './tests/worker-files/thread/testWorker.mjs',
+          { tasksQueueBackPressureSize: 12, tasksQueueBucketSize: 0.2 }
+        )
+    ).toThrow(
+      new TypeError(
+        'Cannot construct a worker node with a tasks queue bucket size option that is not an integer'
+      )
+    )
+    expect(
+      () =>
+        new WorkerNode(
+          WorkerTypes.thread,
+          './tests/worker-files/thread/testWorker.mjs',
+          { tasksQueueBackPressureSize: 12, tasksQueueBucketSize: 0 }
+        )
+    ).toThrow(
+      new RangeError(
+        'Cannot construct a worker node with a tasks queue bucket size option that is not a positive integer'
+      )
+    )
+    expect(
+      () =>
+        new WorkerNode(
+          WorkerTypes.thread,
+          './tests/worker-files/thread/testWorker.mjs',
+          { tasksQueueBackPressureSize: 12, tasksQueueBucketSize: -1 }
+        )
+    ).toThrow(
+      new RangeError(
+        'Cannot construct a worker node with a tasks queue bucket size option that is not a positive integer'
+      )
+    )
     expect(threadWorkerNode).toBeInstanceOf(WorkerNode)
     expect(threadWorkerNode.worker).toBeInstanceOf(ThreadWorker)
     expect(threadWorkerNode.info).toStrictEqual({
@@ -158,6 +223,7 @@ describe('Worker node test suite', () => {
     expect(threadWorkerNode.tasksQueueBackPressureSize).toBe(12)
     expect(threadWorkerNode.tasksQueue).toBeInstanceOf(PriorityQueue)
     expect(threadWorkerNode.tasksQueue.size).toBe(0)
+    expect(threadWorkerNode.tasksQueue.k).toBe(6)
     expect(threadWorkerNode.tasksQueueSize()).toBe(
       threadWorkerNode.tasksQueue.size
     )
@@ -202,6 +268,7 @@ describe('Worker node test suite', () => {
     expect(clusterWorkerNode.tasksQueueBackPressureSize).toBe(12)
     expect(clusterWorkerNode.tasksQueue).toBeInstanceOf(PriorityQueue)
     expect(clusterWorkerNode.tasksQueue.size).toBe(0)
+    expect(clusterWorkerNode.tasksQueue.k).toBe(6)
     expect(clusterWorkerNode.tasksQueueSize()).toBe(
       clusterWorkerNode.tasksQueue.size
     )