errorHandler: e => console.error(e)
}
)
+ const queuePool = new FixedThreadPool(
+ numberOfThreads,
+ './tests/worker-files/thread/testWorker.js',
+ {
+ enableTasksQueue: true,
+ tasksQueueOptions: {
+ concurrency: 2
+ },
+ errorHandler: e => console.error(e)
+ }
+ )
const emptyPool = new FixedThreadPool(
numberOfThreads,
'./tests/worker-files/thread/emptyWorker.js',
await errorPool.destroy()
await asyncErrorPool.destroy()
await emptyPool.destroy()
+ await queuePool.destroy()
})
it('Verify that the function is executed in a worker thread', async () => {
expect(poolBusy).toBe(numberOfThreads + 1)
})
+ it('Verify that tasks queuing is working', async () => {
+ const maxMultiplier = 10
+ for (let i = 0; i < numberOfThreads * maxMultiplier; i++) {
+ queuePool.execute()
+ }
+ for (const workerNode of queuePool.workerNodes) {
+ expect(workerNode.tasksUsage.running).toBeLessThanOrEqual(
+ queuePool.opts.tasksQueueOptions.concurrency
+ )
+ expect(workerNode.tasksUsage.run).toBe(0)
+ expect(workerNode.tasksQueue.length).toBeGreaterThan(0)
+ }
+ // FIXME: wait for ongoing tasks to be executed
+ const promises = []
+ for (let i = 0; i < numberOfThreads * maxMultiplier; i++) {
+ promises.push(queuePool.execute())
+ }
+ await Promise.all(promises)
+ for (const workerNode of queuePool.workerNodes) {
+ expect(workerNode.tasksUsage.running).toBe(0)
+ expect(workerNode.tasksUsage.run).toBeGreaterThan(0)
+ expect(workerNode.tasksQueue.length).toBe(0)
+ }
+ })
+
it('Verify that is possible to have a worker that return undefined', async () => {
const result = await emptyPool.execute()
expect(result).toBeUndefined()