refactor: refine circular buffer error message
[poolifier.git] / tests / pools / worker-node.test.mjs
index b77af2fda507fb5401758e479094495dddd805fb..54a69ca458a8566067c2c8af8160e6605214a6cf 100644 (file)
@@ -3,7 +3,7 @@ import { MessageChannel, Worker as ThreadWorker } from 'node:worker_threads'
 
 import { expect } from 'expect'
 
-import { CircularArray } from '../../lib/circular-array.cjs'
+import { CircularBuffer } from '../../lib/circular-buffer.cjs'
 import { WorkerTypes } from '../../lib/index.cjs'
 import { WorkerNode } from '../../lib/pools/worker-node.cjs'
 import { PriorityQueue } from '../../lib/priority-queue.cjs'
@@ -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({
@@ -127,7 +192,8 @@ describe('Worker node test suite', () => {
       type: WorkerTypes.thread,
       dynamic: false,
       ready: false,
-      stealing: false
+      stealing: false,
+      backPressure: false
     })
     expect(threadWorkerNode.usage).toStrictEqual({
       tasks: {
@@ -140,17 +206,17 @@ describe('Worker node test suite', () => {
         failed: 0
       },
       runTime: {
-        history: new CircularArray()
+        history: expect.any(CircularBuffer)
       },
       waitTime: {
-        history: new CircularArray()
+        history: expect.any(CircularBuffer)
       },
       elu: {
         idle: {
-          history: new CircularArray()
+          history: expect.any(CircularBuffer)
         },
         active: {
-          history: new CircularArray()
+          history: expect.any(CircularBuffer)
         }
       }
     })
@@ -158,10 +224,11 @@ describe('Worker node test suite', () => {
     expect(threadWorkerNode.tasksQueueBackPressureSize).toBe(12)
     expect(threadWorkerNode.tasksQueue).toBeInstanceOf(PriorityQueue)
     expect(threadWorkerNode.tasksQueue.size).toBe(0)
+    expect(threadWorkerNode.tasksQueue.bucketSize).toBe(6)
     expect(threadWorkerNode.tasksQueueSize()).toBe(
       threadWorkerNode.tasksQueue.size
     )
-    expect(threadWorkerNode.onBackPressureStarted).toBe(false)
+    expect(threadWorkerNode.setBackPressureFlag).toBe(false)
     expect(threadWorkerNode.taskFunctionsUsage).toBeInstanceOf(Map)
 
     expect(clusterWorkerNode).toBeInstanceOf(WorkerNode)
@@ -171,7 +238,8 @@ describe('Worker node test suite', () => {
       type: WorkerTypes.cluster,
       dynamic: false,
       ready: false,
-      stealing: false
+      stealing: false,
+      backPressure: false
     })
     expect(clusterWorkerNode.usage).toStrictEqual({
       tasks: {
@@ -184,17 +252,17 @@ describe('Worker node test suite', () => {
         failed: 0
       },
       runTime: {
-        history: new CircularArray()
+        history: expect.any(CircularBuffer)
       },
       waitTime: {
-        history: new CircularArray()
+        history: expect.any(CircularBuffer)
       },
       elu: {
         idle: {
-          history: new CircularArray()
+          history: expect.any(CircularBuffer)
         },
         active: {
-          history: new CircularArray()
+          history: expect.any(CircularBuffer)
         }
       }
     })
@@ -202,10 +270,11 @@ describe('Worker node test suite', () => {
     expect(clusterWorkerNode.tasksQueueBackPressureSize).toBe(12)
     expect(clusterWorkerNode.tasksQueue).toBeInstanceOf(PriorityQueue)
     expect(clusterWorkerNode.tasksQueue.size).toBe(0)
+    expect(clusterWorkerNode.tasksQueue.bucketSize).toBe(6)
     expect(clusterWorkerNode.tasksQueueSize()).toBe(
       clusterWorkerNode.tasksQueue.size
     )
-    expect(clusterWorkerNode.onBackPressureStarted).toBe(false)
+    expect(clusterWorkerNode.setBackPressureFlag).toBe(false)
     expect(clusterWorkerNode.taskFunctionsUsage).toBeInstanceOf(Map)
   })
 
@@ -245,17 +314,17 @@ describe('Worker node test suite', () => {
         failed: 0
       },
       runTime: {
-        history: new CircularArray()
+        history: expect.any(CircularBuffer)
       },
       waitTime: {
-        history: new CircularArray()
+        history: expect.any(CircularBuffer)
       },
       elu: {
         idle: {
-          history: new CircularArray()
+          history: expect.any(CircularBuffer)
         },
         active: {
-          history: new CircularArray()
+          history: expect.any(CircularBuffer)
         }
       }
     })
@@ -269,17 +338,17 @@ describe('Worker node test suite', () => {
         failed: 0
       },
       runTime: {
-        history: new CircularArray()
+        history: expect.any(CircularBuffer)
       },
       waitTime: {
-        history: new CircularArray()
+        history: expect.any(CircularBuffer)
       },
       elu: {
         idle: {
-          history: new CircularArray()
+          history: expect.any(CircularBuffer)
         },
         active: {
-          history: new CircularArray()
+          history: expect.any(CircularBuffer)
         }
       }
     })
@@ -293,17 +362,17 @@ describe('Worker node test suite', () => {
         failed: 0
       },
       runTime: {
-        history: new CircularArray()
+        history: expect.any(CircularBuffer)
       },
       waitTime: {
-        history: new CircularArray()
+        history: expect.any(CircularBuffer)
       },
       elu: {
         idle: {
-          history: new CircularArray()
+          history: expect.any(CircularBuffer)
         },
         active: {
-          history: new CircularArray()
+          history: expect.any(CircularBuffer)
         }
       }
     })