Tests enhancement (#172)
[poolifier.git] / tests / pools / thread / fixed.test.js
CommitLineData
506c2a14 1const expect = require('expect')
325f50bc 2const { FixedThreadPool } = require('../../../lib/index')
85a3f8a7 3const TestUtils = require('../../test-utils')
5c5a1fb7 4const numberOfThreads = 10
515e5da7 5const maxTasks = 400
325f50bc 6const pool = new FixedThreadPool(
5c5a1fb7 7 numberOfThreads,
76b1e974 8 './tests/worker-files/thread/testWorker.js',
325f50bc 9 {
e5177d86 10 errorHandler: e => console.error(e)
325f50bc
S
11 }
12)
76b1e974
S
13const emptyPool = new FixedThreadPool(
14 1,
15 './tests/worker-files/thread/emptyWorker.js'
16)
17const echoPool = new FixedThreadPool(
18 1,
19 './tests/worker-files/thread/echoWorker.js'
20)
325f50bc
S
21const errorPool = new FixedThreadPool(
22 1,
76b1e974 23 './tests/worker-files/thread/errorWorker.js',
325f50bc
S
24 {
25 errorHandler: e => console.error(e),
26 onlineHandler: () => console.log('worker is online')
27 }
28)
515e5da7
APA
29const asyncPool = new FixedThreadPool(
30 1,
76b1e974 31 './tests/worker-files/thread/asyncWorker.js',
515e5da7
APA
32 { maxTasks: maxTasks }
33)
506c2a14 34
35describe('Fixed thread pool test suite ', () => {
36 it('Choose worker round robin test', async () => {
37 const results = new Set()
5c5a1fb7 38 for (let i = 0; i < numberOfThreads; i++) {
fa0f5b28 39 results.add(pool.chooseWorker().threadId)
506c2a14 40 }
5c5a1fb7 41 expect(results.size).toBe(numberOfThreads)
506c2a14 42 })
43
44 it('Verify that the function is executed in a worker thread', async () => {
45 const result = await pool.execute({ test: 'test' })
46 expect(result).toBeDefined()
47 expect(result).toBeFalsy()
48 })
49
106744f7 50 it('Verify that is possible to invoke the execute method without input', async () => {
51 const result = await pool.execute()
52 expect(result).toBeDefined()
53 expect(result).toBeFalsy()
54 })
55
56 it('Verify that is possible to have a worker that return undefined', async () => {
57 const result = await emptyPool.execute()
58 expect(result).toBeFalsy()
59 })
60
61 it('Verify that data are sent to the worker correctly', async () => {
62 const data = { f: 10 }
63 const result = await echoPool.execute(data)
64 expect(result).toBeTruthy()
65 expect(result.f).toBe(data.f)
66 })
67
68 it('Verify that error handling is working properly', async () => {
69 const data = { f: 10 }
70 let inError
71 try {
72 await errorPool.execute(data)
73 } catch (e) {
74 inError = e
75 }
76 expect(inError).toBeTruthy()
77 expect(inError instanceof Error).toBeTruthy()
78 expect(inError.message).toBeTruthy()
79 })
80
7784f548 81 it('Verify that async function is working properly', async () => {
82 const data = { f: 10 }
83 const startTime = new Date().getTime()
84 const result = await asyncPool.execute(data)
85 const usedTime = new Date().getTime() - startTime
86 expect(result).toBeTruthy()
87 expect(result.f).toBe(data.f)
32d490eb 88 expect(usedTime).toBeGreaterThanOrEqual(2000)
7784f548 89 })
90
515e5da7
APA
91 it('Verify that maxTasks is set properly', async () => {
92 const worker = asyncPool.chooseWorker()
93 expect(worker.port2.getMaxListeners()).toBe(maxTasks)
94 })
95
506c2a14 96 it('Shutdown test', async () => {
85a3f8a7 97 const exitPromise = TestUtils.waitExits(pool, numberOfThreads)
1f9a5a44 98 await pool.destroy()
85a3f8a7
APA
99 const res = await exitPromise
100 expect(res).toBe(numberOfThreads)
506c2a14 101 })
102
103 it('Should work even without opts in input', async () => {
76b1e974
S
104 const pool1 = new FixedThreadPool(
105 1,
106 './tests/worker-files/thread/testWorker.js'
107 )
506c2a14 108 const res = await pool1.execute({ test: 'test' })
109 expect(res).toBeFalsy()
110 })
111})