From: aardizio Date: Wed, 11 Nov 2020 08:10:25 +0000 (+0100) Subject: Fix the issue https://github.com/pioardi/poolifier/issues/61 X-Git-Tag: v1.2.1~1 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=bcf04003ea3a80a5ab2a52102cd908c5ebcb0d1a;p=poolifier.git Fix the issue https://github.com/pioardi/poolifier/issues/61 --- 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 => {