Fix the issue https://github.com/pioardi/poolifier/issues/61
authoraardizio <alessandroardizio94@gmail.com>
Wed, 11 Nov 2020 08:10:25 +0000 (09:10 +0100)
committeraardizio <alessandroardizio94@gmail.com>
Wed, 11 Nov 2020 08:10:25 +0000 (09:10 +0100)
lib/dynamic.js
tests/dynamic.test.js

index 36db0c75673ed7a70d4fe5e6e634b2754c38529f..77e5e5facd04804f1581d77c2a2e8c568be99b86 100644 (file)
@@ -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
index e8be74ace413c874a008dbd846c2ec98811d46cb..15195012be80f25db7750f80de1fbebdf5614765 100644 (file)
@@ -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 => {