X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=tests%2Fpools%2Fthread%2Ffixed.test.js;h=cc60a9b5a021253819445d46ff49fab6e91abe2f;hb=1927ee6758147bb8a2479b987322564cea20992b;hp=ff945a700f6bf6efe85359b260499597c3497a1c;hpb=d56f6cfde3b9eeb2a5e4341b91ec474580a0c07a;p=poolifier.git diff --git a/tests/pools/thread/fixed.test.js b/tests/pools/thread/fixed.test.js index ff945a70..cc60a9b5 100644 --- a/tests/pools/thread/fixed.test.js +++ b/tests/pools/thread/fixed.test.js @@ -1,13 +1,12 @@ const expect = require('expect') const { FixedThreadPool } = require('../../../lib/index') +const TestUtils = require('../../test-utils') const numberOfThreads = 10 -const maxTasks = 400 const pool = new FixedThreadPool( numberOfThreads, './tests/worker-files/thread/testWorker.js', { - errorHandler: e => console.error(e), - onlineHandler: () => console.log('worker is online') + errorHandler: e => console.error(e) } ) const emptyPool = new FixedThreadPool( @@ -28,11 +27,18 @@ const errorPool = new FixedThreadPool( ) const asyncPool = new FixedThreadPool( 1, - './tests/worker-files/thread/asyncWorker.js', - { maxTasks: maxTasks } + './tests/worker-files/thread/asyncWorker.js' ) -describe('Fixed thread pool test suite ', () => { +describe('Fixed thread 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 emptyPool.destroy() + }) + it('Choose worker round robin test', async () => { const results = new Set() for (let i = 0; i < numberOfThreads; i++) { @@ -88,32 +94,11 @@ describe('Fixed thread pool test suite ', () => { expect(usedTime).toBeGreaterThanOrEqual(2000) }) - it('Verify that maxTasks is set properly', async () => { - const worker = asyncPool.chooseWorker() - expect(worker.port2.getMaxListeners()).toBe(maxTasks) - }) - it('Shutdown test', async () => { - let closedThreads = 0 - pool.workers.forEach(w => { - w.on('exit', () => { - closedThreads++ - }) - }) + const exitPromise = TestUtils.waitExits(pool, numberOfThreads) await pool.destroy() - expect(closedThreads).toBe(numberOfThreads) - }) - - it('Validations test', () => { - let error - try { - const pool1 = new FixedThreadPool() - console.log(pool1) - } catch (e) { - error = e - } - expect(error).toBeTruthy() - expect(error.message).toBeTruthy() + const res = await exitPromise + expect(res).toBe(numberOfThreads) }) it('Should work even without opts in input', async () => { @@ -123,5 +108,13 @@ describe('Fixed thread 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 FixedThreadPool(0, './tests/worker-files/thread/testWorker.js') + ).toThrowError(new Error('Cannot instantiate a fixed pool with no worker')) }) })