summary |
shortlog |
log |
commit | commitdiff |
tree
raw |
patch |
inline | side by side (from parent 1:
ee11a4a)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
'use strict'
const { ThreadWorker } = require('poolifier')
'use strict'
const { ThreadWorker } = require('poolifier')
+const { isMainThread } = require('worker_threads')
+
+const debug = false
function yourFunction (data) {
for (let i = 0; i <= 1000; i++) {
function yourFunction (data) {
for (let i = 0; i <= 1000; i++) {
- // console.log('This is the main thread ' + isMainThread)
+ debug === true && console.log('This is the main thread ' + isMainThread)
- it('Verify that pool options are checked', () => {
+ it('Verify that pool options are checked', async () => {
let pool = new FixedThreadPool(
numberOfWorkers,
'./tests/worker-files/thread/testWorker.js'
let pool = new FixedThreadPool(
numberOfWorkers,
'./tests/worker-files/thread/testWorker.js'
expect(pool.opts.errorHandler).toBeUndefined()
expect(pool.opts.onlineHandler).toBeUndefined()
expect(pool.opts.exitHandler).toBeUndefined()
expect(pool.opts.errorHandler).toBeUndefined()
expect(pool.opts.onlineHandler).toBeUndefined()
expect(pool.opts.exitHandler).toBeUndefined()
const testHandler = () => console.log('test handler executed')
pool = new FixedThreadPool(
numberOfWorkers,
const testHandler = () => console.log('test handler executed')
pool = new FixedThreadPool(
numberOfWorkers,
expect(pool.opts.errorHandler).toStrictEqual(testHandler)
expect(pool.opts.onlineHandler).toStrictEqual(testHandler)
expect(pool.opts.exitHandler).toStrictEqual(testHandler)
expect(pool.opts.errorHandler).toStrictEqual(testHandler)
expect(pool.opts.onlineHandler).toStrictEqual(testHandler)
expect(pool.opts.exitHandler).toStrictEqual(testHandler)
- it('Simulate worker not found during increaseWorkerRunningTasks', () => {
+ it('Simulate worker not found during increaseWorkerRunningTasks', async () => {
const pool = new StubPoolWithWorkerTasksUsageMapClear(
numberOfWorkers,
'./tests/worker-files/cluster/testWorker.js'
const pool = new StubPoolWithWorkerTasksUsageMapClear(
numberOfWorkers,
'./tests/worker-files/cluster/testWorker.js'
expect(() => pool.increaseWorkerRunningTasks()).toThrowError(
workerNotFoundInTasksUsageMapError
)
expect(() => pool.increaseWorkerRunningTasks()).toThrowError(
workerNotFoundInTasksUsageMapError
)
- it('Simulate worker not found during decreaseWorkerRunningTasks', () => {
+ it('Simulate worker not found during decreaseWorkerRunningTasks', async () => {
const pool = new StubPoolWithWorkerTasksUsageMapClear(
numberOfWorkers,
'./tests/worker-files/cluster/testWorker.js',
const pool = new StubPoolWithWorkerTasksUsageMapClear(
numberOfWorkers,
'./tests/worker-files/cluster/testWorker.js',
expect(() => pool.decreaseWorkerRunningTasks()).toThrowError(
workerNotFoundInTasksUsageMapError
)
expect(() => pool.decreaseWorkerRunningTasks()).toThrowError(
workerNotFoundInTasksUsageMapError
)
- it('Simulate worker not found during stepWorkerRunTasks', () => {
+ it('Simulate worker not found during stepWorkerRunTasks', async () => {
const pool = new StubPoolWithWorkerTasksUsageMapClear(
numberOfWorkers,
'./tests/worker-files/cluster/testWorker.js',
const pool = new StubPoolWithWorkerTasksUsageMapClear(
numberOfWorkers,
'./tests/worker-files/cluster/testWorker.js',
expect(() => pool.stepWorkerRunTasks()).toThrowError(
workerNotFoundInTasksUsageMapError
)
expect(() => pool.stepWorkerRunTasks()).toThrowError(
workerNotFoundInTasksUsageMapError
)
- it('Simulate worker not found during updateWorkerTasksRunTime with strategy not requiring it', () => {
+ it('Simulate worker not found during updateWorkerTasksRunTime with strategy not requiring it', async () => {
const pool = new StubPoolWithWorkerTasksUsageMapClear(
numberOfWorkers,
'./tests/worker-files/cluster/testWorker.js',
const pool = new StubPoolWithWorkerTasksUsageMapClear(
numberOfWorkers,
'./tests/worker-files/cluster/testWorker.js',
// Simulate worker not found.
pool.removeAllWorker()
expect(() => pool.updateWorkerTasksRunTime()).not.toThrowError()
// Simulate worker not found.
pool.removeAllWorker()
expect(() => pool.updateWorkerTasksRunTime()).not.toThrowError()
- it('Simulate worker not found during updateWorkerTasksRunTime with strategy requiring it', () => {
+ it('Simulate worker not found during updateWorkerTasksRunTime with strategy requiring it', async () => {
const pool = new StubPoolWithWorkerTasksUsageMapClear(
numberOfWorkers,
'./tests/worker-files/cluster/testWorker.js',
const pool = new StubPoolWithWorkerTasksUsageMapClear(
numberOfWorkers,
'./tests/worker-files/cluster/testWorker.js',
expect(() => pool.updateWorkerTasksRunTime()).toThrowError(
workerNotFoundInTasksUsageMapError
)
expect(() => pool.updateWorkerTasksRunTime()).toThrowError(
workerNotFoundInTasksUsageMapError
)
- it('Verify that worker pool tasks usage are initialized', () => {
+ it('Verify that worker pool tasks usage are initialized', async () => {
const pool = new FixedClusterPool(
numberOfWorkers,
'./tests/worker-files/cluster/testWorker.js'
const pool = new FixedClusterPool(
numberOfWorkers,
'./tests/worker-files/cluster/testWorker.js'
expect(tasksUsage.runTime).toBe(0)
expect(tasksUsage.avgRunTime).toBe(0)
}
expect(tasksUsage.runTime).toBe(0)
expect(tasksUsage.avgRunTime).toBe(0)
}
})
it('Verify that worker pool tasks usage are computed', async () => {
})
it('Verify that worker pool tasks usage are computed', async () => {
expect(tasksUsage.runTime).toBeGreaterThanOrEqual(0)
expect(tasksUsage.avgRunTime).toBeGreaterThanOrEqual(0)
}
expect(tasksUsage.runTime).toBeGreaterThanOrEqual(0)
expect(tasksUsage.avgRunTime).toBeGreaterThanOrEqual(0)
}
})
it('Verify that worker pool tasks usage are reset at worker choice strategy change', async () => {
})
it('Verify that worker pool tasks usage are reset at worker choice strategy change', async () => {
expect(tasksUsage.runTime).toBe(0)
expect(tasksUsage.avgRunTime).toBe(0)
}
expect(tasksUsage.runTime).toBe(0)
expect(tasksUsage.avgRunTime).toBe(0)
}
})
it("Verify that pool event emitter 'busy' event can register a callback", async () => {
})
it("Verify that pool event emitter 'busy' event can register a callback", async () => {
// 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)
// 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)
- after(() => {
- pool.destroy()
+ after(async () => {
+ await pool.destroy()
})
it('Verify that getWorkerChoiceStrategy() default return ROUND_ROBIN strategy', () => {
})
it('Verify that getWorkerChoiceStrategy() default return ROUND_ROBIN strategy', () => {
pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
.requiredStatistics.runTime
).toBe(false)
pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
.requiredStatistics.runTime
).toBe(false)
pool = new DynamicThreadPool(
min,
max,
pool = new DynamicThreadPool(
min,
max,
pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
.requiredStatistics.runTime
).toBe(false)
pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
.requiredStatistics.runTime
).toBe(false)
pool = new DynamicThreadPool(
min,
max,
pool = new DynamicThreadPool(
min,
max,
pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
.requiredStatistics.runTime
).toBe(true)
pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
.requiredStatistics.runTime
).toBe(true)
pool = new DynamicThreadPool(
min,
max,
pool = new DynamicThreadPool(
min,
max,
pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
.requiredStatistics.runTime
).toBe(true)
pool.workerChoiceStrategyContext.getWorkerChoiceStrategy()
.requiredStatistics.runTime
).toBe(true)
pool = new DynamicThreadPool(
min,
max,
pool = new DynamicThreadPool(
min,
max,
- after(() => {
- fixedPool.destroy()
- dynamicPool.destroy()
+ after(async () => {
+ await fixedPool.destroy()
+ await dynamicPool.destroy()
})
it('Verify that execute() return the worker chosen by the strategy with fixed pool', () => {
})
it('Verify that execute() return the worker chosen by the strategy with fixed pool', () => {