--- /dev/null
+{
+ "check-coverage": true,
+ "lines": 90,
+ "statements": 90,
+ "functions": 89,
+ "branches": 85
+}
For cluster and thread pools, you can now only send and receive serializable `JSON` data.
_This is not a limitation by poolifier but NodeJS._
-#### Public properties renaming
+### Public methods removed
-- Thread Pool's `numWorkers` is now `numberOfWorkers`
-- Thread Pool's `nextWorker` is now `nextWorkerIndex`
+`numWorkers` method removed
+`nextWorker` method removed
#### Internal (protected) properties and methods renaming
type: 'conjunction'
})
-// wait some seconds before start, my pools need to load threads !!!
+// Wait some seconds before start, pools need to load threads !!!
setTimeout(async () => {
test()
}, 3000)
async function test () {
- // add tests
+ // Add tests
suite
.add('Pioardi:Static:ThreadPool', async function () {
await fixedThreadTest()
.add('Pioardi:Dynamic:ClusterPool', async function () {
await dynamicClusterTest()
})
- // add listeners
+ // Add listeners
.on('cycle', function (event) {
console.log(event.target.toString())
})
"test:debug": "npm run build && mocha -r source-map-support/register --inspect --exit --timeout 20000 'tests/**/*.test.js'",
"test:prod": "npm run build:prod && nyc mocha -r source-map-support/register --exit --timeout 20000 'tests/**/*.test.js'",
"sonar": "sonar-scanner",
- "coverage": "nyc report --reporter=lcov --check-coverage --lines 80",
- "coverage:html": "nyc report --reporter=html --check-coverage --lines 80",
+ "coverage": "nyc report --reporter=lcov",
+ "coverage:html": "nyc report --reporter=html",
"format": "prettier --loglevel silent --write .; prettierx --write .",
"lint": "eslint .",
"lint:fix": "eslint . --fix"
this.emitter = new PoolEmitter()
}
- /**
- * Number of workers that this pool should manage.
- *
- * @returns Number of workers that this pool manages.
- * @deprecated Only here for backward compatibility.
- */
- // eslint-disable-next-line spellcheck/spell-checker
- public get numWorkers (): number {
- return this.numberOfWorkers
- }
-
- /**
- * Index for the next worker.
- *
- * @returns Index for the next worker.
- * @deprecated Only here for backward compatibility.
- */
- public get nextWorker (): number {
- return this.nextWorkerIndex
- }
-
/**
* Perform the task specified in the constructor with the data parameter.
*
const expect = require('expect')
const { DynamicClusterPool } = require('../../../lib/index')
+const TestUtils = require('../../test-utils')
const min = 1
const max = 3
const pool = new DynamicClusterPool(
it('Verify that new workers are created when required, max size is not exceeded and that after a while new workers will die', async () => {
const promises = []
- let closedWorkers = 0
let fullPool = 0
pool.emitter.on('FullPool', () => fullPool++)
for (let i = 0; i < max * 2; i++) {
}
expect(pool.workers.length).toBeLessThanOrEqual(max)
expect(pool.workers.length).toBeGreaterThan(min)
- pool.workers.forEach(w => {
- w.on('exit', () => {
- closedWorkers++
- })
- })
expect(fullPool > 1).toBeTruthy()
- await new Promise(resolve => setTimeout(resolve, 5000))
- expect(closedWorkers).toBe(max - min)
+ const numberOfExitEvents = await TestUtils.waitExits(pool, max - min)
+ expect(numberOfExitEvents).toBe(max - min)
})
it('Verify scale worker up and down is working', async () => {
pool.execute({ test: 'test' })
}
expect(pool.workers.length).toBeGreaterThan(min)
- await new Promise(resolve => setTimeout(resolve, 3000))
+ await TestUtils.waitExits(pool, max - min)
expect(pool.workers.length).toBe(min)
for (let i = 0; i < max * 10; i++) {
pool.execute({ test: 'test' })
}
expect(pool.workers.length).toBeGreaterThan(min)
- await new Promise(resolve => setTimeout(resolve, 3000))
+ await TestUtils.waitExits(pool, max - min)
expect(pool.workers.length).toBe(min)
})
- it('Shutdown test', async () => {
- let closedWorkers = 0
- pool.workers.forEach(w => {
- w.on('exit', () => {
- closedWorkers++
- })
- })
- pool.destroy()
- await new Promise(resolve => setTimeout(resolve, 2000))
- expect(closedWorkers).toBe(min)
- })
- it('Validations test', () => {
- let error
- try {
- const pool1 = new DynamicClusterPool()
- console.log(pool1)
- } catch (e) {
- error = e
- }
- expect(error).toBeTruthy()
- expect(error.message).toBeTruthy()
+ it('Shutdown test', async () => {
+ const exitPromise = TestUtils.waitExits(pool, min)
+ await pool.destroy()
+ const res = await exitPromise
+ expect(res).toBe(min)
})
it('Should work even without opts in input', async () => {
const longRunningPool = new DynamicClusterPool(
min,
max,
- './tests/worker/cluster/longRunningWorkerHardBehavior.js'
+ './tests/worker-files/cluster/longRunningWorkerHardBehavior.js'
)
expect(longRunningPool.workers.length).toBe(min)
for (let i = 0; i < max * 10; i++) {
longRunningPool.execute({ test: 'test' })
}
expect(longRunningPool.workers.length).toBe(max)
- await new Promise(resolve => setTimeout(resolve, 3000))
+ await TestUtils.waitExits(longRunningPool, max - min)
// Here we expect the workers to be at the max size since that the task is still running
expect(longRunningPool.workers.length).toBe(min)
})
const longRunningPool = new DynamicClusterPool(
min,
max,
- './tests/worker/cluster/longRunningWorkerSoftBehavior.js'
+ './tests/worker-files/cluster/longRunningWorkerSoftBehavior.js'
)
expect(longRunningPool.workers.length).toBe(min)
for (let i = 0; i < max * 10; i++) {
longRunningPool.execute({ test: 'test' })
}
expect(longRunningPool.workers.length).toBe(max)
- await new Promise(resolve => setTimeout(resolve, 3000))
+ await TestUtils.sleep(1500)
// Here we expect the workers to be at the max size since that the task is still running
expect(longRunningPool.workers.length).toBe(max)
})
const expect = require('expect')
const { FixedClusterPool } = require('../../../lib/index')
+const TestUtils = require('../../test-utils')
const numberOfWorkers = 10
const maxTasks = 500
const pool = new FixedClusterPool(
})
it('Shutdown test', async () => {
- let closedWorkers = 0
- pool.workers.forEach(w => {
- w.on('exit', () => {
- closedWorkers++
- })
- })
+ const exitPromise = TestUtils.waitExits(pool, numberOfWorkers)
await pool.destroy()
- await new Promise(resolve => setTimeout(resolve, 500))
- expect(closedWorkers).toBe(numberOfWorkers)
- })
-
- it('Validations test', () => {
- let error
- try {
- const pool1 = new FixedClusterPool()
- console.log(pool1)
- } catch (e) {
- error = e
- }
- expect(error).toBeTruthy()
- expect(error.message).toBeTruthy()
+ const res = await exitPromise
+ expect(res).toBe(numberOfWorkers)
})
it('Should work even without opts in input', async () => {
const expect = require('expect')
const { DynamicThreadPool } = require('../../../lib/index')
+const TestUtils = require('../../test-utils')
const min = 1
const max = 3
const pool = new DynamicThreadPool(
it('Verify that new workers are created when required, max size is not exceeded and that after a while new workers will die', async () => {
const promises = []
- let closedThreads = 0
let fullPool = 0
pool.emitter.on('FullPool', () => fullPool++)
for (let i = 0; i < max * 2; i++) {
promises.push(pool.execute({ test: 'test' }))
}
expect(pool.workers.length).toBe(max)
- pool.workers.forEach(w => {
- w.on('exit', () => {
- closedThreads++
- })
- })
expect(fullPool > 1).toBeTruthy()
- await new Promise(resolve => setTimeout(resolve, 2000))
- expect(closedThreads).toBe(max - min)
+ const res = await TestUtils.waitExits(pool, max - min)
+ expect(res).toBe(max - min)
})
it('Verify scale thread up and down is working', async () => {
pool.execute({ test: 'test' })
}
expect(pool.workers.length).toBe(max)
- await new Promise(resolve => setTimeout(resolve, 1000))
+ await TestUtils.waitExits(pool, max - min)
expect(pool.workers.length).toBe(min)
for (let i = 0; i < max * 10; i++) {
pool.execute({ test: 'test' })
}
expect(pool.workers.length).toBe(max)
- await new Promise(resolve => setTimeout(resolve, 1500))
+ await TestUtils.waitExits(pool, max - min)
expect(pool.workers.length).toBe(min)
})
})
it('Validations test', () => {
- let error
- try {
+ expect(() => {
const pool1 = new DynamicThreadPool()
- console.log(pool1)
- } catch (e) {
- error = e
- }
- expect(error).toBeTruthy()
- expect(error.message).toBeTruthy()
+ }).toThrowError(
+ new Error('Please specify a file with a worker implementation')
+ )
})
it('Should work even without opts in input', async () => {
const longRunningPool = new DynamicThreadPool(
min,
max,
- './tests/worker/thread/longRunningWorkerHardBehavior.js',
+ './tests/worker-files/thread/longRunningWorkerHardBehavior.js',
{
errorHandler: e => console.error(e),
onlineHandler: () => console.log('worker is online')
longRunningPool.execute({ test: 'test' })
}
expect(longRunningPool.workers.length).toBe(max)
- await new Promise(resolve => setTimeout(resolve, 1500))
+ await TestUtils.waitExits(longRunningPool, max - min)
expect(longRunningPool.workers.length).toBe(min)
})
const longRunningPool = new DynamicThreadPool(
min,
max,
- './tests/worker/thread/longRunningWorkerSoftBehavior.js',
+ './tests/worker-files/thread/longRunningWorkerSoftBehavior.js',
{
errorHandler: e => console.error(e),
onlineHandler: () => console.log('worker is online')
longRunningPool.execute({ test: 'test' })
}
expect(longRunningPool.workers.length).toBe(max)
- await new Promise(resolve => setTimeout(resolve, 1500))
+ await TestUtils.sleep(1500)
// Here we expect the workers to be at the max size since that the task is still running
expect(longRunningPool.workers.length).toBe(max)
})
const expect = require('expect')
const { FixedThreadPool } = require('../../../lib/index')
+const TestUtils = require('../../test-utils')
const numberOfThreads = 10
const maxTasks = 400
const pool = new FixedThreadPool(
})
it('Shutdown test', async () => {
- let closedThreads = 0
- pool.workers.forEach(w => {
- w.on('exit', () => {
- closedThreads++
- })
- })
+ const exitPromise = TestUtils.waitExits(pool, numberOfThreads)
await pool.destroy()
- expect(closedThreads).toBe(numberOfThreads)
- })
-
- it('Validations test', () => {
- let error
- try {
- const pool1 = new FixedThreadPool()
- console.log(pool1)
- } catch (e) {
- error = e
- }
- expect(error).toBeTruthy()
- expect(error.message).toBeTruthy()
+ const res = await exitPromise
+ expect(res).toBe(numberOfThreads)
})
it('Should work even without opts in input', async () => {
--- /dev/null
+class TestUtils {
+ static async waitExits (pool, numberOfExitEventsToWait) {
+ let exitEvents = 0
+ return new Promise(function (resolve, reject) {
+ pool.workers.forEach(w => {
+ w.on('exit', () => {
+ exitEvents++
+ if (exitEvents === numberOfExitEventsToWait) {
+ resolve(exitEvents)
+ }
+ })
+ })
+ })
+ }
+
+ static async sleep (ms) {
+ return new Promise(resolve => setTimeout(resolve, ms))
+ }
+}
+
+module.exports = TestUtils
}
module.exports = new ClusterWorker(echo, {
- maxInactiveTime: 500,
killBehavior: KillBehaviors.HARD
})
}
module.exports = new ThreadWorker(echo, {
- maxInactiveTime: 500,
killBehavior: KillBehaviors.HARD
})