test: add tasks queuing tests
[poolifier.git] / tests / pools / cluster / fixed.test.js
index b9c7d676d0404c7787e2fd8a00560e4167b88290..ddcca10568fe0ee5a82ae18c659a9d23ece43561 100644 (file)
@@ -12,6 +12,17 @@ describe('Fixed cluster pool test suite', () => {
       errorHandler: e => console.error(e)
     }
   )
+  const queuePool = new FixedClusterPool(
+    numberOfWorkers,
+    './tests/worker-files/cluster/testWorker.js',
+    {
+      enableTasksQueue: true,
+      tasksQueueOptions: {
+        concurrency: 2
+      },
+      errorHandler: e => console.error(e)
+    }
+  )
   const emptyPool = new FixedClusterPool(
     numberOfWorkers,
     './tests/worker-files/cluster/emptyWorker.js',
@@ -47,6 +58,7 @@ describe('Fixed cluster pool test suite', () => {
     await errorPool.destroy()
     await asyncErrorPool.destroy()
     await emptyPool.destroy()
+    await queuePool.destroy()
   })
 
   it('Verify that the function is executed in a worker cluster', async () => {
@@ -76,6 +88,31 @@ describe('Fixed cluster pool test suite', () => {
     expect(poolBusy).toBe(numberOfWorkers + 1)
   })
 
+  it('Verify that tasks queuing is working', async () => {
+    const maxMultiplier = 10
+    for (let i = 0; i < numberOfWorkers * maxMultiplier; i++) {
+      queuePool.execute()
+    }
+    for (const workerNode of queuePool.workerNodes) {
+      expect(workerNode.tasksUsage.running).toBeLessThanOrEqual(
+        queuePool.opts.tasksQueueOptions.concurrency
+      )
+      expect(workerNode.tasksUsage.run).toBe(0)
+      expect(workerNode.tasksQueue.length).toBeGreaterThan(0)
+    }
+    // FIXME: wait for ongoing tasks to be executed
+    const promises = []
+    for (let i = 0; i < numberOfWorkers * maxMultiplier; i++) {
+      promises.push(queuePool.execute())
+    }
+    await Promise.all(promises)
+    for (const workerNode of queuePool.workerNodes) {
+      expect(workerNode.tasksUsage.running).toBe(0)
+      expect(workerNode.tasksUsage.run).toBeGreaterThan(0)
+      expect(workerNode.tasksQueue.length).toBe(0)
+    }
+  })
+
   it('Verify that is possible to have a worker that return undefined', async () => {
     const result = await emptyPool.execute()
     expect(result).toBeUndefined()