Add two tests to assess worker options. (#260)
[poolifier.git] / tests / pools / thread / dynamic.test.js
CommitLineData
506c2a14 1const expect = require('expect')
325f50bc 2const { DynamicThreadPool } = require('../../../lib/index')
85a3f8a7 3const TestUtils = require('../../test-utils')
506c2a14 4const min = 1
c719859c 5const max = 3
325f50bc
S
6const pool = new DynamicThreadPool(
7 min,
8 max,
76b1e974 9 './tests/worker-files/thread/testWorker.js',
325f50bc 10 {
e5177d86 11 errorHandler: e => console.error(e)
325f50bc
S
12 }
13)
506c2a14 14
a35560ba 15describe('Dynamic thread pool test suite', () => {
506c2a14 16 it('Verify that the function is executed in a worker thread', async () => {
17 const result = await pool.execute({ test: 'test' })
18 expect(result).toBeDefined()
19 expect(result).toBeFalsy()
20 })
21
22 it('Verify that new workers are created when required, max size is not exceeded and that after a while new workers will die', async () => {
23 const promises = []
7c0ba920
JB
24 let poolBusy = 0
25 pool.emitter.on('busy', () => poolBusy++)
cf9aa6c3 26 for (let i = 0; i < max * 2; i++) {
506c2a14 27 promises.push(pool.execute({ test: 'test' }))
28 }
29 expect(pool.workers.length).toBe(max)
8620fb25 30 expect(poolBusy).toBe(max + 1)
85a3f8a7
APA
31 const res = await TestUtils.waitExits(pool, max - min)
32 expect(res).toBe(max - min)
506c2a14 33 })
34
bcf04003 35 it('Verify scale thread up and down is working', async () => {
36 expect(pool.workers.length).toBe(min)
37 for (let i = 0; i < max * 10; i++) {
38 pool.execute({ test: 'test' })
39 }
40 expect(pool.workers.length).toBe(max)
85a3f8a7 41 await TestUtils.waitExits(pool, max - min)
bcf04003 42 expect(pool.workers.length).toBe(min)
43 for (let i = 0; i < max * 10; i++) {
44 pool.execute({ test: 'test' })
45 }
46 expect(pool.workers.length).toBe(max)
85a3f8a7 47 await TestUtils.waitExits(pool, max - min)
bcf04003 48 expect(pool.workers.length).toBe(min)
49 })
c01733f1 50
506c2a14 51 it('Shutdown test', async () => {
52 let closedThreads = 0
53 pool.workers.forEach(w => {
54 w.on('exit', () => {
55 closedThreads++
56 })
57 })
1f9a5a44 58 await pool.destroy()
506c2a14 59 expect(closedThreads).toBe(min)
60 })
61
8d3782fa
JB
62 it('Validation of inputs test', () => {
63 expect(() => new DynamicThreadPool(min)).toThrowError(
85a3f8a7
APA
64 new Error('Please specify a file with a worker implementation')
65 )
506c2a14 66 })
67
68 it('Should work even without opts in input', async () => {
325f50bc
S
69 const pool1 = new DynamicThreadPool(
70 1,
71 1,
76b1e974 72 './tests/worker-files/thread/testWorker.js'
325f50bc 73 )
506c2a14 74 const res = await pool1.execute({ test: 'test' })
8620fb25 75 expect(res).toBeDefined()
506c2a14 76 expect(res).toBeFalsy()
0e2503fc
JB
77 // We need to clean up the resources after our test
78 await pool1.destroy()
506c2a14 79 })
c01733f1 80
4c35177b 81 it('Verify scale thread up and down is working when long running task is used:hard', async () => {
c01733f1 82 const longRunningPool = new DynamicThreadPool(
83 min,
84 max,
85a3f8a7 85 './tests/worker-files/thread/longRunningWorkerHardBehavior.js',
4c35177b 86 {
87 errorHandler: e => console.error(e),
292ad316
JB
88 onlineHandler: () => console.log('long running worker is online'),
89 exitHandler: () => console.log('long running worker exited')
4c35177b 90 }
91 )
92 expect(longRunningPool.workers.length).toBe(min)
93 for (let i = 0; i < max * 10; i++) {
94 longRunningPool.execute({ test: 'test' })
95 }
96 expect(longRunningPool.workers.length).toBe(max)
85a3f8a7 97 await TestUtils.waitExits(longRunningPool, max - min)
4c35177b 98 expect(longRunningPool.workers.length).toBe(min)
0e2503fc
JB
99 // We need to clean up the resources after our test
100 await longRunningPool.destroy()
4c35177b 101 })
102
103 it('Verify scale thread up and down is working when long running task is used:soft', async () => {
104 const longRunningPool = new DynamicThreadPool(
105 min,
106 max,
85a3f8a7 107 './tests/worker-files/thread/longRunningWorkerSoftBehavior.js',
c01733f1 108 {
109 errorHandler: e => console.error(e),
292ad316
JB
110 onlineHandler: () => console.log('long running worker is online'),
111 exitHandler: () => console.log('long running worker exited')
c01733f1 112 }
113 )
114 expect(longRunningPool.workers.length).toBe(min)
115 for (let i = 0; i < max * 10; i++) {
116 longRunningPool.execute({ test: 'test' })
117 }
118 expect(longRunningPool.workers.length).toBe(max)
85a3f8a7 119 await TestUtils.sleep(1500)
c01733f1 120 // Here we expect the workers to be at the max size since that the task is still running
121 expect(longRunningPool.workers.length).toBe(max)
0e2503fc
JB
122 // We need to clean up the resources after our test
123 await longRunningPool.destroy()
c01733f1 124 })
8d3782fa
JB
125
126 it('Verify that a pool with zero worker can be instantiated', async () => {
127 const pool = new DynamicThreadPool(
128 0,
129 max,
130 './tests/worker-files/thread/testWorker.js'
131 )
132 expect(pool).toBeInstanceOf(DynamicThreadPool)
133 // We need to clean up the resources after our test
134 await pool.destroy()
135 })
506c2a14 136})