Merge pull request #876 from poolifier/dependabot/npm_and_yarn/examples/typescript...
[poolifier.git] / tests / pools / thread / dynamic.test.js
... / ...
CommitLineData
1const { expect } = require('expect')
2const { DynamicThreadPool, PoolEvents } = require('../../../lib')
3const { TaskFunctions } = require('../../test-types')
4const { sleep, waitWorkerEvents } = require('../../test-utils')
5
6describe('Dynamic thread pool test suite', () => {
7 const min = 1
8 const max = 3
9 const pool = new DynamicThreadPool(
10 min,
11 max,
12 './tests/worker-files/thread/testWorker.js',
13 {
14 errorHandler: (e) => console.error(e)
15 }
16 )
17
18 it('Verify that the function is executed in a worker thread', async () => {
19 let result = await pool.execute({
20 function: TaskFunctions.fibonacci
21 })
22 expect(result).toBe(75025)
23 result = await pool.execute({
24 function: TaskFunctions.factorial
25 })
26 expect(result).toBe(9.33262154439441e157)
27 })
28
29 it('Verify that new workers are created when required, max size is not exceeded and that after a while new workers will die', async () => {
30 let poolBusy = 0
31 pool.emitter.on(PoolEvents.busy, () => ++poolBusy)
32 for (let i = 0; i < max * 2; i++) {
33 pool.execute()
34 }
35 expect(pool.workerNodes.length).toBeLessThanOrEqual(max)
36 expect(pool.workerNodes.length).toBeGreaterThan(min)
37 expect(poolBusy).toBe(1)
38 const numberOfExitEvents = await waitWorkerEvents(pool, 'exit', max - min)
39 expect(numberOfExitEvents).toBe(max - min)
40 })
41
42 it('Verify scale thread up and down is working', async () => {
43 expect(pool.workerNodes.length).toBe(min)
44 for (let i = 0; i < max * 2; i++) {
45 pool.execute()
46 }
47 expect(pool.workerNodes.length).toBe(max)
48 await waitWorkerEvents(pool, 'exit', max - min)
49 expect(pool.workerNodes.length).toBe(min)
50 for (let i = 0; i < max * 2; i++) {
51 pool.execute()
52 }
53 expect(pool.workerNodes.length).toBe(max)
54 await waitWorkerEvents(pool, 'exit', max - min)
55 expect(pool.workerNodes.length).toBe(min)
56 })
57
58 it('Shutdown test', async () => {
59 const exitPromise = waitWorkerEvents(pool, 'exit', min)
60 let poolDestroy = 0
61 pool.emitter.on(PoolEvents.destroy, () => ++poolDestroy)
62 await pool.destroy()
63 const numberOfExitEvents = await exitPromise
64 expect(numberOfExitEvents).toBe(min)
65 expect(poolDestroy).toBe(1)
66 })
67
68 it('Validation of inputs test', () => {
69 expect(() => new DynamicThreadPool(min)).toThrowError(
70 'Please specify a file with a worker implementation'
71 )
72 })
73
74 it('Should work even without opts in input', async () => {
75 const pool1 = new DynamicThreadPool(
76 min,
77 max,
78 './tests/worker-files/thread/testWorker.js'
79 )
80 const res = await pool1.execute()
81 expect(res).toStrictEqual({ ok: 1 })
82 // We need to clean up the resources after our test
83 await pool1.destroy()
84 })
85
86 it('Verify scale thread up and down is working when long executing task is used:hard', async () => {
87 const longRunningPool = new DynamicThreadPool(
88 min,
89 max,
90 './tests/worker-files/thread/longRunningWorkerHardBehavior.js',
91 {
92 errorHandler: (e) => console.error(e),
93 onlineHandler: () => console.info('long executing worker is online'),
94 exitHandler: () => console.info('long executing worker exited')
95 }
96 )
97 expect(longRunningPool.workerNodes.length).toBe(min)
98 for (let i = 0; i < max * 2; i++) {
99 longRunningPool.execute()
100 }
101 expect(longRunningPool.workerNodes.length).toBe(max)
102 await waitWorkerEvents(longRunningPool, 'exit', max - min)
103 expect(longRunningPool.workerNodes.length).toBe(min)
104 expect(
105 longRunningPool.workerChoiceStrategyContext.workerChoiceStrategies.get(
106 longRunningPool.workerChoiceStrategyContext.workerChoiceStrategy
107 ).nextWorkerNodeKey
108 ).toBeLessThan(longRunningPool.workerNodes.length)
109 // We need to clean up the resources after our test
110 await longRunningPool.destroy()
111 })
112
113 it('Verify scale thread up and down is working when long executing task is used:soft', async () => {
114 const longRunningPool = new DynamicThreadPool(
115 min,
116 max,
117 './tests/worker-files/thread/longRunningWorkerSoftBehavior.js',
118 {
119 errorHandler: (e) => console.error(e),
120 onlineHandler: () => console.info('long executing worker is online'),
121 exitHandler: () => console.info('long executing worker exited')
122 }
123 )
124 expect(longRunningPool.workerNodes.length).toBe(min)
125 for (let i = 0; i < max * 2; i++) {
126 longRunningPool.execute()
127 }
128 expect(longRunningPool.workerNodes.length).toBe(max)
129 await sleep(1000)
130 // Here we expect the workerNodes to be at the max size since the task is still executing
131 expect(longRunningPool.workerNodes.length).toBe(max)
132 // We need to clean up the resources after our test
133 await longRunningPool.destroy()
134 })
135
136 it('Verify that a pool with zero worker can be instantiated', async () => {
137 const pool = new DynamicThreadPool(
138 0,
139 max,
140 './tests/worker-files/thread/testWorker.js'
141 )
142 expect(pool).toBeInstanceOf(DynamicThreadPool)
143 // We need to clean up the resources after our test
144 await pool.destroy()
145 })
146})