Factor out some UTs code in test-utils.js
authorJérôme Benoit <jerome.benoit@sap.com>
Sat, 8 Oct 2022 12:51:04 +0000 (14:51 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sat, 8 Oct 2022 12:51:04 +0000 (14:51 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
tests/pools/selection-strategies/selection-strategies.test.js [moved from tests/pools/selection-strategies.test.js with 99% similarity]
tests/test-utils.js
tests/worker-files/cluster/asyncErrorWorker.js
tests/worker-files/thread/asyncErrorWorker.js
tests/worker-files/thread/errorWorker.js

similarity index 99%
rename from tests/pools/selection-strategies.test.js
rename to tests/pools/selection-strategies/selection-strategies.test.js
index c554ab14f1304f0805ef865798d299e1fc7426bf..6448c667dee24cb3420c6744303a4db1eb5c65d1 100644 (file)
@@ -3,7 +3,7 @@ const {
   WorkerChoiceStrategies,
   DynamicThreadPool,
   FixedThreadPool
-} = require('../../lib/index')
+} = require('../../../lib/index')
 
 describe('Selection strategies test suite', () => {
   it('Verify that WorkerChoiceStrategies enumeration provides string values', () => {
index a34cd78886584cdad6866bbb7ac470dba7eb1622..ab57685eb58fc000099fe4beec1287c01fcff14a 100644 (file)
@@ -17,9 +17,20 @@ class TestUtils {
     return new Promise(resolve => setTimeout(resolve, ms))
   }
 
-  static async workerSleepFunction (data, ms) {
-    return new Promise(resolve => {
-      setTimeout(() => resolve(data), ms)
+  static async workerSleepFunction (
+    data,
+    ms,
+    rejection = false,
+    rejectionMessage = ''
+  ) {
+    return new Promise((resolve, reject) => {
+      setTimeout(
+        () =>
+          rejection === true
+            ? reject(new Error(rejectionMessage))
+            : resolve(data),
+        ms
+      )
     })
   }
 
index b9fd9016ab91801122e4cb6394b5e590377b4f3f..0b6f5d8acb937a02bc4dcb47d855f6a89456ddc2 100644 (file)
@@ -1,13 +1,14 @@
 'use strict'
 const { ClusterWorker, KillBehaviors } = require('../../../lib/index')
+const TestUtils = require('../../test-utils')
 
 async function error (data) {
-  return new Promise((resolve, reject) => {
-    setTimeout(
-      () => reject(new Error('Error Message from ClusterWorker:async')),
-      2000
-    )
-  })
+  return TestUtils.workerSleepFunction(
+    data,
+    2000,
+    true,
+    'Error Message from ClusterWorker:async'
+  )
 }
 
 module.exports = new ClusterWorker(error, {
index d973f0a4d7625e826c19ad550c1f61a6f7836217..39f4055d771d7896c3d0cdf8d490af0d8baca64f 100644 (file)
@@ -1,10 +1,14 @@
 'use strict'
 const { ThreadWorker, KillBehaviors } = require('../../../lib/index')
+const TestUtils = require('../../test-utils')
 
 async function error (data) {
-  return new Promise((resolve, reject) => {
-    setTimeout(() => reject(new Error(data)), 2000)
-  })
+  return TestUtils.workerSleepFunction(
+    data,
+    2000,
+    true,
+    'Error Message from ThreadWorker:async'
+  )
 }
 
 module.exports = new ThreadWorker(error, {
index cc13c050ca8cd07d485a06e1faa1ba2239c1540f..841af907f1b2f6c290282c91adfc6afec5ae4ed4 100644 (file)
@@ -2,7 +2,7 @@
 const { ThreadWorker, KillBehaviors } = require('../../../lib/index')
 
 function error (data) {
-  throw new Error(data)
+  throw new Error('Error Message from ThreadWorker')
 }
 
 module.exports = new ThreadWorker(error, {