]> Piment Noir Git Repositories - poolifier.git/commitdiff
fix: ensure using task abortion API is not altering operations
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Tue, 1 Jul 2025 14:07:01 +0000 (16:07 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Tue, 1 Jul 2025 14:07:01 +0000 (16:07 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/pools/abstract-pool.ts
tests/pools/abstract-pool.test.mjs
tests/pools/cluster/dynamic.test.mjs
tests/pools/cluster/fixed.test.mjs
tests/pools/thread/dynamic.test.mjs
tests/pools/thread/fixed.test.mjs

index 695861966a9b18afc20b98fe319c93efde39528b..60f350645de0fda4de7f503e7482d07358bad519 100644 (file)
@@ -2011,7 +2011,7 @@ export abstract class AbstractPool<
       abortSignal?.addEventListener(
         'abort',
         () => {
-          this.workerNodes[workerNodeKey].emit('abortTask', {
+          this.workerNodes[workerNodeKey]?.emit('abortTask', {
             taskId: task.taskId,
             // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
             workerId: this.getWorkerInfo(workerNodeKey)!.id!,
index 542e491c157271625172c596589fdb5457eca9da..2887aba774989ee4ab6fbbfa8c5ad6fbc9f9ef3d 100644 (file)
@@ -1950,15 +1950,17 @@ describe('Abstract pool test suite', () => {
       new Error("Task function 'unknown' not found")
     )
     let results = await pool.mapExecute(
-      [{}, {}, {}, {}],
-      'jsonIntegerSerialization'
+      Array(4).fill({}),
+      'jsonIntegerSerialization',
+      Array(4).fill(AbortSignal.timeout(1000))
     )
     expect(results).toStrictEqual([{ ok: 1 }, { ok: 1 }, { ok: 1 }, { ok: 1 }])
     expect(pool.info.executingTasks).toBe(0)
     expect(pool.info.executedTasks).toBe(4)
     results = await pool.mapExecute(
       [{ n: 10 }, { n: 20 }, { n: 30 }, { n: 40 }],
-      'factorial'
+      'factorial',
+      Array(4).fill(AbortSignal.timeout(1000))
     )
     expect(results).toStrictEqual([
       3628800, 2432902008176640000, 2.6525285981219103e32, 8.159152832478977e47,
@@ -1967,7 +1969,13 @@ describe('Abstract pool test suite', () => {
     expect(pool.info.executedTasks).toBe(8)
     results = await pool.mapExecute(
       new Set([{ n: 10 }, { n: 20 }, { n: 30 }, { n: 40 }]),
-      'factorial'
+      'factorial',
+      new Set([
+        AbortSignal.timeout(1000),
+        AbortSignal.timeout(1500),
+        AbortSignal.timeout(2000),
+        AbortSignal.timeout(2500),
+      ])
     )
     expect(results).toStrictEqual([
       3628800, 2432902008176640000, 2.6525285981219103e32, 8.159152832478977e47,
index e1b386a47190edf02bfb577ecc4f168c8fd0da87..48e2fed45c48ebd10f6013b4e2f8020502e6a023 100644 (file)
@@ -25,13 +25,21 @@ describe('Dynamic cluster pool test suite', () => {
   })
 
   it('Verify that the function is executed in a worker cluster', async () => {
-    let result = await pool.execute({
-      function: TaskFunctions.fibonacci,
-    })
+    let result = await pool.execute(
+      {
+        function: TaskFunctions.fibonacci,
+      },
+      'default',
+      AbortSignal.timeout(2000)
+    )
     expect(result).toBe(354224848179262000000)
-    result = await pool.execute({
-      function: TaskFunctions.factorial,
-    })
+    result = await pool.execute(
+      {
+        function: TaskFunctions.factorial,
+      },
+      'default',
+      AbortSignal.timeout(2000)
+    )
     expect(result).toBe(9.33262154439441e157)
   })
 
index e41a8a6fcd56b11cbfec73b1c8092e188ec7e81d..a3f69131acc8bfcfd21867f131e89e1e1fed7c07 100644 (file)
@@ -70,13 +70,21 @@ describe('Fixed cluster pool test suite', () => {
   })
 
   it('Verify that the function is executed in a worker cluster', async () => {
-    let result = await pool.execute({
-      function: TaskFunctions.fibonacci,
-    })
+    let result = await pool.execute(
+      {
+        function: TaskFunctions.fibonacci,
+      },
+      'default',
+      AbortSignal.timeout(2000)
+    )
     expect(result).toBe(354224848179262000000)
-    result = await pool.execute({
-      function: TaskFunctions.factorial,
-    })
+    result = await pool.execute(
+      {
+        function: TaskFunctions.factorial,
+      },
+      'default',
+      AbortSignal.timeout(2000)
+    )
     expect(result).toBe(9.33262154439441e157)
   })
 
index d78ebbd9439a3d6ea8facf75f149ed2de5029106..da4d5eae8f61fcd5cb18e3924497ce149b0ee77a 100644 (file)
@@ -25,13 +25,21 @@ describe('Dynamic thread pool test suite', () => {
   })
 
   it('Verify that the function is executed in a worker thread', async () => {
-    let result = await pool.execute({
-      function: TaskFunctions.fibonacci,
-    })
+    let result = await pool.execute(
+      {
+        function: TaskFunctions.fibonacci,
+      },
+      'default',
+      AbortSignal.timeout(2000)
+    )
     expect(result).toBe(354224848179262000000)
-    result = await pool.execute({
-      function: TaskFunctions.factorial,
-    })
+    result = await pool.execute(
+      {
+        function: TaskFunctions.factorial,
+      },
+      'default',
+      AbortSignal.timeout(2000)
+    )
     expect(result).toBe(9.33262154439441e157)
   })
 
index ac7937095840a3ba7ae4d1b988d511f79e0e02e6..f83147c66dd841d5f36f7b9ca3375d0c4a36b6a2 100644 (file)
@@ -69,13 +69,21 @@ describe('Fixed thread pool test suite', () => {
   })
 
   it('Verify that the function is executed in a worker thread', async () => {
-    let result = await pool.execute({
-      function: TaskFunctions.fibonacci,
-    })
+    let result = await pool.execute(
+      {
+        function: TaskFunctions.fibonacci,
+      },
+      'default',
+      AbortSignal.timeout(2000)
+    )
     expect(result).toBe(354224848179262000000)
-    result = await pool.execute({
-      function: TaskFunctions.factorial,
-    })
+    result = await pool.execute(
+      {
+        function: TaskFunctions.factorial,
+      },
+      'default',
+      AbortSignal.timeout(2000)
+    )
     expect(result).toBe(9.33262154439441e157)
   })