Ligth number of threads
[poolifier.git] / tests / fixed.test.js
1 const expect = require('expect')
2 const FixedThreadPool = require('../lib/fixed')
3 const numThreads = 10
4 const pool = new FixedThreadPool(numThreads,
5 './tests/testWorker.js',
6 { errorHandler: (e) => console.error(e), onlineHandler: () => console.log('worker is online') })
7
8 describe('Fixed thread pool test suite ', () => {
9 it('Choose worker round robin test', async () => {
10 const results = new Set()
11 for (let i = 0; i < numThreads; i++) {
12 results.add(pool._chooseWorker().threadId)
13 }
14 expect(results.size).toBe(numThreads)
15 })
16
17 it('Verify that the function is executed in a worker thread', async () => {
18 const result = await pool.execute({ test: 'test' })
19 expect(result).toBeDefined()
20 expect(result).toBeFalsy()
21 })
22
23 it('Shutdown test', async () => {
24 let closedThreads = 0
25 pool.workers.forEach(w => {
26 w.on('exit', () => {
27 closedThreads++
28 })
29 })
30 pool.destroy()
31 await new Promise(resolve => setTimeout(resolve, 1000))
32 expect(closedThreads).toBe(numThreads)
33 })
34
35 it('Validations test', () => {
36 let error
37 try {
38 const pool1 = new FixedThreadPool()
39 console.log(pool1)
40 } catch (e) {
41 error = e
42 }
43 expect(error).toBeTruthy()
44 expect(error.message).toBeTruthy()
45 })
46
47 it('Should work even without opts in input', async () => {
48 const pool1 = new FixedThreadPool(1, './tests/testWorker.js')
49 const res = await pool1.execute({ test: 'test' })
50 expect(res).toBeFalsy()
51 })
52 })