fix: fix dynamic import syntax
[poolifier.git] / tests / worker / abstract-worker.test.js
CommitLineData
a61a0724 1const { expect } = require('expect')
8620fb25 2const { ClusterWorker, KillBehaviors, ThreadWorker } = require('../../lib')
7fc5cce6 3
e1ffb94f 4describe('Abstract worker test suite', () => {
1f68cede 5 class StubWorkerWithMainWorker extends ThreadWorker {
e1ffb94f
JB
6 constructor (fn, opts) {
7 super(fn, opts)
78cea37e 8 this.mainWorker = undefined
e1ffb94f 9 }
7fc5cce6 10 }
c510fea7 11
e088a00c 12 it('Verify worker options default values', () => {
8620fb25 13 const worker = new ThreadWorker(() => {})
978aad6f 14 expect(worker.opts.maxInactiveTime).toStrictEqual(60000)
e088a00c 15 expect(worker.opts.killBehavior).toBe(KillBehaviors.SOFT)
571227f4 16 expect(worker.opts.async).toBe(undefined)
8620fb25
JB
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 })
978aad6f 25 expect(worker.opts.maxInactiveTime).toStrictEqual(6000)
e088a00c 26 expect(worker.opts.killBehavior).toBe(KillBehaviors.HARD)
571227f4 27 expect(worker.opts.async).toBe(undefined)
8620fb25
JB
28 })
29
a86b6df1
JB
30 it('Verify that taskFunctions parameter is mandatory', () => {
31 expect(() => new ClusterWorker()).toThrowError(
32 'taskFunctions parameter is mandatory'
33 )
d4aeae5a
JB
34 })
35
f34fdabe 36 it('Verify that taskFunctions parameter is a function or a plain object', () => {
a86b6df1 37 expect(() => new ClusterWorker(0)).toThrowError(
f34fdabe
JB
38 new TypeError(
39 'taskFunctions parameter is not a function or a plain object'
40 )
d4aeae5a
JB
41 )
42 expect(() => new ClusterWorker('')).toThrowError(
f34fdabe
JB
43 new TypeError(
44 'taskFunctions parameter is not a function or a plain object'
45 )
a86b6df1
JB
46 )
47 expect(() => new ClusterWorker(true)).toThrowError(
f34fdabe
JB
48 new TypeError(
49 'taskFunctions parameter is not a function or a plain object'
50 )
d4aeae5a 51 )
a86b6df1 52 expect(() => new ClusterWorker([])).toThrowError(
f34fdabe
JB
53 new TypeError(
54 'taskFunctions parameter is not a function or a plain object'
55 )
a86b6df1
JB
56 )
57 expect(() => new ClusterWorker(new Map())).toThrowError(
f34fdabe
JB
58 new TypeError(
59 'taskFunctions parameter is not a function or a plain object'
60 )
a86b6df1
JB
61 )
62 expect(() => new ClusterWorker(new Set())).toThrowError(
f34fdabe
JB
63 new TypeError(
64 'taskFunctions parameter is not a function or a plain object'
65 )
a86b6df1
JB
66 )
67 expect(() => new ClusterWorker(new WeakMap())).toThrowError(
f34fdabe
JB
68 new TypeError(
69 'taskFunctions parameter is not a function or a plain object'
70 )
d4aeae5a 71 )
a86b6df1 72 expect(() => new ClusterWorker(new WeakSet())).toThrowError(
f34fdabe
JB
73 new TypeError(
74 'taskFunctions parameter is not a function or a plain object'
75 )
a86b6df1 76 )
f34fdabe
JB
77 })
78
79 it('Verify that taskFunctions parameter is not an empty object', () => {
630f0acf 80 expect(() => new ClusterWorker({})).toThrowError(
0d80593b 81 new Error('taskFunctions parameter object is empty')
630f0acf 82 )
a86b6df1
JB
83 })
84
f34fdabe
JB
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
a86b6df1
JB
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 })
82888165 103 expect(typeof worker.taskFunctions.get('default') === 'function').toBe(true)
a86b6df1
JB
104 expect(typeof worker.taskFunctions.get('fn1') === 'function').toBe(true)
105 expect(typeof worker.taskFunctions.get('fn2') === 'function').toBe(true)
d4aeae5a
JB
106 })
107
318d4156 108 it('Verify that handleError() method is working properly', () => {
7fc5cce6
APA
109 const error = new Error('My error')
110 const worker = new ThreadWorker(() => {})
78cea37e 111 expect(worker.handleError(error)).toStrictEqual(error)
7fc5cce6
APA
112 })
113
318d4156 114 it('Verify that getMainWorker() throw error if main worker is not set', () => {
7fc5cce6 115 expect(() =>
1f68cede 116 new StubWorkerWithMainWorker(() => {}).getMainWorker()
d4aeae5a 117 ).toThrowError('Main worker was not set')
7fc5cce6 118 })
c510fea7 119})