/** @inheritDoc */
public choose (): number {
- // const freeWorkerNodeKey = this.findFreeWorkerNodeKey()
- // if (freeWorkerNodeKey !== -1) {
- // return freeWorkerNodeKey
- // }
let minRunTime = Infinity
let leastBusyWorkerNodeKey!: number
for (const [workerNodeKey, workerNode] of this.pool.workerNodes.entries()) {
)
const promises = new Set()
let poolFull = 0
- pool.emitter.on(PoolEvents.full, () => ++poolFull)
+ let poolInfo
+ pool.emitter.on(PoolEvents.full, info => {
+ ++poolFull
+ poolInfo = info
+ })
for (let i = 0; i < numberOfWorkers * 2; i++) {
promises.add(pool.execute())
}
// The `full` event is triggered when the number of submitted tasks at once reach the max number of workers in the dynamic pool.
// So in total numberOfWorkers * 2 times for a loop submitting up to numberOfWorkers * 2 tasks to the dynamic pool with min = max = numberOfWorkers.
expect(poolFull).toBe(numberOfWorkers * 2)
+ expect(poolInfo).toStrictEqual({
+ type: PoolTypes.dynamic,
+ worker: WorkerTypes.thread,
+ minSize: expect.any(Number),
+ maxSize: expect.any(Number),
+ workerNodes: expect.any(Number),
+ idleWorkerNodes: expect.any(Number),
+ busyWorkerNodes: expect.any(Number),
+ runningTasks: expect.any(Number),
+ queuedTasks: expect.any(Number),
+ maxQueuedTasks: expect.any(Number)
+ })
await pool.destroy()
})
)
const promises = new Set()
let poolBusy = 0
- pool.emitter.on(PoolEvents.busy, () => ++poolBusy)
+ let poolInfo
+ pool.emitter.on(PoolEvents.busy, info => {
+ ++poolBusy
+ poolInfo = info
+ })
for (let i = 0; i < numberOfWorkers * 2; i++) {
promises.add(pool.execute())
}
// The `busy` event is triggered when the number of submitted tasks at once reach the number of fixed pool workers.
// So in total numberOfWorkers + 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the fixed pool.
expect(poolBusy).toBe(numberOfWorkers + 1)
+ expect(poolInfo).toStrictEqual({
+ type: PoolTypes.fixed,
+ worker: WorkerTypes.thread,
+ minSize: expect.any(Number),
+ maxSize: expect.any(Number),
+ workerNodes: expect.any(Number),
+ idleWorkerNodes: expect.any(Number),
+ busyWorkerNodes: expect.any(Number),
+ runningTasks: expect.any(Number),
+ queuedTasks: expect.any(Number),
+ maxQueuedTasks: expect.any(Number)
+ })
await pool.destroy()
})
it('Verify that error handling is working properly:sync', async () => {
const data = { f: 10 }
+ let taskError
+ errorPool.emitter.on(PoolEvents.taskError, e => {
+ taskError = e
+ })
let inError
try {
await errorPool.execute(data)
expect(inError).toBeDefined()
expect(typeof inError === 'string').toBe(true)
expect(inError).toBe('Error Message from ClusterWorker')
+ expect(taskError).toStrictEqual({
+ error: 'Error Message from ClusterWorker',
+ errorData: data
+ })
expect(
errorPool.workerNodes.some(
workerNode => workerNode.tasksUsage.error === 1
it('Verify that error handling is working properly:async', async () => {
const data = { f: 10 }
+ // let taskError
+ // errorPool.emitter.on(PoolEvents.taskError, e => {
+ // taskError = e
+ // })
let inError
try {
await asyncErrorPool.execute(data)
expect(inError).toBeDefined()
expect(typeof inError === 'string').toBe(true)
expect(inError).toBe('Error Message from ClusterWorker:async')
+ // expect(taskError).toStrictEqual({
+ // error: 'Error Message from ClusterWorker:async',
+ // errorData: data
+ // })
expect(
asyncErrorPool.workerNodes.some(
workerNode => workerNode.tasksUsage.error === 1
it('Verify that error handling is working properly:sync', async () => {
const data = { f: 10 }
+ let taskError
+ errorPool.emitter.on(PoolEvents.taskError, e => {
+ taskError = e
+ })
let inError
try {
await errorPool.execute(data)
expect(inError.message).toBeDefined()
expect(typeof inError.message === 'string').toBe(true)
expect(inError.message).toBe('Error Message from ThreadWorker')
+ expect(taskError).toStrictEqual({
+ error: new Error('Error Message from ThreadWorker'),
+ errorData: data
+ })
expect(
errorPool.workerNodes.some(
workerNode => workerNode.tasksUsage.error === 1
it('Verify that error handling is working properly:async', async () => {
const data = { f: 10 }
+ // let taskError
+ // errorPool.emitter.on(PoolEvents.taskError, e => {
+ // taskError = e
+ // })
let inError
try {
await asyncErrorPool.execute(data)
expect(inError.message).toBeDefined()
expect(typeof inError.message === 'string').toBe(true)
expect(inError.message).toBe('Error Message from ThreadWorker:async')
+ // expect(taskError).toStrictEqual({
+ // error: new Error('Error Message from ThreadWorker:async'),
+ // errorData: data
+ // })
expect(
asyncErrorPool.workerNodes.some(
workerNode => workerNode.tasksUsage.error === 1