+import cluster from 'node:cluster'
+
import { expect } from 'expect'
-import { FixedClusterPool, PoolEvents } from '../../../lib/index.js'
-import { TaskFunctions } from '../../test-types.js'
-import { waitPoolEvents, waitWorkerEvents } from '../../test-utils.js'
-import { DEFAULT_TASK_NAME } from '../../../lib/utils.js'
+
+import { FixedClusterPool, PoolEvents } from '../../../lib/index.cjs'
+import { DEFAULT_TASK_NAME } from '../../../lib/utils.cjs'
+import { TaskFunctions } from '../../test-types.cjs'
+import { waitPoolEvents, waitWorkerEvents } from '../../test-utils.cjs'
describe('Fixed cluster pool test suite', () => {
const numberOfWorkers = 8
const tasksConcurrency = 2
const pool = new FixedClusterPool(
numberOfWorkers,
- './tests/worker-files/cluster/testWorker.js',
+ './tests/worker-files/cluster/testWorker.cjs',
{
- errorHandler: e => console.error(e)
+ errorHandler: e => console.error(e),
}
)
const queuePool = new FixedClusterPool(
numberOfWorkers,
- './tests/worker-files/cluster/testWorker.js',
+ './tests/worker-files/cluster/testWorker.cjs',
{
enableTasksQueue: true,
tasksQueueOptions: {
- concurrency: tasksConcurrency
+ concurrency: tasksConcurrency,
},
- errorHandler: e => console.error(e)
+ errorHandler: e => console.error(e),
}
)
const emptyPool = new FixedClusterPool(
numberOfWorkers,
- './tests/worker-files/cluster/emptyWorker.js',
+ './tests/worker-files/cluster/emptyWorker.cjs',
{ exitHandler: () => console.info('empty pool worker exited') }
)
const echoPool = new FixedClusterPool(
numberOfWorkers,
- './tests/worker-files/cluster/echoWorker.js'
+ './tests/worker-files/cluster/echoWorker.cjs'
)
const errorPool = new FixedClusterPool(
numberOfWorkers,
- './tests/worker-files/cluster/errorWorker.js',
+ './tests/worker-files/cluster/errorWorker.cjs',
{
- errorHandler: e => console.error(e)
+ errorHandler: e => console.error(e),
}
)
const asyncErrorPool = new FixedClusterPool(
numberOfWorkers,
- './tests/worker-files/cluster/asyncErrorWorker.js',
+ './tests/worker-files/cluster/asyncErrorWorker.cjs',
{
- errorHandler: e => console.error(e)
+ errorHandler: e => console.error(e),
}
)
const asyncPool = new FixedClusterPool(
numberOfWorkers,
- './tests/worker-files/cluster/asyncWorker.js'
+ './tests/worker-files/cluster/asyncWorker.cjs'
)
after('Destroy all pools', async () => {
it('Verify that the function is executed in a worker cluster', async () => {
let result = await pool.execute({
- function: TaskFunctions.fibonacci
+ function: TaskFunctions.fibonacci,
})
- expect(result).toBe(75025)
+ expect(result).toBe(354224848179262000000)
result = await pool.execute({
- function: TaskFunctions.factorial
+ function: TaskFunctions.factorial,
})
expect(result).toBe(9.33262154439441e157)
})
it("Verify that 'ready' event is emitted", async () => {
const pool = new FixedClusterPool(
numberOfWorkers,
- './tests/worker-files/cluster/testWorker.js',
+ './tests/worker-files/cluster/testWorker.cjs',
{
- errorHandler: e => console.error(e)
+ errorHandler: e => console.error(e),
}
)
expect(pool.emitter.eventNames()).toStrictEqual([])
expect(taskError).toStrictEqual({
name: DEFAULT_TASK_NAME,
message: 'Error Message from ClusterWorker',
- data
+ data,
})
expect(
errorPool.workerNodes.some(
taskError = e
})
expect(asyncErrorPool.emitter.eventNames()).toStrictEqual([
- PoolEvents.taskError
+ PoolEvents.taskError,
])
let inError
try {
expect(taskError).toStrictEqual({
name: DEFAULT_TASK_NAME,
message: 'Error Message from ClusterWorker:async',
- data
+ data,
})
expect(
asyncErrorPool.workerNodes.some(
pool.emitter.on(PoolEvents.destroy, () => ++poolDestroy)
expect(pool.emitter.eventNames()).toStrictEqual([
PoolEvents.busy,
- PoolEvents.destroy
+ PoolEvents.destroy,
])
await pool.destroy()
const numberOfExitEvents = await exitPromise
expect(pool.started).toBe(false)
- expect(pool.emitter.eventNames()).toStrictEqual([])
+ expect(pool.emitter.eventNames()).toStrictEqual([
+ PoolEvents.busy,
+ PoolEvents.destroy,
+ ])
expect(pool.readyEventEmitted).toBe(false)
expect(pool.workerNodes.length).toBe(0)
expect(numberOfExitEvents).toBe(numberOfWorkers)
})
it('Verify that cluster pool options are checked', async () => {
- const workerFilePath = './tests/worker-files/cluster/testWorker.js'
+ const workerFilePath = './tests/worker-files/cluster/testWorker.cjs'
let pool = new FixedClusterPool(numberOfWorkers, workerFilePath)
expect(pool.opts.env).toBeUndefined()
expect(pool.opts.settings).toBeUndefined()
+ expect(cluster.settings).toMatchObject({
+ exec: workerFilePath,
+ silent: false,
+ })
await pool.destroy()
pool = new FixedClusterPool(numberOfWorkers, workerFilePath, {
env: { TEST: 'test' },
- settings: { args: ['--use', 'http'], silent: true }
+ settings: { args: ['--use', 'http'], silent: true },
})
expect(pool.opts.env).toStrictEqual({ TEST: 'test' })
expect(pool.opts.settings).toStrictEqual({
args: ['--use', 'http'],
- silent: true
+ silent: true,
})
- expect({ ...pool.opts.settings, exec: workerFilePath }).toStrictEqual({
+ expect(cluster.settings).toMatchObject({
args: ['--use', 'http'],
silent: true,
- exec: workerFilePath
+ exec: workerFilePath,
})
await pool.destroy()
})
it('Should work even without opts in input', async () => {
- const workerFilePath = './tests/worker-files/cluster/testWorker.js'
+ const workerFilePath = './tests/worker-files/cluster/testWorker.cjs'
const pool = new FixedClusterPool(numberOfWorkers, workerFilePath)
const res = await pool.execute()
expect(res).toStrictEqual({ ok: 1 })
})
it('Verify destroyWorkerNode()', async () => {
- const workerFilePath = './tests/worker-files/cluster/testWorker.js'
+ const workerFilePath = './tests/worker-files/cluster/testWorker.cjs'
const pool = new FixedClusterPool(numberOfWorkers, workerFilePath)
const workerNodeKey = 0
let disconnectEvent = 0
await expect(pool.destroyWorkerNode(workerNodeKey)).resolves.toBeUndefined()
expect(disconnectEvent).toBe(1)
expect(exitEvent).toBe(1)
- expect(pool.workerNodes.length).toBe(numberOfWorkers - 1)
+ // Simulates an illegitimate worker node destroy and the minimum number of worker nodes is guaranteed
+ expect(pool.workerNodes.length).toBe(numberOfWorkers)
await pool.destroy()
})
it('Verify that a pool with zero worker fails', () => {
expect(
() =>
- new FixedClusterPool(0, './tests/worker-files/cluster/testWorker.js')
+ new FixedClusterPool(0, './tests/worker-files/cluster/testWorker.cjs')
).toThrow('Cannot instantiate a fixed pool with zero worker')
})
})