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 fn parameter is mandatory', () => {
31 expect(() => new ClusterWorker()).toThrowError('fn parameter is mandatory')
34 it('Verify that fn parameter is a function', () => {
35 expect(() => new ClusterWorker({})).toThrowError(
36 new TypeError('fn parameter is not a function')
38 expect(() => new ClusterWorker('')).toThrowError(
39 new TypeError('fn parameter is not a function')
43 it('Verify that async fn parameter without async option throw error', () => {
44 const fn
= async () => {
47 expect(() => new ClusterWorker(fn
)).toThrowError(
48 'fn parameter is an async function, please set the async option to true'
52 it('Verify that handleError() method is working properly', () => {
53 const error
= new Error('My error')
54 const worker
= new ThreadWorker(() => {})
55 expect(worker
.handleError(error
)).toStrictEqual(error
)
58 it('Verify that getMainWorker() throw error if main worker is not set', () => {
60 new StubPoolWithIsMainWorker(() => {}).getMainWorker()
61 ).toThrowError('Main worker was not set')