test: enhance tasks queueing tests
authorJérôme Benoit <jerome.benoit@sap.com>
Fri, 14 Apr 2023 16:40:57 +0000 (18:40 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Fri, 14 Apr 2023 16:40:57 +0000 (18:40 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
tests/pools/cluster/fixed.test.js
tests/pools/thread/fixed.test.js

index ddcca10568fe0ee5a82ae18c659a9d23ece43561..c28b8021db11f0342ba3130146cc0e54d2b66eb5 100644 (file)
@@ -90,9 +90,11 @@ describe('Fixed cluster pool test suite', () => {
 
   it('Verify that tasks queuing is working', async () => {
     const maxMultiplier = 10
+    const promises = new Set()
     for (let i = 0; i < numberOfWorkers * maxMultiplier; i++) {
-      queuePool.execute()
+      promises.add(queuePool.execute())
     }
+    expect(promises.size).toBe(numberOfWorkers * maxMultiplier)
     for (const workerNode of queuePool.workerNodes) {
       expect(workerNode.tasksUsage.running).toBeLessThanOrEqual(
         queuePool.opts.tasksQueueOptions.concurrency
@@ -100,17 +102,17 @@ describe('Fixed cluster pool test suite', () => {
       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())
-    }
+    expect(queuePool.numberOfRunningTasks).toBe(numberOfWorkers)
+    expect(queuePool.numberOfQueuedTasks).toBe(
+      numberOfWorkers * maxMultiplier - numberOfWorkers
+    )
     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)
     }
+    promises.clear()
   })
 
   it('Verify that is possible to have a worker that return undefined', async () => {
index 7b4b9bf536acd131c3dcc1a9e729f90e86ff05e4..75c4a935ab57c9e1ae1fd685a4b20183affbaf8c 100644 (file)
@@ -90,9 +90,11 @@ describe('Fixed thread pool test suite', () => {
 
   it('Verify that tasks queuing is working', async () => {
     const maxMultiplier = 10
+    const promises = new Set()
     for (let i = 0; i < numberOfThreads * maxMultiplier; i++) {
-      queuePool.execute()
+      promises.add(queuePool.execute())
     }
+    expect(promises.size).toBe(numberOfThreads * maxMultiplier)
     for (const workerNode of queuePool.workerNodes) {
       expect(workerNode.tasksUsage.running).toBeLessThanOrEqual(
         queuePool.opts.tasksQueueOptions.concurrency
@@ -100,17 +102,17 @@ describe('Fixed thread pool test suite', () => {
       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())
-    }
+    expect(queuePool.numberOfRunningTasks).toBe(numberOfThreads)
+    expect(queuePool.numberOfQueuedTasks).toBe(
+      numberOfThreads * maxMultiplier - numberOfThreads
+    )
     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)
     }
+    promises.clear()
   })
 
   it('Verify that is possible to have a worker that return undefined', async () => {