fix: fix back pressure detection
[poolifier.git] / tests / pools / abstract / abstract-pool.test.js
index 0929adc36a60dcfa0422ad6908041484736ee28c..ec552e986003d859f38fe0557d6d8727c23d7ceb 100644 (file)
@@ -765,31 +765,25 @@ describe('Abstract pool test suite', () => {
     await pool.destroy()
   })
 
-  it("Verify that pool event emitter 'full' event can register a callback", async () => {
-    const pool = new DynamicThreadPool(
+  it("Verify that pool event emitter 'ready' event can register a callback", async () => {
+    const pool = new DynamicClusterPool(
       Math.floor(numberOfWorkers / 2),
       numberOfWorkers,
-      './tests/worker-files/thread/testWorker.js'
+      './tests/worker-files/cluster/testWorker.js'
     )
-    const promises = new Set()
-    let poolFull = 0
     let poolInfo
-    pool.emitter.on(PoolEvents.full, (info) => {
-      ++poolFull
+    let poolReady = 0
+    pool.emitter.on(PoolEvents.ready, (info) => {
+      ++poolReady
       poolInfo = info
     })
-    for (let i = 0; i < numberOfWorkers * 2; i++) {
-      promises.add(pool.execute())
-    }
-    await Promise.all(promises)
-    // The `full` event is triggered when the number of submitted tasks at once reach the maximum number of workers in the dynamic pool.
-    // So in total numberOfWorkers * 2 - 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the dynamic pool with min = (max = numberOfWorkers) / 2.
-    expect(poolFull).toBe(numberOfWorkers * 2 - 1)
+    await waitPoolEvents(pool, PoolEvents.ready, 1)
+    expect(poolReady).toBe(1)
     expect(poolInfo).toStrictEqual({
       version,
       type: PoolTypes.dynamic,
-      worker: WorkerTypes.thread,
-      ready: expect.any(Boolean),
+      worker: WorkerTypes.cluster,
+      ready: true,
       strategy: WorkerChoiceStrategies.ROUND_ROBIN,
       minSize: expect.any(Number),
       maxSize: expect.any(Number),
@@ -803,25 +797,30 @@ describe('Abstract pool test suite', () => {
     await pool.destroy()
   })
 
-  it("Verify that pool event emitter 'ready' event can register a callback", async () => {
-    const pool = new DynamicClusterPool(
-      Math.floor(numberOfWorkers / 2),
+  it("Verify that pool event emitter 'busy' event can register a callback", async () => {
+    const pool = new FixedThreadPool(
       numberOfWorkers,
-      './tests/worker-files/cluster/testWorker.js'
+      './tests/worker-files/thread/testWorker.js'
     )
+    const promises = new Set()
+    let poolBusy = 0
     let poolInfo
-    let poolReady = 0
-    pool.emitter.on(PoolEvents.ready, (info) => {
-      ++poolReady
+    pool.emitter.on(PoolEvents.busy, (info) => {
+      ++poolBusy
       poolInfo = info
     })
-    await waitPoolEvents(pool, PoolEvents.ready, 1)
-    expect(poolReady).toBe(1)
+    for (let i = 0; i < numberOfWorkers * 2; i++) {
+      promises.add(pool.execute())
+    }
+    await Promise.all(promises)
+    // The `busy` event is triggered when the number of submitted tasks at once reach the number of fixed pool workers.
+    // So in total numberOfWorkers + 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the fixed pool.
+    expect(poolBusy).toBe(numberOfWorkers + 1)
     expect(poolInfo).toStrictEqual({
       version,
-      type: PoolTypes.dynamic,
-      worker: WorkerTypes.cluster,
-      ready: true,
+      type: PoolTypes.fixed,
+      worker: WorkerTypes.thread,
+      ready: expect.any(Boolean),
       strategy: WorkerChoiceStrategies.ROUND_ROBIN,
       minSize: expect.any(Number),
       maxSize: expect.any(Number),
@@ -835,28 +834,29 @@ describe('Abstract pool test suite', () => {
     await pool.destroy()
   })
 
-  it("Verify that pool event emitter 'busy' event can register a callback", async () => {
-    const pool = new FixedThreadPool(
+  it("Verify that pool event emitter 'full' event can register a callback", async () => {
+    const pool = new DynamicThreadPool(
+      Math.floor(numberOfWorkers / 2),
       numberOfWorkers,
       './tests/worker-files/thread/testWorker.js'
     )
     const promises = new Set()
-    let poolBusy = 0
+    let poolFull = 0
     let poolInfo
-    pool.emitter.on(PoolEvents.busy, (info) => {
-      ++poolBusy
+    pool.emitter.on(PoolEvents.full, (info) => {
+      ++poolFull
       poolInfo = info
     })
     for (let i = 0; i < numberOfWorkers * 2; i++) {
       promises.add(pool.execute())
     }
     await Promise.all(promises)
-    // The `busy` event is triggered when the number of submitted tasks at once reach the number of fixed pool workers.
-    // So in total numberOfWorkers + 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the fixed pool.
-    expect(poolBusy).toBe(numberOfWorkers + 1)
+    // The `full` event is triggered when the number of submitted tasks at once reach the maximum number of workers in the dynamic pool.
+    // So in total numberOfWorkers * 2 - 1 times for a loop submitting up to numberOfWorkers * 2 tasks to the dynamic pool with min = (max = numberOfWorkers) / 2.
+    expect(poolFull).toBe(numberOfWorkers * 2 - 1)
     expect(poolInfo).toStrictEqual({
       version,
-      type: PoolTypes.fixed,
+      type: PoolTypes.dynamic,
       worker: WorkerTypes.thread,
       ready: expect.any(Boolean),
       strategy: WorkerChoiceStrategies.ROUND_ROBIN,