Apply dependencies update
[poolifier.git] / tests / worker / abstract-worker.test.js
CommitLineData
a61a0724 1const { expect } = require('expect')
8620fb25 2const { ClusterWorker, KillBehaviors, ThreadWorker } = require('../../lib')
7fc5cce6 3
e1ffb94f
JB
4describe('Abstract worker test suite', () => {
5 class StubPoolWithIsMainWorker extends ThreadWorker {
6 constructor (fn, opts) {
7 super(fn, opts)
78cea37e 8 this.mainWorker = undefined
e1ffb94f 9 }
7fc5cce6 10 }
c510fea7 11
78cea37e 12 it('Verify that fn parameter is mandatory', () => {
8d3782fa
JB
13 expect(() => new ClusterWorker()).toThrowError(
14 new Error('fn parameter is mandatory')
15 )
c510fea7 16 })
7fc5cce6 17
78cea37e
JB
18 it('Verify that fn parameter is a function', () => {
19 expect(() => new ClusterWorker({})).toThrowError(
20 new TypeError('fn parameter is not a function')
21 )
22 })
23
e088a00c 24 it('Verify worker options default values', () => {
8620fb25 25 const worker = new ThreadWorker(() => {})
978aad6f 26 expect(worker.opts.maxInactiveTime).toStrictEqual(60000)
e088a00c
JB
27 expect(worker.opts.killBehavior).toBe(KillBehaviors.SOFT)
28 expect(worker.opts.async).toBe(false)
8620fb25
JB
29 })
30
31 it('Verify that worker options are set at worker creation', () => {
32 const worker = new ClusterWorker(() => {}, {
33 maxInactiveTime: 6000,
34 async: true,
35 killBehavior: KillBehaviors.HARD
36 })
978aad6f 37 expect(worker.opts.maxInactiveTime).toStrictEqual(6000)
e088a00c
JB
38 expect(worker.opts.killBehavior).toBe(KillBehaviors.HARD)
39 expect(worker.opts.async).toBe(true)
8620fb25
JB
40 })
41
292ad316 42 it('Verify that handleError function is working properly', () => {
7fc5cce6
APA
43 const error = new Error('My error')
44 const worker = new ThreadWorker(() => {})
78cea37e 45 expect(worker.handleError(error)).toStrictEqual(error)
7fc5cce6
APA
46 })
47
48 it('Verify that get main worker throw error if main worker is not set', () => {
49 expect(() =>
50 new StubPoolWithIsMainWorker(() => {}).getMainWorker()
292ad316 51 ).toThrowError(new Error('Main worker was not set'))
7fc5cce6 52 })
c510fea7 53})