Cluster pool is now working
authoraardizio <alessandroardizio94@gmail.com>
Mon, 15 Feb 2021 16:29:36 +0000 (17:29 +0100)
committeraardizio <alessandroardizio94@gmail.com>
Mon, 15 Feb 2021 16:29:36 +0000 (17:29 +0100)
src/pools/cluster/dynamic.ts
tests/pools/cluster/dynamic.test.js
tests/pools/cluster/fixed.test.js
tests/pools/thread/dynamic.test.js
tests/pools/thread/fixed.test.js

index 44847b61bebe5b502fec575dfddc3fff7eab33ef..f6c70126f41599b482c6a653923dad3a66bed964 100644 (file)
@@ -62,7 +62,7 @@ export class DynamicClusterPool<
     const worker = this.createAndSetupWorker()
     this.registerWorkerMessageListener<Data>(worker, message => {
       const tasksInProgress = this.tasks.get(worker)
-      if (message.kill) {
+      if (message.kill && !tasksInProgress) {
         this.sendToWorker(worker, { kill: 1 })
         void this.destroyWorker(worker)
       }
index 024e26707e66900f3844256334142272888059d3..e0a0fde3cc6246bf662cc656851bbf78f32abcde 100644 (file)
@@ -7,8 +7,7 @@ const pool = new DynamicClusterPool(
   max,
   './tests/worker/cluster/testWorker.js',
   {
-    errorHandler: e => console.error(e),
-    onlineHandler: () => console.log('worker is online')
+    errorHandler: e => console.error(e)
   }
 )
 
@@ -51,7 +50,7 @@ describe('Dynamic cluster pool test suite ', () => {
       pool.execute({ test: 'test' })
     }
     expect(pool.workers.length).toBeGreaterThan(min)
-    await new Promise(resolve => setTimeout(resolve, 2000))
+    await new Promise(resolve => setTimeout(resolve, 3000))
     expect(pool.workers.length).toBe(min)
   })
   it('Shutdown test', async () => {
@@ -62,7 +61,7 @@ describe('Dynamic cluster pool test suite ', () => {
       })
     })
     pool.destroy()
-    await new Promise(resolve => setTimeout(resolve, 1000))
+    await new Promise(resolve => setTimeout(resolve, 2000))
     expect(closedWorkers).toBe(min)
   })
 
@@ -91,14 +90,14 @@ describe('Dynamic cluster pool test suite ', () => {
     const longRunningPool = new DynamicClusterPool(
       min,
       max,
-      './tests/worker/thread/longRunningWorker.js'
+      './tests/worker/cluster/longRunningWorker.js'
     )
     expect(longRunningPool.workers.length).toBe(min)
     for (let i = 0; i < max * 10; i++) {
       longRunningPool.execute({ test: 'test' })
     }
     expect(longRunningPool.workers.length).toBe(max)
-    await new Promise(resolve => setTimeout(resolve, 1000))
+    await new Promise(resolve => setTimeout(resolve, 3000))
     // Here we expect the workers to be at the max size since that the task is still running
     expect(longRunningPool.workers.length).toBe(max)
   })
index d22f697a5d3893863c5c60798cd8d77a7dd7e451..51501df0350dde87d73be94cc4ddebb7be66cee2 100644 (file)
@@ -6,8 +6,7 @@ const pool = new FixedClusterPool(
   numberOfWorkers,
   './tests/worker/cluster/testWorker.js',
   {
-    errorHandler: e => console.error(e),
-    onlineHandler: () => console.log('worker is online')
+    errorHandler: e => console.error(e)
   }
 )
 const emptyPool = new FixedClusterPool(
@@ -19,8 +18,7 @@ const errorPool = new FixedClusterPool(
   1,
   './tests/worker/cluster/errorWorker.js',
   {
-    errorHandler: e => console.error(e),
-    onlineHandler: () => console.log('worker is online')
+    errorHandler: e => console.error(e)
   }
 )
 
index d59b98587c4f6c225ba9d0f6038b1a54bc6d2f33..2f89b2ffea69b045b576db32287a5acc8bfd2a7b 100644 (file)
@@ -7,8 +7,7 @@ const pool = new DynamicThreadPool(
   max,
   './tests/worker/thread/testWorker.js',
   {
-    errorHandler: e => console.error(e),
-    onlineHandler: () => console.log('worker is online')
+    errorHandler: e => console.error(e)
   }
 )
 
index a5ec3f0a8a63e720b1c789e9a23249a1ca866a4f..e160f17c72b6d4db2e8111d7fed488f84a666610 100644 (file)
@@ -6,8 +6,7 @@ const pool = new FixedThreadPool(
   numberOfThreads,
   './tests/worker/thread/testWorker.js',
   {
-    errorHandler: e => console.error(e),
-    onlineHandler: () => console.log('worker is online')
+    errorHandler: e => console.error(e)
   }
 )
 const emptyPool = new FixedThreadPool(1, './tests/worker/thread/emptyWorker.js')