From bcf04003ea3a80a5ab2a52102cd908c5ebcb0d1a Mon Sep 17 00:00:00 2001 From: aardizio Date: Wed, 11 Nov 2020 09:10:25 +0100 Subject: [PATCH] Fix the issue https://github.com/pioardi/poolifier/issues/61 --- lib/dynamic.js | 4 ++++ tests/dynamic.test.js | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/dynamic.js b/lib/dynamic.js index 36db0c75..77e5e5fa 100644 --- a/lib/dynamic.js +++ b/lib/dynamic.js @@ -45,6 +45,10 @@ class DynamicThreadPool extends FixedThreadPool { if (message.kill) { worker.postMessage({ kill: 1 }) worker.terminate() + // clean workers from data structures + const myIndex = this.workers.indexOf(worker) + this.workers.splice(myIndex, 1) + this.tasks.delete(worker) } }) return worker diff --git a/tests/dynamic.test.js b/tests/dynamic.test.js index e8be74ac..15195012 100644 --- a/tests/dynamic.test.js +++ b/tests/dynamic.test.js @@ -32,6 +32,21 @@ describe('Dynamic thread pool test suite ', () => { expect(closedThreads).toBe(max - min) }) + it('Verify scale thread up and down is working', async () => { + expect(pool.workers.length).toBe(min) + for (let i = 0; i < max * 10; i++) { + pool.execute({ test: 'test' }) + } + expect(pool.workers.length).toBe(max) + await new Promise(resolve => setTimeout(resolve, 1000)) + expect(pool.workers.length).toBe(min) + for (let i = 0; i < max * 10; i++) { + pool.execute({ test: 'test' }) + } + expect(pool.workers.length).toBe(max) + await new Promise(resolve => setTimeout(resolve, 1000)) + expect(pool.workers.length).toBe(min) + }) it('Shutdown test', async () => { let closedThreads = 0 pool.workers.forEach(w => { -- 2.34.1