Properly integrate standard JS tools for JS and TS code
[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 = undefined
9 }
10 }
11
12 it('Verify that fn parameter is mandatory', () => {
13 expect(() => new ClusterWorker()).toThrowError(
14 new Error('fn parameter is mandatory')
15 )
16 })
17
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
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)
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 })
37 expect(worker.opts.maxInactiveTime).toStrictEqual(6000)
38 expect(worker.opts.killBehavior).toBe(KillBehaviors.HARD)
39 expect(worker.opts.async).toBe(true)
40 })
41
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)
46 })
47
48 it('Verify that get main worker throw error if main worker is not set', () => {
49 expect(() =>
50 new StubPoolWithIsMainWorker(() => {}).getMainWorker()
51 ).toThrowError(new Error('Main worker was not set'))
52 })
53 })