}
// All workers are busy, create a new worker
- const worker = this.createAndSetupWorker()
- this.registerWorkerMessageListener<Data>(worker, message => {
- const tasksInProgress = this.tasks.get(worker)
- const isKillBehaviorOptionHard =
- message.kill === killBehaviorTypes.HARD
+ const workerCreated = this.createAndSetupWorker()
+ this.registerWorkerMessageListener<Data>(workerCreated, message => {
+ const tasksInProgress = this.tasks.get(workerCreated)
+ const isKillBehaviorOptionHard = message.kill === killBehaviorTypes.HARD
if (isKillBehaviorOptionHard || tasksInProgress === 0) {
// Kill received from the worker, means that no new tasks are submitted to that worker for a while ( > maxInactiveTime)
- this.sendToWorker(worker, { kill: 1 })
- void this.destroyWorker(worker)
+ this.sendToWorker(workerCreated, { kill: 1 })
+ void this.destroyWorker(workerCreated)
}
})
- return worker
+ return workerCreated
}
}
}
// All workers are busy, create a new worker
- const worker = this.createAndSetupWorker()
- this.registerWorkerMessageListener<Data>(worker, message => {
- const tasksInProgress = this.tasks.get(worker)
- const isKillBehaviorOptionHard =
- message.kill === killBehaviorTypes.HARD
+ const workerCreated = this.createAndSetupWorker()
+ this.registerWorkerMessageListener<Data>(workerCreated, message => {
+ const tasksInProgress = this.tasks.get(workerCreated)
+ const isKillBehaviorOptionHard = message.kill === killBehaviorTypes.HARD
if (isKillBehaviorOptionHard || tasksInProgress === 0) {
// Kill received from the worker, means that no new tasks are submitted to that worker for a while ( > maxInactiveTime)
- this.sendToWorker(worker, { kill: 1 })
- void this.destroyWorker(worker)
+ this.sendToWorker(workerCreated, { kill: 1 })
+ void this.destroyWorker(workerCreated)
}
})
- return worker
+ return workerCreated
}
}
import type { MessagePort } from 'worker_threads'
import type { MessageValue, KillBehavior } from '../utility-types'
import type { WorkerOptions } from './worker-options'
-// import { killBehaviorTypes } from './worker-options'
const defaultMaxInactiveTime = 1000 * 60
// TODO fix this and avoid that SOFT/HARD words are replicated so much times into the project
--- /dev/null
+const expect = require('expect')
+const { FixedThreadPool } = require('../../../lib/index')
+const expectedError = new Error('Worker could not be found in tasks map')
+
+class StubPoolWithTasksMapClear extends FixedThreadPool {
+ removeAllWorker () {
+ this.tasks.clear()
+ }
+}
+
+class StubPoolWithIsMainMethod extends FixedThreadPool {
+ isMain () {
+ return false
+ }
+}
+
+describe('Abstract pool test suite ', () => {
+ it('Simulate worker not found during increaseWorkersTask', () => {
+ const pool = new StubPoolWithTasksMapClear(
+ 1,
+ './tests/worker/cluster/testWorker.js',
+ {
+ errorHandler: e => console.error(e)
+ }
+ )
+ // simulate worker not found.
+ pool.removeAllWorker()
+ expect(() => pool.increaseWorkersTask()).toThrowError(expectedError)
+ })
+
+ it('Simulate worker not found during decreaseWorkersTasks', () => {
+ const pool = new StubPoolWithTasksMapClear(
+ 1,
+ './tests/worker/cluster/testWorker.js',
+ {
+ errorHandler: e => console.error(e)
+ }
+ )
+ // simulate worker not found.
+ pool.removeAllWorker()
+ expect(() => pool.decreaseWorkersTasks()).toThrowError(expectedError)
+ })
+
+ it('Simulate pool creation from a non main thread/process', () => {
+ expect(() => {
+ const pool = new StubPoolWithIsMainMethod(
+ 1,
+ './tests/worker/cluster/testWorker.js',
+ {
+ errorHandler: e => console.error(e)
+ }
+ )
+ }).toThrowError()
+ })
+})