X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=tests%2Fdynamic.test.js;h=e8be74ace413c874a008dbd846c2ec98811d46cb;hb=10c266b8490b5abb134d4aafb6a762a3c4a2a682;hp=7b0865bb9f951c9c038f2c0c80f6bf6440e81656;hpb=766e51a0c50e4aadad5f8c65a2b6689d91ba75d8;p=poolifier.git diff --git a/tests/dynamic.test.js b/tests/dynamic.test.js index 7b0865bb..e8be74ac 100644 --- a/tests/dynamic.test.js +++ b/tests/dynamic.test.js @@ -1,9 +1,9 @@ const expect = require('expect') const DynamicThreadPool = require('../lib/dynamic') const min = 1 -const max = 10 +const max = 3 const pool = new DynamicThreadPool(min, max, - './tests/testWorker.js', + './tests/workers/testWorker.js', { errorHandler: (e) => console.error(e), onlineHandler: () => console.log('worker is online') }) describe('Dynamic thread pool test suite ', () => { @@ -16,7 +16,9 @@ describe('Dynamic thread pool test suite ', () => { it('Verify that new workers are created when required, max size is not exceeded and that after a while new workers will die', async () => { const promises = [] let closedThreads = 0 - for (let i = 0; i < (max * 3); i++) { + let fullPool = 0 + pool.emitter.on('FullPool', () => fullPool++) + for (let i = 0; i < (max * 2); i++) { promises.push(pool.execute({ test: 'test' })) } expect(pool.workers.length).toBe(max) @@ -25,7 +27,8 @@ describe('Dynamic thread pool test suite ', () => { closedThreads++ }) }) - await new Promise(resolve => setTimeout(resolve, 1000 * 2)) + expect(fullPool > 1).toBeTruthy() + await new Promise(resolve => setTimeout(resolve, 2000)) expect(closedThreads).toBe(max - min) }) @@ -36,8 +39,7 @@ describe('Dynamic thread pool test suite ', () => { closedThreads++ }) }) - pool.destroy() - await new Promise(resolve => setTimeout(resolve, 1000)) + await pool.destroy() expect(closedThreads).toBe(min) }) @@ -54,7 +56,7 @@ describe('Dynamic thread pool test suite ', () => { }) it('Should work even without opts in input', async () => { - const pool1 = new DynamicThreadPool(1, 1, './tests/testWorker.js') + const pool1 = new DynamicThreadPool(1, 1, './tests/workers/testWorker.js') const res = await pool1.execute({ test: 'test' }) expect(res).toBeFalsy() })