test: add tasks queuing tests
[poolifier.git] / tests / pools / thread / fixed.test.js
index cf5c3175df42d9a5c94732e46d440af5a8f5deb1..7b4b9bf536acd131c3dcc1a9e729f90e86ff05e4 100644 (file)
@@ -12,6 +12,17 @@ describe('Fixed thread pool test suite', () => {
       errorHandler: e => console.error(e)
     }
   )
+  const queuePool = new FixedThreadPool(
+    numberOfThreads,
+    './tests/worker-files/thread/testWorker.js',
+    {
+      enableTasksQueue: true,
+      tasksQueueOptions: {
+        concurrency: 2
+      },
+      errorHandler: e => console.error(e)
+    }
+  )
   const emptyPool = new FixedThreadPool(
     numberOfThreads,
     './tests/worker-files/thread/emptyWorker.js',
@@ -47,6 +58,7 @@ describe('Fixed thread 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 thread', async () => {
@@ -76,6 +88,31 @@ describe('Fixed thread pool test suite', () => {
     expect(poolBusy).toBe(numberOfThreads + 1)
   })
 
+  it('Verify that tasks queuing is working', async () => {
+    const maxMultiplier = 10
+    for (let i = 0; i < numberOfThreads * 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 < numberOfThreads * 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()