X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=tests%2Fpools%2Fthread%2Ffixed.test.mjs;h=44e7d80ba7c7f4049ec2305ab6b039fb38039124;hb=6d7beb8c8cf0bdb1f5ef19f57fcfb0aa0e53f074;hp=a1cb8dd294b100263e0bb97e898b2890e02d4c43;hpb=a074ffee1b46f43d0dcfba58128748c7492104dd;p=poolifier.git diff --git a/tests/pools/thread/fixed.test.mjs b/tests/pools/thread/fixed.test.mjs index a1cb8dd2..44e7d80b 100644 --- a/tests/pools/thread/fixed.test.mjs +++ b/tests/pools/thread/fixed.test.mjs @@ -1,22 +1,22 @@ import { expect } from 'expect' -import { FixedThreadPool, PoolEvents } from '../../../lib/index.js' -import { TaskFunctions } from '../../test-types.js' -import { waitPoolEvents, waitWorkerEvents } from '../../test-utils.js' -import { DEFAULT_TASK_NAME } from '../../../lib/utils.js' +import { FixedThreadPool, PoolEvents } from '../../../lib/index.cjs' +import { TaskFunctions } from '../../test-types.cjs' +import { waitPoolEvents, waitWorkerEvents } from '../../test-utils.cjs' +import { DEFAULT_TASK_NAME } from '../../../lib/utils.cjs' describe('Fixed thread pool test suite', () => { const numberOfThreads = 6 const tasksConcurrency = 2 const pool = new FixedThreadPool( numberOfThreads, - './tests/worker-files/thread/testWorker.js', + './tests/worker-files/thread/testWorker.mjs', { errorHandler: e => console.error(e) } ) const queuePool = new FixedThreadPool( numberOfThreads, - './tests/worker-files/thread/testWorker.js', + './tests/worker-files/thread/testWorker.mjs', { enableTasksQueue: true, tasksQueueOptions: { @@ -27,30 +27,30 @@ describe('Fixed thread pool test suite', () => { ) const emptyPool = new FixedThreadPool( numberOfThreads, - './tests/worker-files/thread/emptyWorker.js', + './tests/worker-files/thread/emptyWorker.mjs', { exitHandler: () => console.info('empty pool worker exited') } ) const echoPool = new FixedThreadPool( numberOfThreads, - './tests/worker-files/thread/echoWorker.js' + './tests/worker-files/thread/echoWorker.mjs' ) const errorPool = new FixedThreadPool( numberOfThreads, - './tests/worker-files/thread/errorWorker.js', + './tests/worker-files/thread/errorWorker.mjs', { errorHandler: e => console.error(e) } ) const asyncErrorPool = new FixedThreadPool( numberOfThreads, - './tests/worker-files/thread/asyncErrorWorker.js', + './tests/worker-files/thread/asyncErrorWorker.mjs', { errorHandler: e => console.error(e) } ) const asyncPool = new FixedThreadPool( numberOfThreads, - './tests/worker-files/thread/asyncWorker.js' + './tests/worker-files/thread/asyncWorker.mjs' ) after('Destroy all pools', async () => { @@ -82,7 +82,7 @@ describe('Fixed thread pool test suite', () => { it("Verify that 'ready' event is emitted", async () => { const pool = new FixedThreadPool( numberOfThreads, - './tests/worker-files/thread/testWorker.js', + './tests/worker-files/thread/testWorker.mjs', { errorHandler: e => console.error(e) } @@ -130,6 +130,7 @@ describe('Fixed thread pool test suite', () => { expect(workerNode.usage.tasks.maxQueued).toBe( maxMultiplier - queuePool.opts.tasksQueueOptions.concurrency ) + expect(workerNode.usage.tasks.sequentiallyStolen).toBe(0) expect(workerNode.usage.tasks.stolen).toBe(0) } expect(queuePool.info.executedTasks).toBe(0) @@ -157,6 +158,12 @@ describe('Fixed thread pool test suite', () => { expect(workerNode.usage.tasks.maxQueued).toBe( maxMultiplier - queuePool.opts.tasksQueueOptions.concurrency ) + expect(workerNode.usage.tasks.sequentiallyStolen).toBeGreaterThanOrEqual( + 0 + ) + expect(workerNode.usage.tasks.sequentiallyStolen).toBeLessThanOrEqual( + numberOfThreads * maxMultiplier + ) expect(workerNode.usage.tasks.stolen).toBeGreaterThanOrEqual(0) expect(workerNode.usage.tasks.stolen).toBeLessThanOrEqual( numberOfThreads * maxMultiplier @@ -202,8 +209,9 @@ describe('Fixed thread pool test suite', () => { error = e } expect(result).toStrictEqual({ ok: 1 }) - expect(error).toStrictEqual( - new TypeError('Found invalid object in transferList') + expect(error).toBeInstanceOf(Error) + expect(error.message).toMatch( + /Found invalid (object|value) in transferList/ ) }) @@ -292,13 +300,15 @@ describe('Fixed thread pool test suite', () => { await pool.destroy() const numberOfExitEvents = await exitPromise expect(pool.started).toBe(false) + expect(pool.emitter.eventNames()).toStrictEqual([]) + expect(pool.readyEventEmitted).toBe(false) expect(pool.workerNodes.length).toBe(0) expect(numberOfExitEvents).toBe(numberOfThreads) expect(poolDestroy).toBe(1) }) it('Verify that thread pool options are checked', async () => { - const workerFilePath = './tests/worker-files/thread/testWorker.js' + const workerFilePath = './tests/worker-files/thread/testWorker.mjs' let pool = new FixedThreadPool(numberOfThreads, workerFilePath) expect(pool.opts.workerOptions).toBeUndefined() await pool.destroy() @@ -316,7 +326,7 @@ describe('Fixed thread pool test suite', () => { }) it('Should work even without opts in input', async () => { - const workerFilePath = './tests/worker-files/thread/testWorker.js' + const workerFilePath = './tests/worker-files/thread/testWorker.mjs' const pool = new FixedThreadPool(numberOfThreads, workerFilePath) const res = await pool.execute() expect(res).toStrictEqual({ ok: 1 }) @@ -325,7 +335,7 @@ describe('Fixed thread pool test suite', () => { }) it('Verify destroyWorkerNode()', async () => { - const workerFilePath = './tests/worker-files/thread/testWorker.js' + const workerFilePath = './tests/worker-files/thread/testWorker.mjs' const pool = new FixedThreadPool(numberOfThreads, workerFilePath) const workerNodeKey = 0 let exitEvent = 0 @@ -338,9 +348,9 @@ describe('Fixed thread pool test suite', () => { await pool.destroy() }) - it('Verify that a pool with zero worker fails', async () => { + it('Verify that a pool with zero worker fails', () => { expect( - () => new FixedThreadPool(0, './tests/worker-files/thread/testWorker.js') - ).toThrowError('Cannot instantiate a fixed pool with zero worker') + () => new FixedThreadPool(0, './tests/worker-files/thread/testWorker.mjs') + ).toThrow('Cannot instantiate a fixed pool with zero worker') }) })