Commit | Line | Data |
---|---|---|
a61a0724 | 1 | const { expect } = require('expect') |
8620fb25 | 2 | const { ClusterWorker, KillBehaviors, ThreadWorker } = require('../../lib') |
7fc5cce6 | 3 | |
e1ffb94f JB |
4 | describe('Abstract worker test suite', () => { |
5 | class StubPoolWithIsMainWorker extends ThreadWorker { | |
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 JB |
15 | expect(worker.opts.killBehavior).toBe(KillBehaviors.SOFT) |
16 | expect(worker.opts.async).toBe(false) | |
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 JB |
26 | expect(worker.opts.killBehavior).toBe(KillBehaviors.HARD) |
27 | expect(worker.opts.async).toBe(true) | |
8620fb25 JB |
28 | }) |
29 | ||
d4aeae5a JB |
30 | it('Verify that fn parameter is mandatory', () => { |
31 | expect(() => new ClusterWorker()).toThrowError('fn parameter is mandatory') | |
32 | }) | |
33 | ||
34 | it('Verify that fn parameter is a function', () => { | |
35 | expect(() => new ClusterWorker({})).toThrowError( | |
36 | new TypeError('fn parameter is not a function') | |
37 | ) | |
38 | expect(() => new ClusterWorker('')).toThrowError( | |
39 | new TypeError('fn parameter is not a function') | |
40 | ) | |
41 | }) | |
42 | ||
43 | it('Verify that async fn parameter without async option throw error', () => { | |
44 | const fn = async () => { | |
45 | return new Promise() | |
46 | } | |
47 | expect(() => new ClusterWorker(fn)).toThrowError( | |
48 | 'fn parameter is an async function, please set the async option to true' | |
49 | ) | |
50 | }) | |
51 | ||
318d4156 | 52 | it('Verify that handleError() method is working properly', () => { |
7fc5cce6 APA |
53 | const error = new Error('My error') |
54 | const worker = new ThreadWorker(() => {}) | |
78cea37e | 55 | expect(worker.handleError(error)).toStrictEqual(error) |
7fc5cce6 APA |
56 | }) |
57 | ||
318d4156 | 58 | it('Verify that getMainWorker() throw error if main worker is not set', () => { |
7fc5cce6 APA |
59 | expect(() => |
60 | new StubPoolWithIsMainWorker(() => {}).getMainWorker() | |
d4aeae5a | 61 | ).toThrowError('Main worker was not set') |
7fc5cce6 | 62 | }) |
c510fea7 | 63 | }) |