Dedupe worker attributes (#364)
[poolifier.git] / tests / worker / abstract-worker.test.js
CommitLineData
c510fea7 1const expect = require('expect')
8620fb25 2const { ClusterWorker, KillBehaviors, ThreadWorker } = require('../../lib')
7fc5cce6
APA
3
4class StubPoolWithIsMainWorker extends ThreadWorker {
5 constructor (fn, opts) {
6 super(fn, opts)
7 this.mainWorker = false
8 }
9}
c510fea7
APA
10
11describe('Abstract worker test suite', () => {
12 it('Verify that fn function is mandatory', () => {
8d3782fa
JB
13 expect(() => new ClusterWorker()).toThrowError(
14 new Error('fn parameter is mandatory')
15 )
c510fea7 16 })
7fc5cce6 17
e088a00c 18 it('Verify worker options default values', () => {
8620fb25 19 const worker = new ThreadWorker(() => {})
e088a00c
JB
20 expect(worker.opts.maxInactiveTime).toBe(1000 * 60)
21 expect(worker.opts.killBehavior).toBe(KillBehaviors.SOFT)
22 expect(worker.opts.async).toBe(false)
8620fb25
JB
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 })
e088a00c
JB
31 expect(worker.opts.maxInactiveTime).toBe(6000)
32 expect(worker.opts.killBehavior).toBe(KillBehaviors.HARD)
33 expect(worker.opts.async).toBe(true)
8620fb25
JB
34 })
35
292ad316 36 it('Verify that handleError function is working properly', () => {
7fc5cce6
APA
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()
292ad316 45 ).toThrowError(new Error('Main worker was not set'))
7fc5cce6 46 })
c510fea7 47})