)
const promises = []
for (let i = 0; i < numberOfWorkers * 2; i++) {
- promises.push(pool.execute({ test: 'test' }))
+ promises.push(pool.execute())
}
for (const tasksUsage of pool.workersTasksUsage.values()) {
expect(tasksUsage).toBeDefined()
let poolBusy = 0
pool.emitter.on('busy', () => poolBusy++)
for (let i = 0; i < numberOfWorkers * 2; i++) {
- promises.push(pool.execute({ test: 'test' }))
+ promises.push(pool.execute())
}
await Promise.all(promises)
// The `busy` event is triggered when the number of submitted tasks at once reach the number of fixed pool workers.
const { expect } = require('expect')
const { DynamicClusterPool } = require('../../../lib/index')
+const WorkerFunctions = require('../../test-types')
const TestUtils = require('../../test-utils')
const min = 1
const max = 3
describe('Dynamic cluster pool test suite', () => {
it('Verify that the function is executed in a worker cluster', async () => {
- const result = await pool.execute({ test: 'test' })
- expect(result).toBeDefined()
- expect(result).toBeFalsy()
+ let result = await pool.execute({
+ function: WorkerFunctions.fibonacci
+ })
+ expect(result).toBe(false)
+ result = await pool.execute({
+ function: WorkerFunctions.factorial
+ })
+ expect(result).toBe(false)
})
it('Verify that new workers are created when required, max size is not exceeded and that after a while new workers will die', async () => {
let poolBusy = 0
pool.emitter.on('busy', () => poolBusy++)
for (let i = 0; i < max * 2; i++) {
- promises.push(pool.execute({ test: 'test' }))
+ promises.push(pool.execute())
}
expect(pool.workers.length).toBeLessThanOrEqual(max)
expect(pool.workers.length).toBeGreaterThan(min)
it('Verify scale worker up and down is working', async () => {
expect(pool.workers.length).toBe(min)
for (let i = 0; i < max * 10; i++) {
- pool.execute({ test: 'test' })
+ pool.execute()
}
expect(pool.workers.length).toBeGreaterThan(min)
await TestUtils.waitExits(pool, max - min)
expect(pool.workers.length).toBe(min)
for (let i = 0; i < max * 10; i++) {
- pool.execute({ test: 'test' })
+ pool.execute()
}
expect(pool.workers.length).toBeGreaterThan(min)
await TestUtils.waitExits(pool, max - min)
1,
'./tests/worker-files/cluster/testWorker.js'
)
- const result = await pool1.execute({ test: 'test' })
- expect(result).toBeDefined()
- expect(result).toBeFalsy()
+ const result = await pool1.execute()
+ expect(result).toBe(false)
// We need to clean up the resources after our test
await pool1.destroy()
})
)
expect(longRunningPool.workers.length).toBe(min)
for (let i = 0; i < max * 10; i++) {
- longRunningPool.execute({ test: 'test' })
+ longRunningPool.execute()
}
expect(longRunningPool.workers.length).toBe(max)
await TestUtils.waitExits(longRunningPool, max - min)
)
expect(longRunningPool.workers.length).toBe(min)
for (let i = 0; i < max * 10; i++) {
- longRunningPool.execute({ test: 'test' })
+ longRunningPool.execute()
}
expect(longRunningPool.workers.length).toBe(max)
await TestUtils.sleep(1500)
const { expect } = require('expect')
const { FixedClusterPool } = require('../../../lib/index')
+const WorkerFunctions = require('../../test-types')
const TestUtils = require('../../test-utils')
const numberOfWorkers = 10
const pool = new FixedClusterPool(
})
it('Verify that the function is executed in a worker cluster', async () => {
- const result = await pool.execute({ test: 'test' })
- expect(result).toBeDefined()
- expect(result).toBeFalsy()
+ let result = await pool.execute({
+ function: WorkerFunctions.fibonacci
+ })
+ expect(result).toBe(false)
+ result = await pool.execute({
+ function: WorkerFunctions.factorial
+ })
+ expect(result).toBe(false)
})
it('Verify that is possible to invoke the execute method without input', async () => {
const result = await pool.execute()
- expect(result).toBeDefined()
- expect(result).toBeFalsy()
+ expect(result).toBe(false)
})
it('Verify that busy event is emitted', async () => {
let poolBusy = 0
pool.emitter.on('busy', () => poolBusy++)
for (let i = 0; i < numberOfWorkers * 2; i++) {
- promises.push(pool.execute({ test: 'test' }))
+ promises.push(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.
it('Verify that is possible to have a worker that return undefined', async () => {
const result = await emptyPool.execute()
- expect(result).toBeFalsy()
+ expect(result).toBeUndefined()
})
it('Verify that data are sent to the worker correctly', async () => {
const data = { f: 10 }
const result = await echoPool.execute(data)
- expect(result).toBeTruthy()
- expect(result.f).toBe(data.f)
+ expect(result).toEqual(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).toBeTruthy()
- expect(result.f).toBe(data.f)
+ expect(result).toEqual(data)
expect(usedTime).toBeGreaterThanOrEqual(2000)
})
1,
'./tests/worker-files/cluster/testWorker.js'
)
- const res = await pool1.execute({ test: 'test' })
- expect(res).toBeFalsy()
+ const res = await pool1.execute()
+ expect(res).toBe(false)
// We need to clean up the resources after our test
await pool1.destroy()
})
// TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
const promises = []
for (let i = 0; i < max * 2; i++) {
- promises.push(pool.execute({ test: 'test' }))
+ promises.push(pool.execute())
}
await Promise.all(promises)
// We need to clean up the resources after our test
// TODO: Create a better test to cover `RoundRobinWorkerChoiceStrategy#choose`
const promises = []
for (let i = 0; i < max * 2; i++) {
- promises.push(pool.execute({ test: 'test' }))
+ promises.push(pool.execute())
}
await Promise.all(promises)
// We need to clean up the resources after our test
// TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
const promises = []
for (let i = 0; i < max * 2; i++) {
- promises.push(pool.execute({ test: 'test' }))
+ promises.push(pool.execute())
}
await Promise.all(promises)
// We need to clean up the resources after our test
// TODO: Create a better test to cover `LessRecentlyUsedWorkerChoiceStrategy#choose`
const promises = []
for (let i = 0; i < max * 2; i++) {
- promises.push(pool.execute({ test: 'test' }))
+ promises.push(pool.execute())
}
await Promise.all(promises)
// We need to clean up the resources after our test
// TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
const promises = []
for (let i = 0; i < max * 2; i++) {
- promises.push(pool.execute({ test: 'test' }))
+ promises.push(pool.execute())
}
await Promise.all(promises)
// We need to clean up the resources after our test
// TODO: Create a better test to cover `FairShareChoiceStrategy#choose`
const promises = []
for (let i = 0; i < max * 2; i++) {
- promises.push(pool.execute({ test: 'test' }))
+ promises.push(pool.execute())
}
await Promise.all(promises)
// We need to clean up the resources after our test
// TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
const promises = []
for (let i = 0; i < max * 2; i++) {
- promises.push(pool.execute({ test: 'test' }))
+ promises.push(pool.execute())
}
await Promise.all(promises)
// We need to clean up the resources after our test
// TODO: Create a better test to cover `WeightedRoundRobinWorkerChoiceStrategy#choose`
const promises = []
for (let i = 0; i < max * 2; i++) {
- promises.push(pool.execute({ test: 'test' }))
+ promises.push(pool.execute())
}
await Promise.all(promises)
// We need to clean up the resources after our test
const { expect } = require('expect')
const { DynamicThreadPool } = require('../../../lib/index')
+const WorkerFunctions = require('../../test-types')
const TestUtils = require('../../test-utils')
const min = 1
const max = 3
describe('Dynamic thread pool test suite', () => {
it('Verify that the function is executed in a worker thread', async () => {
- const result = await pool.execute({ test: 'test' })
- expect(result).toBeDefined()
- expect(result).toBeFalsy()
+ let result = await pool.execute({
+ function: WorkerFunctions.fibonacci
+ })
+ expect(result).toBe(false)
+ result = await pool.execute({
+ function: WorkerFunctions.factorial
+ })
+ expect(result).toBe(false)
})
it('Verify that new workers are created when required, max size is not exceeded and that after a while new workers will die', async () => {
let poolBusy = 0
pool.emitter.on('busy', () => poolBusy++)
for (let i = 0; i < max * 2; i++) {
- promises.push(pool.execute({ test: 'test' }))
+ promises.push(pool.execute())
}
expect(pool.workers.length).toBeLessThanOrEqual(max)
expect(pool.workers.length).toBeGreaterThan(min)
it('Verify scale thread up and down is working', async () => {
expect(pool.workers.length).toBe(min)
for (let i = 0; i < max * 10; i++) {
- pool.execute({ test: 'test' })
+ pool.execute()
}
expect(pool.workers.length).toBe(max)
await TestUtils.waitExits(pool, max - min)
expect(pool.workers.length).toBe(min)
for (let i = 0; i < max * 10; i++) {
- pool.execute({ test: 'test' })
+ pool.execute()
}
expect(pool.workers.length).toBe(max)
await TestUtils.waitExits(pool, max - min)
1,
'./tests/worker-files/thread/testWorker.js'
)
- const res = await pool1.execute({ test: 'test' })
- expect(res).toBeDefined()
- expect(res).toBeFalsy()
+ const res = await pool1.execute()
+ expect(res).toBe(false)
// We need to clean up the resources after our test
await pool1.destroy()
})
)
expect(longRunningPool.workers.length).toBe(min)
for (let i = 0; i < max * 10; i++) {
- longRunningPool.execute({ test: 'test' })
+ longRunningPool.execute()
}
expect(longRunningPool.workers.length).toBe(max)
await TestUtils.waitExits(longRunningPool, max - min)
)
expect(longRunningPool.workers.length).toBe(min)
for (let i = 0; i < max * 10; i++) {
- longRunningPool.execute({ test: 'test' })
+ longRunningPool.execute()
}
expect(longRunningPool.workers.length).toBe(max)
await TestUtils.sleep(1500)
const { expect } = require('expect')
const { FixedThreadPool } = require('../../../lib/index')
+const WorkerFunctions = require('../../test-types')
const TestUtils = require('../../test-utils')
const numberOfThreads = 10
const pool = new FixedThreadPool(
})
it('Verify that the function is executed in a worker thread', async () => {
- const result = await pool.execute({ test: 'test' })
- expect(result).toBeDefined()
- expect(result).toBeFalsy()
+ let result = await pool.execute({
+ function: WorkerFunctions.fibonacci
+ })
+ expect(result).toBe(false)
+ result = await pool.execute({
+ function: WorkerFunctions.factorial
+ })
+ expect(result).toBe(false)
})
it('Verify that is possible to invoke the execute method without input', async () => {
const result = await pool.execute()
- expect(result).toBeDefined()
- expect(result).toBeFalsy()
+ expect(result).toBe(false)
})
it('Verify that busy event is emitted', async () => {
let poolBusy = 0
pool.emitter.on('busy', () => poolBusy++)
for (let i = 0; i < numberOfThreads * 2; i++) {
- promises.push(pool.execute({ test: 'test' }))
+ promises.push(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 numberOfThreads + 1 times for a loop submitting up to numberOfThreads * 2 tasks to the fixed pool.
it('Verify that is possible to have a worker that return undefined', async () => {
const result = await emptyPool.execute()
- expect(result).toBeFalsy()
+ expect(result).toBeUndefined()
})
it('Verify that data are sent to the worker correctly', async () => {
const data = { f: 10 }
const result = await echoPool.execute(data)
- expect(result).toBeTruthy()
- expect(result.f).toBe(data.f)
+ expect(result).toEqual(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).toBeTruthy()
- expect(result.f).toBe(data.f)
+ expect(result).toEqual(data)
expect(usedTime).toBeGreaterThanOrEqual(2000)
})
1,
'./tests/worker-files/thread/testWorker.js'
)
- const res = await pool1.execute({ test: 'test' })
- expect(res).toBeFalsy()
+ const res = await pool1.execute()
+ expect(res).toBe(false)
// We need to clean up the resources after our test
await pool1.destroy()
})
--- /dev/null
+const WorkerFunctions = {
+ jsonIntegerSerialization: 'jsonIntegerSerialization',
+ fibonacci: 'fibonacci',
+ factorial: 'factorial'
+}
+
+module.exports = WorkerFunctions
+const WorkerFunctions = require('./test-types')
+
class TestUtils {
static async waitExits (pool, numberOfExitEventsToWait) {
let exitEvents = 0
return new Promise(resolve => setTimeout(resolve, ms))
}
- static async workerSleepFunction (
+ static async sleepWorkerFunction (
data,
ms,
rejection = false,
return TestUtils.factorial(n - 1) * n
}
}
+
+ static executeWorkerFunction (data) {
+ switch (data.function) {
+ case WorkerFunctions.jsonIntegerSerialization:
+ return TestUtils.jsonIntegerSerialization(data.n || 100)
+ case WorkerFunctions.fibonacci:
+ return TestUtils.fibonacci(data.n || 25)
+ case WorkerFunctions.factorial:
+ return TestUtils.factorial(data.n || 100)
+ default:
+ throw new Error('Unknown worker function')
+ }
+ }
}
module.exports = TestUtils
const TestUtils = require('../../test-utils')
async function error (data) {
- return TestUtils.workerSleepFunction(
+ return TestUtils.sleepWorkerFunction(
data,
2000,
true,
const TestUtils = require('../../test-utils')
async function sleep (data) {
- return TestUtils.workerSleepFunction(data, 2000)
+ return TestUtils.sleepWorkerFunction(data, 2000)
}
module.exports = new ClusterWorker(sleep, {
const TestUtils = require('../../test-utils')
async function sleep (data) {
- return TestUtils.workerSleepFunction(data, 50000)
+ return TestUtils.sleepWorkerFunction(data, 50000)
}
module.exports = new ClusterWorker(sleep, {
const TestUtils = require('../../test-utils')
async function sleep (data) {
- return TestUtils.workerSleepFunction(data, 50000)
+ return TestUtils.sleepWorkerFunction(data, 50000)
}
module.exports = new ClusterWorker(sleep, {
const { ClusterWorker, KillBehaviors } = require('../../../lib/index')
const { isMaster } = require('cluster')
const TestUtils = require('../../test-utils')
+const WorkerFunctions = require('../../test-types')
function test (data) {
- TestUtils.jsonIntegerSerialization(100)
+ data = data || {}
+ data.function = data.function || WorkerFunctions.jsonIntegerSerialization
+ TestUtils.executeWorkerFunction(data)
return isMaster
}
const TestUtils = require('../../test-utils')
async function error (data) {
- return TestUtils.workerSleepFunction(
+ return TestUtils.sleepWorkerFunction(
data,
2000,
true,
const TestUtils = require('../../test-utils')
async function sleep (data) {
- return TestUtils.workerSleepFunction(data, 2000)
+ return TestUtils.sleepWorkerFunction(data, 2000)
}
module.exports = new ThreadWorker(sleep, {
const TestUtils = require('../../test-utils')
async function sleep (data) {
- return TestUtils.workerSleepFunction(data, 50000)
+ return TestUtils.sleepWorkerFunction(data, 50000)
}
module.exports = new ThreadWorker(sleep, {
const TestUtils = require('../../test-utils')
async function sleep (data) {
- return TestUtils.workerSleepFunction(data, 50000)
+ return TestUtils.sleepWorkerFunction(data, 50000)
}
module.exports = new ThreadWorker(sleep, {
const { ThreadWorker, KillBehaviors } = require('../../../lib/index')
const { isMainThread } = require('worker_threads')
const TestUtils = require('../../test-utils')
+const WorkerFunctions = require('../../test-types')
function test (data) {
- TestUtils.jsonIntegerSerialization(100)
+ data = data || {}
+ data.function = data.function || WorkerFunctions.jsonIntegerSerialization
+ TestUtils.executeWorkerFunction(data)
return isMainThread
}