1 const { expect
} = require('expect')
2 const { ClusterWorker
, KillBehaviors
, ThreadWorker
} = require('../../lib')
4 describe('Abstract worker test suite', () => {
5 class StubWorkerWithMainWorker
extends ThreadWorker
{
6 constructor (fn
, opts
) {
8 this.mainWorker
= undefined
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)
19 it('Verify that worker options are set at worker creation', () => {
20 const worker
= new ClusterWorker(() => {}, {
21 maxInactiveTime
: 6000,
23 killBehavior
: KillBehaviors
.HARD
25 expect(worker
.opts
.maxInactiveTime
).toStrictEqual(6000)
26 expect(worker
.opts
.killBehavior
).toBe(KillBehaviors
.HARD
)
27 expect(worker
.opts
.async
).toBe(undefined)
30 it('Verify that taskFunctions parameter is mandatory', () => {
31 expect(() => new ClusterWorker()).toThrowError(
32 'taskFunctions parameter is mandatory'
36 it('Verify that taskFunctions parameter is a function or a plain object', () => {
37 expect(() => new ClusterWorker(0)).toThrowError(
39 'taskFunctions parameter is not a function or a plain object'
42 expect(() => new ClusterWorker('')).toThrowError(
44 'taskFunctions parameter is not a function or a plain object'
47 expect(() => new ClusterWorker(true)).toThrowError(
49 'taskFunctions parameter is not a function or a plain object'
52 expect(() => new ClusterWorker([])).toThrowError(
54 'taskFunctions parameter is not a function or a plain object'
57 expect(() => new ClusterWorker(new Map())).toThrowError(
59 'taskFunctions parameter is not a function or a plain object'
62 expect(() => new ClusterWorker(new Set())).toThrowError(
64 'taskFunctions parameter is not a function or a plain object'
67 expect(() => new ClusterWorker(new WeakMap())).toThrowError(
69 'taskFunctions parameter is not a function or a plain object'
72 expect(() => new ClusterWorker(new WeakSet())).toThrowError(
74 'taskFunctions parameter is not a function or a plain object'
79 it('Verify that taskFunctions parameter is not an empty object', () => {
80 expect(() => new ClusterWorker({})).toThrowError(
81 new Error('taskFunctions parameter object is empty')
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')
95 it('Verify that taskFunctions parameter with multiple task functions contains function', () => {
100 expect(() => new ThreadWorker({ fn1
, fn2
})).toThrowError(
101 new TypeError('A taskFunctions parameter object value is not a function')
105 it('Verify that taskFunctions parameter with multiple task functions is taken', () => {
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')
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
)
131 it('Verify that getMainWorker() throw error if main worker is not set', () => {
133 new StubWorkerWithMainWorker(() => {}).getMainWorker()
134 ).toThrowError('Main worker not set')
137 it('Verify that hasTaskFunction() works', () => {
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)
151 it('Verify that addTaskFunction() works', () => {
158 const fn1Replacement
= () => {
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')
168 expect(() => worker
.addTaskFunction('default', fn2
)).toThrowError(
169 new Error('Cannot add a task function with the default reserved name')
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')
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')
189 it('Verify that removeTaskFunction() works', () => {
196 const worker
= new ClusterWorker({ 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')
204 expect(() => worker
.removeTaskFunction('default')).toThrowError(
206 'Cannot remove the task function with the default reserved name'
209 expect(() => worker
.removeTaskFunction('fn1')).toThrowError(
211 'Cannot remove the task function used as the default task function'
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)
221 it('Verify that listTaskFunctions() works', () => {
228 const worker
= new ClusterWorker({ fn1
, fn2
})
229 expect(worker
.listTaskFunctions()).toStrictEqual(['default', 'fn1', 'fn2'])
232 it('Verify that setDefaultTaskFunction() works', () => {
239 const worker
= new ThreadWorker({ fn1
, fn2
})
240 expect(worker
.taskFunctions
.get('default')).toBeInstanceOf(Function
)
241 expect(worker
.taskFunctions
.get('fn1')).toBeInstanceOf(Function
)
242 expect(worker
.taskFunctions
.get('fn2')).toBeInstanceOf(Function
)
243 expect(worker
.taskFunctions
.size
).toBe(3)
244 expect(worker
.taskFunctions
.get('default')).toStrictEqual(
245 worker
.taskFunctions
.get('fn1')
247 expect(() => worker
.setDefaultTaskFunction('default')).toThrowError(
249 'Cannot set the default task function reserved name as the default task function'
252 worker
.setDefaultTaskFunction('fn1')
253 expect(worker
.taskFunctions
.get('default')).toStrictEqual(
254 worker
.taskFunctions
.get('fn1')
256 worker
.setDefaultTaskFunction('fn2')
257 expect(worker
.taskFunctions
.get('default')).toStrictEqual(
258 worker
.taskFunctions
.get('fn2')