Remove unused dependency (#346)
[poolifier.git] / tests / worker / abstract-worker.test.js
1 const expect = require('expect')
2 const { ClusterWorker, KillBehaviors, ThreadWorker } = require('../../lib')
3
4 class StubPoolWithIsMainWorker extends ThreadWorker {
5 constructor (fn, opts) {
6 super(fn, opts)
7 this.mainWorker = false
8 }
9 }
10
11 describe('Abstract worker test suite', () => {
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 default values', () => {
19 const worker = new ThreadWorker(() => {})
20 expect(worker.maxInactiveTime).toBe(1000 * 60)
21 expect(worker.killBehavior).toBe(KillBehaviors.SOFT)
22 expect(worker.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.maxInactiveTime).toBe(6000)
32 expect(worker.killBehavior).toBe(KillBehaviors.HARD)
33 expect(worker.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 })