X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=tests%2Fpools%2Fcluster%2Ffixed.test.js;h=04467620f7d50c61b8813af25ecd011b6d145e8e;hb=292ad316a2815762f2e4a822383f1aef5ae49774;hp=f5300eccf424c39028e340296b18d1d0492f45f4;hpb=aad2595fd9c26bcb2f0d7a22d06edf56d5b08bdb;p=poolifier.git diff --git a/tests/pools/cluster/fixed.test.js b/tests/pools/cluster/fixed.test.js index f5300ecc..04467620 100644 --- a/tests/pools/cluster/fixed.test.js +++ b/tests/pools/cluster/fixed.test.js @@ -1,7 +1,7 @@ const expect = require('expect') const { FixedClusterPool } = require('../../../lib/index') +const TestUtils = require('../../test-utils') const numberOfWorkers = 10 -const maxTasks = 500 const pool = new FixedClusterPool( numberOfWorkers, './tests/worker-files/cluster/testWorker.js', @@ -11,7 +11,8 @@ const pool = new FixedClusterPool( ) const emptyPool = new FixedClusterPool( 1, - './tests/worker-files/cluster/emptyWorker.js' + './tests/worker-files/cluster/emptyWorker.js', + { exitHandler: () => console.log('empty pool worker exited') } ) const echoPool = new FixedClusterPool( 1, @@ -24,23 +25,28 @@ const errorPool = new FixedClusterPool( errorHandler: e => console.error(e) } ) - const asyncErrorPool = new FixedClusterPool( 1, './tests/worker-files/cluster/asyncErrorWorker.js', { - onlineHandler: () => console.log('worker is online') + errorHandler: e => console.error(e) } ) const asyncPool = new FixedClusterPool( 1, - './tests/worker-files/cluster/asyncWorker.js', - { - maxTasks: maxTasks - } + './tests/worker-files/cluster/asyncWorker.js' ) -describe('Fixed cluster pool test suite ', () => { +describe('Fixed cluster pool test suite', () => { + after('Destroy all pools', async () => { + // We need to clean up the resources after our test + await echoPool.destroy() + await asyncPool.destroy() + 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 < numberOfWorkers; i++) { @@ -61,6 +67,16 @@ describe('Fixed cluster pool test suite ', () => { expect(result).toBeFalsy() }) + it('Verify that busy event is emitted', async () => { + const promises = [] + let poolBusy = 0 + pool.emitter.on('busy', () => poolBusy++) + for (let i = 0; i < numberOfWorkers * 2; i++) { + promises.push(pool.execute({ test: 'test' })) + } + expect(poolBusy).toEqual(numberOfWorkers) + }) + it('Verify that is possible to have a worker that return undefined', async () => { const result = await emptyPool.execute() expect(result).toBeFalsy() @@ -82,7 +98,7 @@ describe('Fixed cluster pool test suite ', () => { inError = e } expect(inError).toBeDefined() - expect(typeof inError === 'string').toBeTruthy() + expect(typeof inError === 'string').toEqual(true) expect(inError).toBe('Error Message from ClusterWorker') }) @@ -95,7 +111,7 @@ describe('Fixed cluster pool test suite ', () => { inError = e } expect(inError).toBeDefined() - expect(typeof inError === 'string').toBeTruthy() + expect(typeof inError === 'string').toEqual(true) expect(inError).toBe('Error Message from ClusterWorker:async') }) @@ -109,33 +125,11 @@ describe('Fixed cluster pool test suite ', () => { expect(usedTime).toBeGreaterThanOrEqual(2000) }) - it('Verify that maxTasks is set properly', async () => { - const worker = asyncPool.chooseWorker() - expect(worker.getMaxListeners()).toBe(maxTasks) - }) - it('Shutdown test', async () => { - let closedWorkers = 0 - pool.workers.forEach(w => { - w.on('exit', () => { - closedWorkers++ - }) - }) + const exitPromise = TestUtils.waitExits(pool, numberOfWorkers) await pool.destroy() - await new Promise(resolve => setTimeout(resolve, 500)) - expect(closedWorkers).toBe(numberOfWorkers) - }) - - it('Validations test', () => { - let error - try { - const pool1 = new FixedClusterPool() - console.log(pool1) - } catch (e) { - error = e - } - expect(error).toBeTruthy() - expect(error.message).toBeTruthy() + const res = await exitPromise + expect(res).toBe(numberOfWorkers) }) it('Should work even without opts in input', async () => { @@ -145,5 +139,14 @@ describe('Fixed cluster pool test suite ', () => { ) const res = await pool1.execute({ test: 'test' }) expect(res).toBeFalsy() + // We need to clean up the resources after our test + await pool1.destroy() + }) + + it('Verify that a pool with zero worker fails', async () => { + expect( + () => + new FixedClusterPool(0, './tests/worker-files/cluster/testWorker.js') + ).toThrowError(new Error('Cannot instantiate a fixed pool with no worker')) }) })