1 const { expect
} = require('expect')
2 const { ClusterWorker
, KillBehaviors
, ThreadWorker
} = require('../../lib')
4 describe('Abstract worker test suite', () => {
5 class StubPoolWithIsMainWorker
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(false)
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(true)
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 an object', () => {
37 expect(() => new ClusterWorker(0)).toThrowError(
38 new TypeError('taskFunctions parameter is not a function or an object')
40 expect(() => new ClusterWorker('')).toThrowError(
41 new TypeError('taskFunctions parameter is not a function or an object')
43 expect(() => new ClusterWorker(true)).toThrowError(
44 new TypeError('taskFunctions parameter is not a function or an object')
48 it('Verify that taskFunctions parameter is an object literal', () => {
49 expect(() => new ClusterWorker([])).toThrowError(
50 new TypeError('taskFunctions parameter is not an object literal')
52 expect(() => new ClusterWorker(new Map())).toThrowError(
53 new TypeError('taskFunctions parameter is not an object literal')
55 expect(() => new ClusterWorker(new Set())).toThrowError(
56 new TypeError('taskFunctions parameter is not an object literal')
58 expect(() => new ClusterWorker(new WeakMap())).toThrowError(
59 new TypeError('taskFunctions parameter is not an object literal')
61 expect(() => new ClusterWorker(new WeakSet())).toThrowError(
62 new TypeError('taskFunctions parameter is not an object literal')
66 it('Verify that taskFunctions parameter with multiple task functions is taken', () => {
73 const worker
= new ClusterWorker({ fn1
, fn2
})
74 expect(typeof worker
.taskFunctions
.get('fn1') === 'function').toBe(true)
75 expect(typeof worker
.taskFunctions
.get('fn2') === 'function').toBe(true)
78 it('Verify that handleError() method is working properly', () => {
79 const error
= new Error('My error')
80 const worker
= new ThreadWorker(() => {})
81 expect(worker
.handleError(error
)).toStrictEqual(error
)
84 it('Verify that getMainWorker() throw error if main worker is not set', () => {
86 new StubPoolWithIsMainWorker(() => {}).getMainWorker()
87 ).toThrowError('Main worker was not set')