feat: support multiple functions per worker
[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 worker options default values', () => {
13 const worker = new ThreadWorker(() => {})
14 expect(worker.opts.maxInactiveTime).toStrictEqual(60000)
15 expect(worker.opts.killBehavior).toBe(KillBehaviors.SOFT)
16 expect(worker.opts.async).toBe(false)
17 })
18
19 it('Verify that worker options are set at worker creation', () => {
20 const worker = new ClusterWorker(() => {}, {
21 maxInactiveTime: 6000,
22 async: true,
23 killBehavior: KillBehaviors.HARD
24 })
25 expect(worker.opts.maxInactiveTime).toStrictEqual(6000)
26 expect(worker.opts.killBehavior).toBe(KillBehaviors.HARD)
27 expect(worker.opts.async).toBe(true)
28 })
29
30 it('Verify that taskFunctions parameter is mandatory', () => {
31 expect(() => new ClusterWorker()).toThrowError(
32 'taskFunctions parameter is mandatory'
33 )
34 })
35
36 it('Verify that taskFunctions parameter is a function or an object', () => {
37 expect(() => new ClusterWorker(0)).toThrowError(
38 new TypeError('taskFunctions parameter is not a function or an object')
39 )
40 expect(() => new ClusterWorker('')).toThrowError(
41 new TypeError('taskFunctions parameter is not a function or an object')
42 )
43 expect(() => new ClusterWorker(true)).toThrowError(
44 new TypeError('taskFunctions parameter is not a function or an object')
45 )
46 })
47
48 it('Verify that taskFunctions parameter is an object literal', () => {
49 expect(() => new ClusterWorker([])).toThrowError(
50 new TypeError('taskFunctions parameter is not an object literal')
51 )
52 expect(() => new ClusterWorker(new Map())).toThrowError(
53 new TypeError('taskFunctions parameter is not an object literal')
54 )
55 expect(() => new ClusterWorker(new Set())).toThrowError(
56 new TypeError('taskFunctions parameter is not an object literal')
57 )
58 expect(() => new ClusterWorker(new WeakMap())).toThrowError(
59 new TypeError('taskFunctions parameter is not an object literal')
60 )
61 expect(() => new ClusterWorker(new WeakSet())).toThrowError(
62 new TypeError('taskFunctions parameter is not an object literal')
63 )
64 })
65
66 it('Verify that taskFunctions parameter with multiple task functions is taken', () => {
67 const fn1 = () => {
68 return 1
69 }
70 const fn2 = () => {
71 return 2
72 }
73 const worker = new ClusterWorker({ fn1, fn2 })
74 expect(typeof worker.taskFunctions.get('fn1') === 'function').toBe(true)
75 expect(typeof worker.taskFunctions.get('fn2') === 'function').toBe(true)
76 })
77
78 it('Verify that handleError() method is working properly', () => {
79 const error = new Error('My error')
80 const worker = new ThreadWorker(() => {})
81 expect(worker.handleError(error)).toStrictEqual(error)
82 })
83
84 it('Verify that getMainWorker() throw error if main worker is not set', () => {
85 expect(() =>
86 new StubPoolWithIsMainWorker(() => {}).getMainWorker()
87 ).toThrowError('Main worker was not set')
88 })
89 })