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