fix: test for worker file existence
authorJérôme Benoit <jerome.benoit@sap.com>
Mon, 10 Jul 2023 17:11:45 +0000 (19:11 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Mon, 10 Jul 2023 17:11:45 +0000 (19:11 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
rollup.config.mjs
src/pools/abstract-pool.ts
src/pools/selection-strategies/interleaved-weighted-round-robin-worker-choice-strategy.ts
src/worker/abstract-worker.ts
tests/pools/abstract/abstract-pool.test.js
tests/pools/selection-strategies/selection-strategies.test.js

index 1410f161af83887ede02a8f98eb6bdcec7d35738..d88f9734de2f431f89c2a79ccc5038b88ee17f98 100644 (file)
@@ -48,6 +48,7 @@ export default {
     'node:cluster',
     'node:crypto',
     'node:events',
+    'node:fs',
     'node:os',
     'node:perf_hooks',
     'node:worker_threads'
index 27d8af0f72713d6a5e224594f221d2f1b9591eec..f1392edd5124e25421531fec9958a2fea610bf59 100644 (file)
@@ -1,5 +1,6 @@
 import { randomUUID } from 'node:crypto'
 import { performance } from 'node:perf_hooks'
+import { existsSync } from 'node:fs'
 import type {
   MessageValue,
   PromiseResponseWrapper,
@@ -137,10 +138,14 @@ export abstract class AbstractPool<
   private checkFilePath (filePath: string): void {
     if (
       filePath == null ||
+      typeof filePath !== 'string' ||
       (typeof filePath === 'string' && filePath.trim().length === 0)
     ) {
       throw new Error('Please specify a file with a worker implementation')
     }
+    if (!existsSync(filePath)) {
+      throw new Error(`Cannot find the worker file '${filePath}'`)
+    }
   }
 
   private checkNumberOfWorkers (numberOfWorkers: number): void {
index 78960f1063c81f1c616d8fb9b868061cdc6598c5..907bd911d6e607966bfddc2cd02087c11cdab44f 100644 (file)
@@ -80,6 +80,7 @@ export class InterleavedWeightedRoundRobinWorkerChoiceStrategy<
       ) {
         const workerWeight =
           this.opts.weights?.[workerNodeKey] ?? this.defaultWorkerWeight
+        // if (this.isWorkerNodeReady(workerNodeKey) && workerWeight >= this.roundWeights[roundIndex]) {
         if (workerWeight >= this.roundWeights[roundIndex]) {
           roundId = roundIndex
           workerNodeId = workerNodeKey
index 93a36bad382fe5781f35d73b71a8169510b818ac..a6959a312fd7c8c8feb93b9bf6885a8581f83d7c 100644 (file)
@@ -311,7 +311,6 @@ export abstract class AbstractWorker<
       this.checkAlive.bind(this),
       (this.opts.maxInactiveTime ?? DEFAULT_MAX_INACTIVE_TIME) / 2
     )
-    this.checkAlive.bind(this)()
   }
 
   /**
index 05e4c34ea094023d61db4f0584c022af645e8c5e..c4fd6c65bcac7dac4b7e65a445b0600b354ee787 100644 (file)
@@ -5,8 +5,8 @@ const {
   FixedClusterPool,
   FixedThreadPool,
   PoolEvents,
-  WorkerChoiceStrategies,
   PoolTypes,
+  WorkerChoiceStrategies,
   WorkerTypes
 } = require('../../../lib')
 const { CircularArray } = require('../../../lib/circular-array')
@@ -45,6 +45,15 @@ describe('Abstract pool test suite', () => {
     expect(() => new FixedThreadPool(numberOfWorkers, '')).toThrowError(
       expectedError
     )
+    expect(() => new FixedThreadPool(numberOfWorkers, 0)).toThrowError(
+      expectedError
+    )
+    expect(() => new FixedThreadPool(numberOfWorkers, true)).toThrowError(
+      expectedError
+    )
+    expect(
+      () => new FixedThreadPool(numberOfWorkers, './dummyWorker.ts')
+    ).toThrowError(new Error("Cannot find the worker file './dummyWorker.ts'"))
   })
 
   it('Verify that numberOfWorkers is checked', () => {
index c3a01da6c19ff81c1aa4e60d46082772bd7cdcbc..6671b57781b1f51c15bdf0717f3c96cafcf0ca76 100644 (file)
@@ -1,9 +1,9 @@
 const { expect } = require('expect')
 const {
-  WorkerChoiceStrategies,
   DynamicThreadPool,
+  FixedClusterPool,
   FixedThreadPool,
-  FixedClusterPool
+  WorkerChoiceStrategies
 } = require('../../../lib')
 const { CircularArray } = require('../../../lib/circular-array')