From 8620fb25c8b80fc7ef55d76e19781181571c5502 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 9 Mar 2021 10:32:35 +0100 Subject: [PATCH] Add two tests to assess worker options. (#260) --- package-lock.json | 71 ---------------------- tests/pools/abstract/abstract-pool.test.js | 6 +- tests/pools/cluster/dynamic.test.js | 3 +- tests/pools/cluster/fixed.test.js | 6 +- tests/pools/thread/dynamic.test.js | 3 +- tests/pools/thread/fixed.test.js | 6 +- tests/worker/abstract-worker.test.js | 20 +++++- 7 files changed, 32 insertions(+), 83 deletions(-) diff --git a/package-lock.json b/package-lock.json index 9ab91708..e568ee60 100644 --- a/package-lock.json +++ b/package-lock.json @@ -663,34 +663,6 @@ "regexpp": "^3.0.0", "semver": "^7.3.2", "tsutils": "^3.17.1" - }, - "dependencies": { - "@typescript-eslint/scope-manager": { - "version": "4.17.0", - "resolved": "https://registry.npmjs.org/@typescript-eslint/scope-manager/-/scope-manager-4.17.0.tgz", - "integrity": "sha512-OJ+CeTliuW+UZ9qgULrnGpPQ1bhrZNFpfT/Bc0pzNeyZwMik7/ykJ0JHnQ7krHanFN9wcnPK89pwn84cRUmYjw==", - "dev": true, - "requires": { - "@typescript-eslint/types": "4.17.0", - "@typescript-eslint/visitor-keys": "4.17.0" - } - }, - "@typescript-eslint/types": { - "version": "4.17.0", - "resolved": "https://registry.npmjs.org/@typescript-eslint/types/-/types-4.17.0.tgz", - "integrity": "sha512-RN5z8qYpJ+kXwnLlyzZkiJwfW2AY458Bf8WqllkondQIcN2ZxQowAToGSd9BlAUZDB5Ea8I6mqL2quGYCLT+2g==", - "dev": true - }, - "@typescript-eslint/visitor-keys": { - "version": "4.17.0", - "resolved": "https://registry.npmjs.org/@typescript-eslint/visitor-keys/-/visitor-keys-4.17.0.tgz", - "integrity": "sha512-WfuMN8mm5SSqXuAr9NM+fItJ0SVVphobWYkWOwQ1odsfC014Vdxk/92t4JwS1Q6fCA/ABfCKpa3AVtpUKTNKGQ==", - "dev": true, - "requires": { - "@typescript-eslint/types": "4.17.0", - "eslint-visitor-keys": "^2.0.0" - } - } } }, "@typescript-eslint/experimental-utils": { @@ -705,49 +677,6 @@ "@typescript-eslint/typescript-estree": "4.17.0", "eslint-scope": "^5.0.0", "eslint-utils": "^2.0.0" - }, - "dependencies": { - "@typescript-eslint/scope-manager": { - "version": "4.17.0", - "resolved": "https://registry.npmjs.org/@typescript-eslint/scope-manager/-/scope-manager-4.17.0.tgz", - "integrity": "sha512-OJ+CeTliuW+UZ9qgULrnGpPQ1bhrZNFpfT/Bc0pzNeyZwMik7/ykJ0JHnQ7krHanFN9wcnPK89pwn84cRUmYjw==", - "dev": true, - "requires": { - "@typescript-eslint/types": "4.17.0", - "@typescript-eslint/visitor-keys": "4.17.0" - } - }, - "@typescript-eslint/types": { - "version": "4.17.0", - "resolved": "https://registry.npmjs.org/@typescript-eslint/types/-/types-4.17.0.tgz", - "integrity": "sha512-RN5z8qYpJ+kXwnLlyzZkiJwfW2AY458Bf8WqllkondQIcN2ZxQowAToGSd9BlAUZDB5Ea8I6mqL2quGYCLT+2g==", - "dev": true - }, - "@typescript-eslint/typescript-estree": { - "version": "4.17.0", - "resolved": "https://registry.npmjs.org/@typescript-eslint/typescript-estree/-/typescript-estree-4.17.0.tgz", - "integrity": "sha512-lRhSFIZKUEPPWpWfwuZBH9trYIEJSI0vYsrxbvVvNyIUDoKWaklOAelsSkeh3E2VBSZiNe9BZ4E5tYBZbUczVQ==", - "dev": true, - "requires": { - "@typescript-eslint/types": "4.17.0", - "@typescript-eslint/visitor-keys": "4.17.0", - "debug": "^4.1.1", - "globby": "^11.0.1", - "is-glob": "^4.0.1", - "semver": "^7.3.2", - "tsutils": "^3.17.1" - } - }, - "@typescript-eslint/visitor-keys": { - "version": "4.17.0", - "resolved": "https://registry.npmjs.org/@typescript-eslint/visitor-keys/-/visitor-keys-4.17.0.tgz", - "integrity": "sha512-WfuMN8mm5SSqXuAr9NM+fItJ0SVVphobWYkWOwQ1odsfC014Vdxk/92t4JwS1Q6fCA/ABfCKpa3AVtpUKTNKGQ==", - "dev": true, - "requires": { - "@typescript-eslint/types": "4.17.0", - "eslint-visitor-keys": "^2.0.0" - } - } } }, "@typescript-eslint/parser": { diff --git a/tests/pools/abstract/abstract-pool.test.js b/tests/pools/abstract/abstract-pool.test.js index a2170491..bb6a2e4a 100644 --- a/tests/pools/abstract/abstract-pool.test.js +++ b/tests/pools/abstract/abstract-pool.test.js @@ -104,7 +104,7 @@ describe('Abstract pool test suite', () => { numberOfWorkers, './tests/worker-files/thread/testWorker.js' ) - expect(pool.opts.enableEvents).toEqual(true) + expect(pool.opts.enableEvents).toBe(true) expect(pool.emitter).toBeDefined() expect(pool.opts.workerChoiceStrategy).toBe( WorkerChoiceStrategies.ROUND_ROBIN @@ -118,7 +118,7 @@ describe('Abstract pool test suite', () => { enableEvents: false } ) - expect(pool.opts.enableEvents).toEqual(false) + expect(pool.opts.enableEvents).toBe(false) expect(pool.emitter).toBeUndefined() expect(pool.opts.workerChoiceStrategy).toBe( WorkerChoiceStrategies.LESS_RECENTLY_USED @@ -137,7 +137,7 @@ describe('Abstract pool test suite', () => { for (let i = 0; i < numberOfWorkers * 2; i++) { promises.push(pool.execute({ test: 'test' })) } - expect(poolBusy).toEqual(numberOfWorkers) + expect(poolBusy).toBe(numberOfWorkers) pool.destroy() }) }) diff --git a/tests/pools/cluster/dynamic.test.js b/tests/pools/cluster/dynamic.test.js index 9e610d65..9a39c1d6 100644 --- a/tests/pools/cluster/dynamic.test.js +++ b/tests/pools/cluster/dynamic.test.js @@ -28,7 +28,7 @@ describe('Dynamic cluster pool test suite', () => { } expect(pool.workers.length).toBeLessThanOrEqual(max) expect(pool.workers.length).toBeGreaterThan(min) - expect(poolBusy).toEqual(max + 1) + expect(poolBusy).toBe(max + 1) const numberOfExitEvents = await TestUtils.waitExits(pool, max - min) expect(numberOfExitEvents).toBe(max - min) }) @@ -69,6 +69,7 @@ describe('Dynamic cluster pool test suite', () => { './tests/worker-files/cluster/testWorker.js' ) const result = await pool1.execute({ test: 'test' }) + expect(result).toBeDefined() expect(result).toBeFalsy() // We need to clean up the resources after our test await pool1.destroy() diff --git a/tests/pools/cluster/fixed.test.js b/tests/pools/cluster/fixed.test.js index 04467620..7048bb79 100644 --- a/tests/pools/cluster/fixed.test.js +++ b/tests/pools/cluster/fixed.test.js @@ -74,7 +74,7 @@ describe('Fixed cluster pool test suite', () => { for (let i = 0; i < numberOfWorkers * 2; i++) { promises.push(pool.execute({ test: 'test' })) } - expect(poolBusy).toEqual(numberOfWorkers) + expect(poolBusy).toBe(numberOfWorkers) }) it('Verify that is possible to have a worker that return undefined', async () => { @@ -98,7 +98,7 @@ describe('Fixed cluster pool test suite', () => { inError = e } expect(inError).toBeDefined() - expect(typeof inError === 'string').toEqual(true) + expect(typeof inError === 'string').toBe(true) expect(inError).toBe('Error Message from ClusterWorker') }) @@ -111,7 +111,7 @@ describe('Fixed cluster pool test suite', () => { inError = e } expect(inError).toBeDefined() - expect(typeof inError === 'string').toEqual(true) + expect(typeof inError === 'string').toBe(true) expect(inError).toBe('Error Message from ClusterWorker:async') }) diff --git a/tests/pools/thread/dynamic.test.js b/tests/pools/thread/dynamic.test.js index 31d64727..9cba566f 100644 --- a/tests/pools/thread/dynamic.test.js +++ b/tests/pools/thread/dynamic.test.js @@ -27,7 +27,7 @@ describe('Dynamic thread pool test suite', () => { promises.push(pool.execute({ test: 'test' })) } expect(pool.workers.length).toBe(max) - expect(poolBusy).toEqual(max + 1) + expect(poolBusy).toBe(max + 1) const res = await TestUtils.waitExits(pool, max - min) expect(res).toBe(max - min) }) @@ -72,6 +72,7 @@ describe('Dynamic thread pool test suite', () => { './tests/worker-files/thread/testWorker.js' ) const res = await pool1.execute({ test: 'test' }) + expect(res).toBeDefined() expect(res).toBeFalsy() // We need to clean up the resources after our test await pool1.destroy() diff --git a/tests/pools/thread/fixed.test.js b/tests/pools/thread/fixed.test.js index 09a58e2d..81ef967e 100644 --- a/tests/pools/thread/fixed.test.js +++ b/tests/pools/thread/fixed.test.js @@ -74,7 +74,7 @@ describe('Fixed thread pool test suite', () => { for (let i = 0; i < numberOfThreads * 2; i++) { promises.push(pool.execute({ test: 'test' })) } - expect(poolBusy).toEqual(numberOfThreads) + expect(poolBusy).toBe(numberOfThreads) }) it('Verify that is possible to have a worker that return undefined', async () => { @@ -100,7 +100,7 @@ describe('Fixed thread pool test suite', () => { expect(inError).toBeDefined() expect(inError).toBeInstanceOf(Error) expect(inError.message).toBeDefined() - expect(typeof inError.message === 'string').toEqual(true) + expect(typeof inError.message === 'string').toBe(true) }) it('Verify that error handling is working properly:async', async () => { @@ -114,7 +114,7 @@ describe('Fixed thread pool test suite', () => { expect(inError).toBeDefined() expect(inError).toBeInstanceOf(Error) expect(inError.message).toBeDefined() - expect(typeof inError.message === 'string').toEqual(true) + expect(typeof inError.message === 'string').toBe(true) }) it('Verify that async function is working properly', async () => { diff --git a/tests/worker/abstract-worker.test.js b/tests/worker/abstract-worker.test.js index a22d1c9f..9b4864c7 100644 --- a/tests/worker/abstract-worker.test.js +++ b/tests/worker/abstract-worker.test.js @@ -1,5 +1,5 @@ const expect = require('expect') -const { ClusterWorker, ThreadWorker } = require('../../lib') +const { ClusterWorker, KillBehaviors, ThreadWorker } = require('../../lib') class StubPoolWithIsMainWorker extends ThreadWorker { constructor (fn, opts) { @@ -15,6 +15,24 @@ describe('Abstract worker test suite', () => { ) }) + it('Verify worker default values', () => { + const worker = new ThreadWorker(() => {}) + expect(worker.maxInactiveTime).toBe(1000 * 60) + expect(worker.killBehavior).toBe(KillBehaviors.SOFT) + expect(worker.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.maxInactiveTime).toBe(6000) + expect(worker.killBehavior).toBe(KillBehaviors.HARD) + expect(worker.async).toBe(true) + }) + it('Verify that handleError function is working properly', () => { const error = new Error('My error') const worker = new ThreadWorker(() => {}) -- 2.34.1