fix: fix dynamic import syntax
[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 StubWorkerWithMainWorker 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(undefined)
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(undefined)
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 a plain object', () => {
37 expect(() => new ClusterWorker(0)).toThrowError(
38 new TypeError(
39 'taskFunctions parameter is not a function or a plain object'
40 )
41 )
42 expect(() => new ClusterWorker('')).toThrowError(
43 new TypeError(
44 'taskFunctions parameter is not a function or a plain object'
45 )
46 )
47 expect(() => new ClusterWorker(true)).toThrowError(
48 new TypeError(
49 'taskFunctions parameter is not a function or a plain object'
50 )
51 )
52 expect(() => new ClusterWorker([])).toThrowError(
53 new TypeError(
54 'taskFunctions parameter is not a function or a plain object'
55 )
56 )
57 expect(() => new ClusterWorker(new Map())).toThrowError(
58 new TypeError(
59 'taskFunctions parameter is not a function or a plain object'
60 )
61 )
62 expect(() => new ClusterWorker(new Set())).toThrowError(
63 new TypeError(
64 'taskFunctions parameter is not a function or a plain object'
65 )
66 )
67 expect(() => new ClusterWorker(new WeakMap())).toThrowError(
68 new TypeError(
69 'taskFunctions parameter is not a function or a plain object'
70 )
71 )
72 expect(() => new ClusterWorker(new WeakSet())).toThrowError(
73 new TypeError(
74 'taskFunctions parameter is not a function or a plain object'
75 )
76 )
77 })
78
79 it('Verify that taskFunctions parameter is not an empty object', () => {
80 expect(() => new ClusterWorker({})).toThrowError(
81 new Error('taskFunctions parameter object is empty')
82 )
83 })
84
85 it('Verify that taskFunctions parameter with multiple task functions contains function', () => {
86 const fn1 = () => {
87 return 1
88 }
89 const fn2 = ''
90 expect(() => new ThreadWorker({ fn1, fn2 })).toThrowError(
91 new TypeError('A taskFunctions parameter object value is not a function')
92 )
93 })
94
95 it('Verify that taskFunctions parameter with multiple task functions is taken', () => {
96 const fn1 = () => {
97 return 1
98 }
99 const fn2 = () => {
100 return 2
101 }
102 const worker = new ClusterWorker({ fn1, fn2 })
103 expect(typeof worker.taskFunctions.get('default') === 'function').toBe(true)
104 expect(typeof worker.taskFunctions.get('fn1') === 'function').toBe(true)
105 expect(typeof worker.taskFunctions.get('fn2') === 'function').toBe(true)
106 })
107
108 it('Verify that handleError() method is working properly', () => {
109 const error = new Error('My error')
110 const worker = new ThreadWorker(() => {})
111 expect(worker.handleError(error)).toStrictEqual(error)
112 })
113
114 it('Verify that getMainWorker() throw error if main worker is not set', () => {
115 expect(() =>
116 new StubWorkerWithMainWorker(() => {}).getMainWorker()
117 ).toThrowError('Main worker was not set')
118 })
119 })