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 multiple task functions contains function', () => {
90 expect(() => new ThreadWorker({ fn1
, fn2
})).toThrowError(
91 new TypeError('A taskFunctions parameter object value is not a function')
95 it('Verify that taskFunctions parameter with multiple task functions is taken', () => {
102 const worker
= new ClusterWorker({ fn1
, fn2
})
103 expect(typeof worker
.taskFunctions
.get('default') === 'function').toBe(true)
104 expect(typeof worker
.taskFunctions
.get('fn1') === 'function').toBe(true)
105 expect(typeof worker
.taskFunctions
.get('fn2') === 'function').toBe(true)
108 it('Verify that handleError() method is working properly', () => {
109 const error
= new Error('My error')
110 const worker
= new ThreadWorker(() => {})
111 expect(worker
.handleError(error
)).toStrictEqual(error
)
114 it('Verify that getMainWorker() throw error if main worker is not set', () => {
116 new StubWorkerWithMainWorker(() => {}).getMainWorker()
117 ).toThrowError('Main worker was not set')