fix: fix worker task functions handling
[poolifier.git] / tests / worker / abstract-worker.test.js
1 const { expect } = require('expect')
2 const { ClusterWorker, KillBehaviors, ThreadWorker } = require('../../lib')
3
4 describe('Abstract worker test suite', () => {
5 class StubWorkerWithMainWorker extends ThreadWorker {
6 constructor (fn, opts) {
7 super(fn, opts)
8 this.mainWorker = undefined
9 }
10 }
11
12 it('Verify worker options default values', () => {
13 const worker = new ThreadWorker(() => {})
14 expect(worker.opts.maxInactiveTime).toStrictEqual(60000)
15 expect(worker.opts.killBehavior).toBe(KillBehaviors.SOFT)
16 expect(worker.opts.async).toBe(undefined)
17 })
18
19 it('Verify that worker options are set at worker creation', () => {
20 const worker = new ClusterWorker(() => {}, {
21 maxInactiveTime: 6000,
22 async: true,
23 killBehavior: KillBehaviors.HARD
24 })
25 expect(worker.opts.maxInactiveTime).toStrictEqual(6000)
26 expect(worker.opts.killBehavior).toBe(KillBehaviors.HARD)
27 expect(worker.opts.async).toBe(undefined)
28 })
29
30 it('Verify that taskFunctions parameter is mandatory', () => {
31 expect(() => new ClusterWorker()).toThrowError(
32 'taskFunctions parameter is mandatory'
33 )
34 })
35
36 it('Verify that taskFunctions parameter is a function or a plain object', () => {
37 expect(() => new ClusterWorker(0)).toThrowError(
38 new TypeError(
39 'taskFunctions parameter is not a function or a plain object'
40 )
41 )
42 expect(() => new ClusterWorker('')).toThrowError(
43 new TypeError(
44 'taskFunctions parameter is not a function or a plain object'
45 )
46 )
47 expect(() => new ClusterWorker(true)).toThrowError(
48 new TypeError(
49 'taskFunctions parameter is not a function or a plain object'
50 )
51 )
52 expect(() => new ClusterWorker([])).toThrowError(
53 new TypeError(
54 'taskFunctions parameter is not a function or a plain object'
55 )
56 )
57 expect(() => new ClusterWorker(new Map())).toThrowError(
58 new TypeError(
59 'taskFunctions parameter is not a function or a plain object'
60 )
61 )
62 expect(() => new ClusterWorker(new Set())).toThrowError(
63 new TypeError(
64 'taskFunctions parameter is not a function or a plain object'
65 )
66 )
67 expect(() => new ClusterWorker(new WeakMap())).toThrowError(
68 new TypeError(
69 'taskFunctions parameter is not a function or a plain object'
70 )
71 )
72 expect(() => new ClusterWorker(new WeakSet())).toThrowError(
73 new TypeError(
74 'taskFunctions parameter is not a function or a plain object'
75 )
76 )
77 })
78
79 it('Verify that taskFunctions parameter is not an empty object', () => {
80 expect(() => new ClusterWorker({})).toThrowError(
81 new Error('taskFunctions parameter object is empty')
82 )
83 })
84
85 it('Verify that taskFunctions parameter with unique function is taken', () => {
86 const worker = new ThreadWorker(() => {})
87 expect(worker.taskFunctions.get('default')).toBeInstanceOf(Function)
88 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
89 expect(worker.taskFunctions.size).toBe(2)
90 expect(worker.taskFunctions.get('default')).toStrictEqual(
91 worker.taskFunctions.get('fn1')
92 )
93 })
94
95 it('Verify that taskFunctions parameter with multiple task functions contains function', () => {
96 const fn1 = () => {
97 return 1
98 }
99 const fn2 = ''
100 expect(() => new ThreadWorker({ fn1, fn2 })).toThrowError(
101 new TypeError('A taskFunctions parameter object value is not a function')
102 )
103 })
104
105 it('Verify that taskFunctions parameter with multiple task functions is taken', () => {
106 const fn1 = () => {
107 return 1
108 }
109 const fn2 = () => {
110 return 2
111 }
112 const worker = new ClusterWorker({ fn1, fn2 })
113 expect(worker.taskFunctions.get('default')).toBeInstanceOf(Function)
114 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
115 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
116 expect(worker.taskFunctions.size).toBe(3)
117 expect(worker.taskFunctions.get('default')).toStrictEqual(
118 worker.taskFunctions.get('fn1')
119 )
120 })
121
122 it('Verify that handleError() method works properly', () => {
123 const error = new Error('Error as an error')
124 const worker = new ClusterWorker(() => {})
125 expect(worker.handleError(error)).not.toBeInstanceOf(Error)
126 expect(worker.handleError(error)).toStrictEqual(error.message)
127 const errorMessage = 'Error as a string'
128 expect(worker.handleError(errorMessage)).toStrictEqual(errorMessage)
129 })
130
131 it('Verify that getMainWorker() throw error if main worker is not set', () => {
132 expect(() =>
133 new StubWorkerWithMainWorker(() => {}).getMainWorker()
134 ).toThrowError('Main worker not set')
135 })
136
137 it('Verify that hasTaskFunction() works', () => {
138 const fn1 = () => {
139 return 1
140 }
141 const fn2 = () => {
142 return 2
143 }
144 const worker = new ClusterWorker({ fn1, fn2 })
145 expect(worker.hasTaskFunction('default')).toBe(true)
146 expect(worker.hasTaskFunction('fn1')).toBe(true)
147 expect(worker.hasTaskFunction('fn2')).toBe(true)
148 expect(worker.hasTaskFunction('fn3')).toBe(false)
149 })
150
151 it('Verify that addTaskFunction() works', () => {
152 const fn1 = () => {
153 return 1
154 }
155 const fn2 = () => {
156 return 2
157 }
158 const fn1Replacement = () => {
159 return 3
160 }
161 const worker = new ThreadWorker(fn1)
162 expect(worker.taskFunctions.get('default')).toBeInstanceOf(Function)
163 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
164 expect(worker.taskFunctions.size).toBe(2)
165 expect(worker.taskFunctions.get('default')).toStrictEqual(
166 worker.taskFunctions.get('fn1')
167 )
168 expect(() => worker.addTaskFunction('default', fn2)).toThrowError(
169 new Error('Cannot add a task function with the default reserved name')
170 )
171 worker.addTaskFunction('fn2', fn2)
172 expect(worker.taskFunctions.get('default')).toBeInstanceOf(Function)
173 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
174 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
175 expect(worker.taskFunctions.size).toBe(3)
176 expect(worker.taskFunctions.get('default')).toStrictEqual(
177 worker.taskFunctions.get('fn1')
178 )
179 worker.addTaskFunction('fn1', fn1Replacement)
180 expect(worker.taskFunctions.get('default')).toBeInstanceOf(Function)
181 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
182 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
183 expect(worker.taskFunctions.size).toBe(3)
184 expect(worker.taskFunctions.get('default')).toStrictEqual(
185 worker.taskFunctions.get('fn1')
186 )
187 })
188
189 it('Verify that removeTaskFunction() works', () => {
190 const fn1 = () => {
191 return 1
192 }
193 const fn2 = () => {
194 return 2
195 }
196 const worker = new ThreadWorker({ fn1, fn2 })
197 expect(worker.taskFunctions.get('default')).toBeInstanceOf(Function)
198 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
199 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
200 expect(worker.taskFunctions.size).toBe(3)
201 expect(worker.taskFunctions.get('default')).toStrictEqual(
202 worker.taskFunctions.get('fn1')
203 )
204 expect(() => worker.removeTaskFunction('default')).toThrowError(
205 new Error(
206 'Cannot remove the task function with the default reserved name'
207 )
208 )
209 expect(() => worker.removeTaskFunction('fn1')).toThrowError(
210 new Error(
211 'Cannot remove the task function used as the default task function'
212 )
213 )
214 worker.removeTaskFunction('fn2')
215 expect(worker.taskFunctions.get('default')).toBeInstanceOf(Function)
216 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
217 expect(worker.taskFunctions.get('fn2')).toBeUndefined()
218 expect(worker.taskFunctions.size).toBe(2)
219 })
220
221 it('Verify that setDefaultTaskFunction() works', () => {
222 const fn1 = () => {
223 return 1
224 }
225 const fn2 = () => {
226 return 2
227 }
228 const worker = new ThreadWorker({ fn1, fn2 })
229 expect(worker.taskFunctions.get('default')).toBeInstanceOf(Function)
230 expect(worker.taskFunctions.get('fn1')).toBeInstanceOf(Function)
231 expect(worker.taskFunctions.get('fn2')).toBeInstanceOf(Function)
232 expect(worker.taskFunctions.size).toBe(3)
233 expect(worker.taskFunctions.get('default')).toStrictEqual(
234 worker.taskFunctions.get('fn1')
235 )
236 expect(() => worker.setDefaultTaskFunction('default')).toThrowError(
237 new Error(
238 'Cannot set the default task function reserved name as the default task function'
239 )
240 )
241 worker.setDefaultTaskFunction('fn1')
242 expect(worker.taskFunctions.get('default')).toStrictEqual(
243 worker.taskFunctions.get('fn1')
244 )
245 worker.setDefaultTaskFunction('fn2')
246 expect(worker.taskFunctions.get('default')).toStrictEqual(
247 worker.taskFunctions.get('fn2')
248 )
249 })
250 })