X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=tests%2Fworker%2Fabstract-worker.test.js;h=b62ba9425e1289fb8afc6361edc32f2be2ca9531;hb=318d4156a8e078eec77d8e3fa8e8697dcc2f4d99;hp=0c9b665ee75ae0de6a6a5fd4d910fc2bccff1fd3;hpb=8d3782faec08afef3370aaaadd06de2c1cb270fd;p=poolifier.git diff --git a/tests/worker/abstract-worker.test.js b/tests/worker/abstract-worker.test.js index 0c9b665e..b62ba942 100644 --- a/tests/worker/abstract-worker.test.js +++ b/tests/worker/abstract-worker.test.js @@ -1,10 +1,63 @@ -const expect = require('expect') -const { ClusterWorker } = require('../../lib') +const { expect } = require('expect') +const { ClusterWorker, KillBehaviors, ThreadWorker } = require('../../lib') describe('Abstract worker test suite', () => { - it('Verify that fn function is mandatory', () => { - expect(() => new ClusterWorker()).toThrowError( - new Error('fn parameter is mandatory') + class StubPoolWithIsMainWorker extends ThreadWorker { + constructor (fn, opts) { + super(fn, opts) + this.mainWorker = undefined + } + } + + it('Verify worker options default values', () => { + const worker = new ThreadWorker(() => {}) + expect(worker.opts.maxInactiveTime).toStrictEqual(60000) + expect(worker.opts.killBehavior).toBe(KillBehaviors.SOFT) + expect(worker.opts.async).toBe(false) + }) + + it('Verify that worker options are set at worker creation', () => { + const worker = new ClusterWorker(() => {}, { + maxInactiveTime: 6000, + async: true, + killBehavior: KillBehaviors.HARD + }) + expect(worker.opts.maxInactiveTime).toStrictEqual(6000) + expect(worker.opts.killBehavior).toBe(KillBehaviors.HARD) + expect(worker.opts.async).toBe(true) + }) + + it('Verify that fn parameter is mandatory', () => { + expect(() => new ClusterWorker()).toThrowError('fn parameter is mandatory') + }) + + it('Verify that fn parameter is a function', () => { + expect(() => new ClusterWorker({})).toThrowError( + new TypeError('fn parameter is not a function') + ) + expect(() => new ClusterWorker('')).toThrowError( + new TypeError('fn parameter is not a function') ) }) + + it('Verify that async fn parameter without async option throw error', () => { + const fn = async () => { + return new Promise() + } + expect(() => new ClusterWorker(fn)).toThrowError( + 'fn parameter is an async function, please set the async option to true' + ) + }) + + it('Verify that handleError() method is working properly', () => { + const error = new Error('My error') + const worker = new ThreadWorker(() => {}) + expect(worker.handleError(error)).toStrictEqual(error) + }) + + it('Verify that getMainWorker() throw error if main worker is not set', () => { + expect(() => + new StubPoolWithIsMainWorker(() => {}).getMainWorker() + ).toThrowError('Main worker was not set') + }) })