| 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 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 | |
| 52 | it('Verify that handleError() method is working properly', () => { |
| 53 | const error = new Error('My error') |
| 54 | const worker = new ThreadWorker(() => {}) |
| 55 | expect(worker.handleError(error)).toStrictEqual(error) |
| 56 | }) |
| 57 | |
| 58 | it('Verify that getMainWorker() throw error if main worker is not set', () => { |
| 59 | expect(() => |
| 60 | new StubPoolWithIsMainWorker(() => {}).getMainWorker() |
| 61 | ).toThrowError('Main worker was not set') |
| 62 | }) |
| 63 | }) |