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 that fn parameter is mandatory', () => {
13 expect(() => new ClusterWorker()).toThrowError(
14 new Error('fn parameter is mandatory')
18 it('Verify that fn parameter is a function', () => {
19 expect(() => new ClusterWorker({})).toThrowError(
20 new TypeError('fn parameter is not a function')
24 it('Verify worker options default values', () => {
25 const worker
= new ThreadWorker(() => {})
26 expect(worker
.opts
.maxInactiveTime
).toStrictEqual(60000)
27 expect(worker
.opts
.killBehavior
).toBe(KillBehaviors
.SOFT
)
28 expect(worker
.opts
.async
).toBe(false)
31 it('Verify that worker options are set at worker creation', () => {
32 const worker
= new ClusterWorker(() => {}, {
33 maxInactiveTime
: 6000,
35 killBehavior
: KillBehaviors
.HARD
37 expect(worker
.opts
.maxInactiveTime
).toStrictEqual(6000)
38 expect(worker
.opts
.killBehavior
).toBe(KillBehaviors
.HARD
)
39 expect(worker
.opts
.async
).toBe(true)
42 it('Verify that handleError function is working properly', () => {
43 const error
= new Error('My error')
44 const worker
= new ThreadWorker(() => {})
45 expect(worker
.handleError(error
)).toStrictEqual(error
)
48 it('Verify that get main worker throw error if main worker is not set', () => {
50 new StubPoolWithIsMainWorker(() => {}).getMainWorker()
51 ).toThrowError(new Error('Main worker was not set'))