test: cleanup resources setup and teardown
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 31 Jul 2024 14:54:41 +0000 (16:54 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 31 Jul 2024 14:54:41 +0000 (16:54 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
tests/pools/selection-strategies/selection-strategies-utils.test.mjs
tests/pools/selection-strategies/weighted-round-robin-worker-choice-strategy.test.mjs
tests/pools/selection-strategies/worker-choice-strategies-context.test.mjs
tests/pools/worker-node.test.mjs

index 9b57e78945baac77c4c522f54147560552703579..f2223b8358e1f303e4a64f4fed9594c9c7953145 100644 (file)
@@ -7,19 +7,34 @@ import {
 } from '../../../lib/pools/selection-strategies/selection-strategies-utils.cjs'
 
 describe('Selection strategies utils test suite', () => {
-  it('Verify buildWorkerChoiceStrategyOptions() behavior', async () => {
-    const numberOfWorkers = 4
-    const pool = new FixedClusterPool(
+  const numberOfWorkers = 4
+  const numberOfThreads = 4
+  let clusterFixedPool, threadFixedPool
+
+  before('Create pools', () => {
+    clusterFixedPool = new FixedClusterPool(
       numberOfWorkers,
       './tests/worker-files/cluster/testWorker.cjs'
     )
-    expect(buildWorkerChoiceStrategyOptions(pool)).toStrictEqual({
+    threadFixedPool = new FixedThreadPool(
+      numberOfThreads,
+      './tests/worker-files/thread/testWorker.mjs'
+    )
+  })
+
+  after('Destroy pools', async () => {
+    await clusterFixedPool.destroy()
+    await threadFixedPool.destroy()
+  })
+
+  it('Verify buildWorkerChoiceStrategyOptions() behavior', async () => {
+    expect(buildWorkerChoiceStrategyOptions(clusterFixedPool)).toStrictEqual({
       runTime: { median: false },
       waitTime: { median: false },
       elu: { median: false },
       weights: expect.objectContaining({
         0: expect.any(Number),
-        [pool.info.maxSize - 1]: expect.any(Number),
+        [clusterFixedPool.info.maxSize - 1]: expect.any(Number),
       }),
     })
     const workerChoiceStrategyOptions = {
@@ -32,18 +47,17 @@ describe('Selection strategies utils test suite', () => {
       },
     }
     expect(
-      buildWorkerChoiceStrategyOptions(pool, workerChoiceStrategyOptions)
+      buildWorkerChoiceStrategyOptions(
+        clusterFixedPool,
+        workerChoiceStrategyOptions
+      )
     ).toStrictEqual(workerChoiceStrategyOptions)
-    await pool.destroy()
   })
 
   it('Verify getWorkerChoiceStrategyRetries() behavior', async () => {
-    const numberOfThreads = 4
-    const pool = new FixedThreadPool(
-      numberOfThreads,
-      './tests/worker-files/thread/testWorker.mjs'
+    expect(getWorkerChoiceStrategiesRetries(threadFixedPool)).toBe(
+      threadFixedPool.info.maxSize * 2
     )
-    expect(getWorkerChoiceStrategiesRetries(pool)).toBe(pool.info.maxSize * 2)
     const workerChoiceStrategyOptions = {
       runTime: { median: true },
       waitTime: { median: true },
@@ -54,11 +68,13 @@ describe('Selection strategies utils test suite', () => {
       },
     }
     expect(
-      getWorkerChoiceStrategiesRetries(pool, workerChoiceStrategyOptions)
+      getWorkerChoiceStrategiesRetries(
+        threadFixedPool,
+        workerChoiceStrategyOptions
+      )
     ).toBe(
-      pool.info.maxSize +
+      threadFixedPool.info.maxSize +
         Object.keys(workerChoiceStrategyOptions.weights).length
     )
-    await pool.destroy()
   })
 })
index b3ea806ef68ac2b4a57bed494209618ab449346a..fae2aecb1ad21c9195db468d342c370f6dadc385 100644 (file)
@@ -11,14 +11,14 @@ describe('Weighted round robin strategy worker choice strategy test suite', () =
   const max = 3
   let pool
 
-  before(() => {
+  before('Create pool', () => {
     pool = new FixedThreadPool(
       max,
       './tests/worker-files/thread/testWorker.mjs'
     )
   })
 
-  after(async () => {
+  after('Destroy pool', async () => {
     await pool.destroy()
   })
 
index e046af55d5a8eeebada837a3971c2cdac55b7ea6..427103a45c45b6429256e75fdd7376a61e24de70 100644 (file)
@@ -20,7 +20,7 @@ describe('Worker choice strategies context test suite', () => {
   const max = 3
   let fixedPool, dynamicPool
 
-  before(() => {
+  before('Create pools', () => {
     fixedPool = new FixedThreadPool(
       max,
       './tests/worker-files/thread/testWorker.mjs'
@@ -36,7 +36,7 @@ describe('Worker choice strategies context test suite', () => {
     restore()
   })
 
-  after(async () => {
+  after('Destroy pools', async () => {
     await fixedPool.destroy()
     await dynamicPool.destroy()
   })
index 886e01bf2c6fb959e9fb82bedeb2c72486a2be16..4fea0e437a9e12774b408ea8e420b09d37aceef9 100644 (file)
@@ -11,24 +11,33 @@ import { PriorityQueue } from '../../lib/queues/priority-queue.cjs'
 import { DEFAULT_TASK_NAME } from '../../lib/utils.cjs'
 
 describe('Worker node test suite', () => {
-  const threadWorkerNode = new WorkerNode(
-    WorkerTypes.thread,
-    './tests/worker-files/thread/testWorker.mjs',
-    {
-      tasksQueueBackPressureSize: 12,
-      tasksQueueBucketSize: 6,
-      tasksQueuePriority: true,
-    }
-  )
-  const clusterWorkerNode = new WorkerNode(
-    WorkerTypes.cluster,
-    './tests/worker-files/cluster/testWorker.cjs',
-    {
-      tasksQueueBackPressureSize: 12,
-      tasksQueueBucketSize: 6,
-      tasksQueuePriority: true,
-    }
-  )
+  let threadWorkerNode, clusterWorkerNode
+
+  before('Create worker nodes', () => {
+    threadWorkerNode = new WorkerNode(
+      WorkerTypes.thread,
+      './tests/worker-files/thread/testWorker.mjs',
+      {
+        tasksQueueBackPressureSize: 12,
+        tasksQueueBucketSize: 6,
+        tasksQueuePriority: true,
+      }
+    )
+    clusterWorkerNode = new WorkerNode(
+      WorkerTypes.cluster,
+      './tests/worker-files/cluster/testWorker.cjs',
+      {
+        tasksQueueBackPressureSize: 12,
+        tasksQueueBucketSize: 6,
+        tasksQueuePriority: true,
+      }
+    )
+  })
+
+  after('Terminate worker nodes', async () => {
+    await threadWorkerNode.terminate()
+    await clusterWorkerNode.terminate()
+  })
 
   it('Worker node instantiation', () => {
     expect(() => new WorkerNode()).toThrow(