X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=tests%2Ffixed.test.js;h=2ed97444bdb94a98f793f1833d027c0070f4b14b;hb=60400df2370f4fc2fb00357f94b7ad0a0098959b;hp=78e960bf547b8a65f91bdc6577765439f0021181;hpb=766e51a0c50e4aadad5f8c65a2b6689d91ba75d8;p=poolifier.git diff --git a/tests/fixed.test.js b/tests/fixed.test.js index 78e960bf..2ed97444 100644 --- a/tests/fixed.test.js +++ b/tests/fixed.test.js @@ -2,8 +2,12 @@ const expect = require('expect') const FixedThreadPool = require('../lib/fixed') const numThreads = 10 const pool = new FixedThreadPool(numThreads, - './tests/testWorker.js', + './tests/workers/testWorker.js', { errorHandler: (e) => console.error(e), onlineHandler: () => console.log('worker is online') }) +const emptyPool = new FixedThreadPool(1, './tests/workers/emptyWorker.js') +const echoPool = new FixedThreadPool(1, './tests/workers/echoWorker.js') +const errorPool = new FixedThreadPool(1, './tests/workers/errorWorker.js', { errorHandler: (e) => console.error(e), onlineHandler: () => console.log('worker is online') }) +const asyncPool = new FixedThreadPool(1, './tests/workers/asyncWorker.js') describe('Fixed thread pool test suite ', () => { it('Choose worker round robin test', async () => { @@ -20,6 +24,47 @@ describe('Fixed thread pool test suite ', () => { expect(result).toBeFalsy() }) + it('Verify that is possible to invoke the execute method without input', async () => { + const result = await pool.execute() + expect(result).toBeDefined() + expect(result).toBeFalsy() + }) + + it('Verify that is possible to have a worker that return undefined', async () => { + const result = await emptyPool.execute() + expect(result).toBeFalsy() + }) + + it('Verify that data are sent to the worker correctly', async () => { + const data = { f: 10 } + const result = await echoPool.execute(data) + expect(result).toBeTruthy() + expect(result.f).toBe(data.f) + }) + + it('Verify that error handling is working properly', async () => { + const data = { f: 10 } + let inError + try { + await errorPool.execute(data) + } catch (e) { + inError = e + } + expect(inError).toBeTruthy() + expect(inError instanceof Error).toBeTruthy() + expect(inError.message).toBeTruthy() + }) + + it('Verify that async function is working properly', async () => { + const data = { f: 10 } + const startTime = new Date().getTime() + const result = await asyncPool.execute(data) + const usedTime = new Date().getTime() - startTime + expect(result).toBeTruthy() + expect(result.f).toBe(data.f) + expect(usedTime).toBeGreaterThanOrEqual(2000) + }) + it('Shutdown test', async () => { let closedThreads = 0 pool.workers.forEach(w => { @@ -27,8 +72,7 @@ describe('Fixed thread pool test suite ', () => { closedThreads++ }) }) - pool.destroy() - await new Promise(resolve => setTimeout(resolve, 1000)) + await pool.destroy() expect(closedThreads).toBe(numThreads) }) @@ -45,7 +89,7 @@ describe('Fixed thread pool test suite ', () => { }) it('Should work even without opts in input', async () => { - const pool1 = new FixedThreadPool(1, './tests/testWorker.js') + const pool1 = new FixedThreadPool(1, './tests/workers/testWorker.js') const res = await pool1.execute({ test: 'test' }) expect(res).toBeFalsy() })