From: aardizio Date: Fri, 24 Jan 2020 21:51:54 +0000 (+0100) Subject: Improvements based on https://github.com/pioardi/poolifier/issues/6 X-Git-Tag: 1.1.0~32 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=1f9a5a440caea7dc60a664d413304250969bb4d8;p=poolifier.git Improvements based on https://github.com/pioardi/poolifier/issues/6 --- diff --git a/README.MD b/README.MD index 5ab24077..7b57162a 100644 --- a/README.MD +++ b/README.MD @@ -49,15 +49,10 @@ function yourFunction (data) { return { ok: 1 } } -class MyWorker extends ThreadWorker { - constructor () { - super(yourFunction, { maxInactiveTime: 1000 * 60}) - } -} -module.exports = new MyWorker() +module.exports = new ThreadWorker(yourFunction, { maxInactiveTime: 60000 }) ``` -Instantiate your pool based on your needed : +Instantiate your pool based on your needed : ```js 'use strict' diff --git a/benchmarks/yourWorker.js b/benchmarks/yourWorker.js index 701aa8cf..ac3959d1 100644 --- a/benchmarks/yourWorker.js +++ b/benchmarks/yourWorker.js @@ -12,9 +12,4 @@ function yourFunction (data) { return { ok: 1 } } -class MyWorker extends ThreadWorker { - constructor () { - super(yourFunction) - } -} -module.exports = new MyWorker() +module.exports = new ThreadWorker(yourFunction) diff --git a/examples/multifunctionWorker.js b/examples/multifunctionWorker.js index 87edd57d..f3efb452 100644 --- a/examples/multifunctionWorker.js +++ b/examples/multifunctionWorker.js @@ -11,9 +11,4 @@ function yourFunction (data) { } } -class MyWorker extends ThreadWorker { - constructor () { - super(yourFunction) - } -} -module.exports = new MyWorker() +module.exports = new ThreadWorker(yourFunction) diff --git a/examples/yourWorker.js b/examples/yourWorker.js index 701aa8cf..ac3959d1 100644 --- a/examples/yourWorker.js +++ b/examples/yourWorker.js @@ -12,9 +12,4 @@ function yourFunction (data) { return { ok: 1 } } -class MyWorker extends ThreadWorker { - constructor () { - super(yourFunction) - } -} -module.exports = new MyWorker() +module.exports = new ThreadWorker(yourFunction) diff --git a/lib/fixed.js b/lib/fixed.js index 8d899d57..67694772 100644 --- a/lib/fixed.js +++ b/lib/fixed.js @@ -34,9 +34,9 @@ class FixedThreadPool { } } - destroy () { + async destroy () { for (const worker of this.workers) { - worker.terminate() + await worker.terminate() } } diff --git a/tests/dynamic.test.js b/tests/dynamic.test.js index e23fd9af..e8be74ac 100644 --- a/tests/dynamic.test.js +++ b/tests/dynamic.test.js @@ -39,8 +39,7 @@ describe('Dynamic thread pool test suite ', () => { closedThreads++ }) }) - pool.destroy() - await new Promise(resolve => setTimeout(resolve, 2000)) + await pool.destroy() expect(closedThreads).toBe(min) }) diff --git a/tests/fixed.test.js b/tests/fixed.test.js index 3b485fbf..e56e15ff 100644 --- a/tests/fixed.test.js +++ b/tests/fixed.test.js @@ -61,8 +61,7 @@ describe('Fixed thread pool test suite ', () => { closedThreads++ }) }) - pool.destroy() - await new Promise(resolve => setTimeout(resolve, 1000)) + await pool.destroy() expect(closedThreads).toBe(numThreads) }) diff --git a/tests/workers/echoWorker.js b/tests/workers/echoWorker.js index 2a5ef89d..1026a369 100644 --- a/tests/workers/echoWorker.js +++ b/tests/workers/echoWorker.js @@ -5,10 +5,4 @@ function echo (data) { return data } -class MyWorker extends ThreadWorker { - constructor () { - super(echo, { maxInactiveTime: 500 }) - } -} - -module.exports = new MyWorker() +module.exports = new ThreadWorker(echo, { maxInactiveTime: 500 }) diff --git a/tests/workers/emptyWorker.js b/tests/workers/emptyWorker.js index 21f93aeb..42388d0e 100644 --- a/tests/workers/emptyWorker.js +++ b/tests/workers/emptyWorker.js @@ -4,10 +4,4 @@ const { ThreadWorker } = require('../../lib/workers') function test (data) { } -class MyWorker extends ThreadWorker { - constructor () { - super(test, { maxInactiveTime: 500 }) - } -} - -module.exports = new MyWorker() +module.exports = new ThreadWorker(test, { maxInactiveTime: 500 }) diff --git a/tests/workers/errorWorker.js b/tests/workers/errorWorker.js index ee3c74e7..c9b4f963 100644 --- a/tests/workers/errorWorker.js +++ b/tests/workers/errorWorker.js @@ -5,10 +5,4 @@ function error (data) { throw new Error(data) } -class MyWorker extends ThreadWorker { - constructor () { - super(error, { maxInactiveTime: 500 }) - } -} - -module.exports = new MyWorker() +module.exports = new ThreadWorker(error, { maxInactiveTime: 500 }) diff --git a/tests/workers/testWorker.js b/tests/workers/testWorker.js index 5d66f62e..5c436a14 100644 --- a/tests/workers/testWorker.js +++ b/tests/workers/testWorker.js @@ -12,10 +12,4 @@ function test (data) { return isMainThread } -class MyWorker extends ThreadWorker { - constructor () { - super(test, { maxInactiveTime: 500 }) - } -} - -module.exports = new MyWorker() +module.exports = new ThreadWorker(test, { maxInactiveTime: 500 })