FixedThreadPool,
WorkerChoiceStrategies
} = require('../../../lib/index')
-const numberOfWorkers = 1
-const workerNotFoundInTasksUsageMapError = new Error(
- 'Worker could not be found in worker tasks usage map'
-)
-class StubPoolWithWorkerTasksUsageMapClear extends FixedThreadPool {
- removeAllWorker () {
- this.workersTasksUsage.clear()
+
+describe('Abstract pool test suite', () => {
+ const numberOfWorkers = 1
+ const workerNotFoundInTasksUsageMapError = new Error(
+ 'Worker could not be found in worker tasks usage map'
+ )
+ class StubPoolWithWorkerTasksUsageMapClear extends FixedThreadPool {
+ removeAllWorker () {
+ this.workersTasksUsage.clear()
+ }
}
-}
-class StubPoolWithIsMainMethod extends FixedThreadPool {
- isMain () {
- return false
+ class StubPoolWithIsMainMethod extends FixedThreadPool {
+ isMain () {
+ return false
+ }
}
-}
-describe('Abstract pool test suite', () => {
it('Simulate pool creation from a non main thread/process', () => {
expect(
() =>
const { DynamicClusterPool } = require('../../../lib/index')
const WorkerFunctions = require('../../test-types')
const TestUtils = require('../../test-utils')
-const min = 1
-const max = 3
-const pool = new DynamicClusterPool(
- min,
- max,
- './tests/worker-files/cluster/testWorker.js',
- {
- errorHandler: e => console.error(e)
- }
-)
describe('Dynamic cluster pool test suite', () => {
+ const min = 1
+ const max = 3
+ const pool = new DynamicClusterPool(
+ min,
+ max,
+ './tests/worker-files/cluster/testWorker.js',
+ {
+ errorHandler: e => console.error(e)
+ }
+ )
+
it('Verify that the function is executed in a worker cluster', async () => {
let result = await pool.execute({
function: WorkerFunctions.fibonacci
it('Should work even without opts in input', async () => {
const pool1 = new DynamicClusterPool(
- 1,
- 1,
+ min,
+ max,
'./tests/worker-files/cluster/testWorker.js'
)
const result = await pool1.execute()
const { FixedClusterPool } = require('../../../lib/index')
const WorkerFunctions = require('../../test-types')
const TestUtils = require('../../test-utils')
-const numberOfWorkers = 10
-const pool = new FixedClusterPool(
- numberOfWorkers,
- './tests/worker-files/cluster/testWorker.js',
- {
- errorHandler: e => console.error(e)
- }
-)
-const emptyPool = new FixedClusterPool(
- 1,
- './tests/worker-files/cluster/emptyWorker.js',
- { exitHandler: () => console.log('empty pool worker exited') }
-)
-const echoPool = new FixedClusterPool(
- 1,
- './tests/worker-files/cluster/echoWorker.js'
-)
-const errorPool = new FixedClusterPool(
- 1,
- './tests/worker-files/cluster/errorWorker.js',
- {
- errorHandler: e => console.error(e)
- }
-)
-const asyncErrorPool = new FixedClusterPool(
- 1,
- './tests/worker-files/cluster/asyncErrorWorker.js',
- {
- errorHandler: e => console.error(e)
- }
-)
-const asyncPool = new FixedClusterPool(
- 1,
- './tests/worker-files/cluster/asyncWorker.js'
-)
describe('Fixed cluster pool test suite', () => {
+ const numberOfWorkers = 6
+ const pool = new FixedClusterPool(
+ numberOfWorkers,
+ './tests/worker-files/cluster/testWorker.js',
+ {
+ errorHandler: e => console.error(e)
+ }
+ )
+ const emptyPool = new FixedClusterPool(
+ numberOfWorkers,
+ './tests/worker-files/cluster/emptyWorker.js',
+ { exitHandler: () => console.log('empty pool worker exited') }
+ )
+ const echoPool = new FixedClusterPool(
+ numberOfWorkers,
+ './tests/worker-files/cluster/echoWorker.js'
+ )
+ const errorPool = new FixedClusterPool(
+ numberOfWorkers,
+ './tests/worker-files/cluster/errorWorker.js',
+ {
+ errorHandler: e => console.error(e)
+ }
+ )
+ const asyncErrorPool = new FixedClusterPool(
+ numberOfWorkers,
+ './tests/worker-files/cluster/asyncErrorWorker.js',
+ {
+ errorHandler: e => console.error(e)
+ }
+ )
+ const asyncPool = new FixedClusterPool(
+ numberOfWorkers,
+ './tests/worker-files/cluster/asyncWorker.js'
+ )
+
after('Destroy all pools', async () => {
// We need to clean up the resources after our test
await echoPool.destroy()
it('Verify that data are sent to the worker correctly', async () => {
const data = { f: 10 }
const result = await echoPool.execute(data)
- expect(result).toEqual(data)
+ expect(result).toStrictEqual(data)
})
it('Verify that error handling is working properly:sync', async () => {
const startTime = new Date().getTime()
const result = await asyncPool.execute(data)
const usedTime = new Date().getTime() - startTime
- expect(result).toEqual(data)
+ expect(result).toStrictEqual(data)
expect(usedTime).toBeGreaterThanOrEqual(2000)
})
it('Should work even without opts in input', async () => {
const pool1 = new FixedClusterPool(
- 1,
+ numberOfWorkers,
'./tests/worker-files/cluster/testWorker.js'
)
const res = await pool1.execute()
} = require('../../../lib/index')
describe('Selection strategies test suite', () => {
+ const min = 0
+ const max = 3
+
it('Verify that WorkerChoiceStrategies enumeration provides string values', () => {
expect(WorkerChoiceStrategies.ROUND_ROBIN).toBe('ROUND_ROBIN')
expect(WorkerChoiceStrategies.LESS_RECENTLY_USED).toBe('LESS_RECENTLY_USED')
})
it('Verify ROUND_ROBIN strategy is the default at pool creation', async () => {
- const min = 0
- const max = 3
const pool = new DynamicThreadPool(
min,
max,
})
it('Verify ROUND_ROBIN strategy can be set after pool creation', async () => {
- const min = 0
- const max = 3
const pool = new DynamicThreadPool(
min,
max,
})
it('Verify ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
- const min = 0
- const max = 3
let pool = new FixedThreadPool(
max,
'./tests/worker-files/thread/testWorker.js'
})
it('Verify ROUND_ROBIN strategy can be run in a fixed pool', async () => {
- const max = 3
const pool = new FixedThreadPool(
max,
'./tests/worker-files/thread/testWorker.js',
})
it('Verify ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
- const min = 0
- const max = 3
const pool = new DynamicThreadPool(
min,
max,
})
it('Verify LESS_RECENTLY_USED strategy is taken at pool creation', async () => {
- const max = 3
const pool = new FixedThreadPool(
max,
'./tests/worker-files/thread/testWorker.js',
})
it('Verify LESS_RECENTLY_USED strategy can be set after pool creation', async () => {
- const max = 3
const pool = new FixedThreadPool(
max,
'./tests/worker-files/thread/testWorker.js'
})
it('Verify LESS_RECENTLY_USED strategy default tasks usage statistics requirements', async () => {
- const min = 0
- const max = 3
let pool = new FixedThreadPool(
max,
'./tests/worker-files/thread/testWorker.js'
})
it('Verify LESS_RECENTLY_USED strategy can be run in a fixed pool', async () => {
- const max = 3
const pool = new FixedThreadPool(
max,
'./tests/worker-files/thread/testWorker.js',
})
it('Verify LESS_RECENTLY_USED strategy can be run in a dynamic pool', async () => {
- const min = 0
- const max = 3
const pool = new DynamicThreadPool(
min,
max,
})
it('Verify FAIR_SHARE strategy is taken at pool creation', async () => {
- const max = 3
const pool = new FixedThreadPool(
max,
'./tests/worker-files/thread/testWorker.js',
})
it('Verify FAIR_SHARE strategy can be set after pool creation', async () => {
- const max = 3
const pool = new FixedThreadPool(
max,
'./tests/worker-files/thread/testWorker.js'
})
it('Verify FAIR_SHARE strategy default tasks usage statistics requirements', async () => {
- const min = 0
- const max = 3
let pool = new FixedThreadPool(
max,
'./tests/worker-files/thread/testWorker.js'
})
it('Verify FAIR_SHARE strategy can be run in a fixed pool', async () => {
- const max = 3
const pool = new FixedThreadPool(
max,
'./tests/worker-files/thread/testWorker.js',
})
it('Verify FAIR_SHARE strategy can be run in a dynamic pool', async () => {
- const min = 0
- const max = 3
const pool = new DynamicThreadPool(
min,
max,
})
it('Verify WEIGHTED_ROUND_ROBIN strategy is taken at pool creation', async () => {
- const max = 3
const pool = new FixedThreadPool(
max,
'./tests/worker-files/thread/testWorker.js',
})
it('Verify WEIGHTED_ROUND_ROBIN strategy can be set after pool creation', async () => {
- const max = 3
const pool = new FixedThreadPool(
max,
'./tests/worker-files/thread/testWorker.js'
})
it('Verify WEIGHTED_ROUND_ROBIN strategy default tasks usage statistics requirements', async () => {
- const min = 0
- const max = 3
let pool = new FixedThreadPool(
max,
'./tests/worker-files/thread/testWorker.js'
})
it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a fixed pool', async () => {
- const max = 3
const pool = new FixedThreadPool(
max,
'./tests/worker-files/thread/testWorker.js',
})
it('Verify WEIGHTED_ROUND_ROBIN strategy can be run in a dynamic pool', async () => {
- const min = 0
- const max = 3
const pool = new DynamicThreadPool(
min,
max,
})
it('Verify unknown strategies throw error', () => {
- const min = 1
- const max = 3
expect(
() =>
new DynamicThreadPool(
const { DynamicThreadPool } = require('../../../lib/index')
const WorkerFunctions = require('../../test-types')
const TestUtils = require('../../test-utils')
-const min = 1
-const max = 3
-const pool = new DynamicThreadPool(
- min,
- max,
- './tests/worker-files/thread/testWorker.js',
- {
- errorHandler: e => console.error(e)
- }
-)
describe('Dynamic thread pool test suite', () => {
+ const min = 1
+ const max = 3
+ const pool = new DynamicThreadPool(
+ min,
+ max,
+ './tests/worker-files/thread/testWorker.js',
+ {
+ errorHandler: e => console.error(e)
+ }
+ )
+
it('Verify that the function is executed in a worker thread', async () => {
let result = await pool.execute({
function: WorkerFunctions.fibonacci
it('Should work even without opts in input', async () => {
const pool1 = new DynamicThreadPool(
- 1,
- 1,
+ min,
+ max,
'./tests/worker-files/thread/testWorker.js'
)
const res = await pool1.execute()
const { FixedThreadPool } = require('../../../lib/index')
const WorkerFunctions = require('../../test-types')
const TestUtils = require('../../test-utils')
-const numberOfThreads = 10
-const pool = new FixedThreadPool(
- numberOfThreads,
- './tests/worker-files/thread/testWorker.js',
- {
- errorHandler: e => console.error(e)
- }
-)
-const emptyPool = new FixedThreadPool(
- 1,
- './tests/worker-files/thread/emptyWorker.js',
- { exitHandler: () => console.log('empty pool worker exited') }
-)
-const echoPool = new FixedThreadPool(
- 1,
- './tests/worker-files/thread/echoWorker.js'
-)
-const errorPool = new FixedThreadPool(
- 1,
- './tests/worker-files/thread/errorWorker.js',
- {
- errorHandler: e => console.error(e)
- }
-)
-const asyncErrorPool = new FixedThreadPool(
- 1,
- './tests/worker-files/thread/asyncErrorWorker.js',
- {
- errorHandler: e => console.error(e)
- }
-)
-const asyncPool = new FixedThreadPool(
- 1,
- './tests/worker-files/thread/asyncWorker.js'
-)
describe('Fixed thread pool test suite', () => {
+ const numberOfThreads = 6
+ const pool = new FixedThreadPool(
+ numberOfThreads,
+ './tests/worker-files/thread/testWorker.js',
+ {
+ errorHandler: e => console.error(e)
+ }
+ )
+ const emptyPool = new FixedThreadPool(
+ numberOfThreads,
+ './tests/worker-files/thread/emptyWorker.js',
+ { exitHandler: () => console.log('empty pool worker exited') }
+ )
+ const echoPool = new FixedThreadPool(
+ numberOfThreads,
+ './tests/worker-files/thread/echoWorker.js'
+ )
+ const errorPool = new FixedThreadPool(
+ numberOfThreads,
+ './tests/worker-files/thread/errorWorker.js',
+ {
+ errorHandler: e => console.error(e)
+ }
+ )
+ const asyncErrorPool = new FixedThreadPool(
+ numberOfThreads,
+ './tests/worker-files/thread/asyncErrorWorker.js',
+ {
+ errorHandler: e => console.error(e)
+ }
+ )
+ const asyncPool = new FixedThreadPool(
+ numberOfThreads,
+ './tests/worker-files/thread/asyncWorker.js'
+ )
+
after('Destroy all pools', async () => {
// We need to clean up the resources after our test
await echoPool.destroy()
it('Verify that data are sent to the worker correctly', async () => {
const data = { f: 10 }
const result = await echoPool.execute(data)
- expect(result).toEqual(data)
+ expect(result).toStrictEqual(data)
})
it('Verify that error handling is working properly:sync', async () => {
const startTime = new Date().getTime()
const result = await asyncPool.execute(data)
const usedTime = new Date().getTime() - startTime
- expect(result).toEqual(data)
+ expect(result).toStrictEqual(data)
expect(usedTime).toBeGreaterThanOrEqual(2000)
})
it('Should work even without opts in input', async () => {
const pool1 = new FixedThreadPool(
- 1,
+ numberOfThreads,
'./tests/worker-files/thread/testWorker.js'
)
const res = await pool1.execute()
const { expect } = require('expect')
const { ClusterWorker, KillBehaviors, ThreadWorker } = require('../../lib')
-class StubPoolWithIsMainWorker extends ThreadWorker {
- constructor (fn, opts) {
- super(fn, opts)
- this.mainWorker = false
+describe('Abstract worker test suite', () => {
+ class StubPoolWithIsMainWorker extends ThreadWorker {
+ constructor (fn, opts) {
+ super(fn, opts)
+ this.mainWorker = false
+ }
}
-}
-describe('Abstract worker test suite', () => {
it('Verify that fn function is mandatory', () => {
expect(() => new ClusterWorker()).toThrowError(
new Error('fn parameter is mandatory')
describe('Cluster worker test suite', () => {
it('Verify worker has default maxInactiveTime', () => {
const worker = new ClusterWorker(() => {})
- expect(worker.opts.maxInactiveTime).toEqual(60000)
+ expect(worker.opts.maxInactiveTime).toStrictEqual(60000)
})
it('Verify that handleError function works properly', () => {
const { expect } = require('expect')
const { ThreadWorker } = require('../../lib')
-let numberOfMessagesPosted = 0
-const postMessage = function () {
- numberOfMessagesPosted++
-}
-class SpyWorker extends ThreadWorker {
- getMainWorker () {
- return { postMessage }
+describe('Thread worker test suite', () => {
+ let numberOfMessagesPosted = 0
+ const postMessage = function () {
+ numberOfMessagesPosted++
+ }
+ class SpyWorker extends ThreadWorker {
+ getMainWorker () {
+ return { postMessage }
+ }
}
-}
-describe('Thread worker test suite', () => {
it('Verify worker has default maxInactiveTime', () => {
const worker = new ThreadWorker(() => {})
- expect(worker.opts.maxInactiveTime).toEqual(60000)
+ expect(worker.opts.maxInactiveTime).toStrictEqual(60000)
})
it('Verify worker invoke the getMainWorker and postMessage methods', () => {