X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=tests%2Fpools%2Fthread%2Ffixed.test.js;h=f2b36fa7d9eeb617919c86dcdbbf1d797f7e777e;hb=95ec6006ca39aef0102c82ca42e104a4e9b40b6b;hp=c7abad00834c0a6cfd3c96bf4ae06b731d82e2f3;hpb=3f7eb773a2c8775fa263b696b6a611e484e437a8;p=poolifier.git diff --git a/tests/pools/thread/fixed.test.js b/tests/pools/thread/fixed.test.js index c7abad00..f2b36fa7 100644 --- a/tests/pools/thread/fixed.test.js +++ b/tests/pools/thread/fixed.test.js @@ -1,6 +1,6 @@ const { expect } = require('expect') -const { FixedThreadPool } = require('../../../lib/index') -const WorkerFunctions = require('../../test-types') +const { FixedThreadPool, PoolEvents } = require('../../../lib/index') +const { WorkerFunctions } = require('../../test-types') const TestUtils = require('../../test-utils') describe('Fixed thread pool test suite', () => { @@ -12,6 +12,17 @@ describe('Fixed thread pool test suite', () => { 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', @@ -47,14 +58,7 @@ describe('Fixed thread pool test suite', () => { await errorPool.destroy() await asyncErrorPool.destroy() await emptyPool.destroy() - }) - - it('Choose worker round robin test', async () => { - const results = new Set() - for (let i = 0; i < numberOfThreads; i++) { - results.add(pool.chooseWorker().threadId) - } - expect(results.size).toBe(numberOfThreads) + await queuePool.destroy() }) it('Verify that the function is executed in a worker thread', async () => { @@ -68,23 +72,49 @@ describe('Fixed thread pool test suite', () => { expect(result).toBe(false) }) - it('Verify that is possible to invoke the execute method without input', async () => { + it('Verify that is possible to invoke the execute() method without input', async () => { const result = await pool.execute() expect(result).toBe(false) }) - it('Verify that busy event is emitted', async () => { - const promises = [] + it("Verify that 'busy' event is emitted", async () => { let poolBusy = 0 - pool.emitter.on('busy', () => poolBusy++) + pool.emitter.on(PoolEvents.busy, () => ++poolBusy) for (let i = 0; i < numberOfThreads * 2; i++) { - promises.push(pool.execute()) + 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. expect(poolBusy).toBe(numberOfThreads + 1) }) + it('Verify that tasks queuing is working', async () => { + const maxMultiplier = 10 + const promises = new Set() + for (let i = 0; i < numberOfThreads * maxMultiplier; i++) { + promises.add(queuePool.execute()) + } + expect(promises.size).toBe(numberOfThreads * maxMultiplier) + 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) + } + expect(queuePool.numberOfRunningTasks).toBe(numberOfThreads) + expect(queuePool.numberOfQueuedTasks).toBe( + numberOfThreads * maxMultiplier - numberOfThreads + ) + 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) + } + promises.clear() + }) + it('Verify that is possible to have a worker that return undefined', async () => { const result = await emptyPool.execute() expect(result).toBeUndefined() @@ -128,9 +158,9 @@ describe('Fixed thread pool test suite', () => { it('Verify that async function is working properly', async () => { const data = { f: 10 } - const startTime = Date.now() + const startTime = performance.now() const result = await asyncPool.execute(data) - const usedTime = Date.now() - startTime + const usedTime = performance.now() - startTime expect(result).toStrictEqual(data) expect(usedTime).toBeGreaterThanOrEqual(2000) }) @@ -156,6 +186,6 @@ describe('Fixed thread pool test suite', () => { it('Verify that a pool with zero worker fails', async () => { expect( () => new FixedThreadPool(0, './tests/worker-files/thread/testWorker.js') - ).toThrowError(new Error('Cannot instantiate a fixed pool with no worker')) + ).toThrowError('Cannot instantiate a fixed pool with no worker') }) })