refactor: cleanup queue API
authorJérôme Benoit <jerome.benoit@sap.com>
Thu, 4 May 2023 22:06:49 +0000 (00:06 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Thu, 4 May 2023 22:06:49 +0000 (00:06 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/pools/abstract-pool.ts
src/queue.ts
tests/pools/abstract/abstract-pool.test.js
tests/pools/cluster/fixed.test.js
tests/pools/thread/fixed.test.js

index 8ab6b26e4f385f54f7c46d5cc51f1aa5f5ea0212..06cdc3cd5d635c1e13b209228631bea7e6960fd6 100644 (file)
@@ -198,7 +198,7 @@ export abstract class AbstractPool<
       return 0
     }
     return this.workerNodes.reduce(
-      (accumulator, workerNode) => accumulator + workerNode.tasksQueue.size(),
+      (accumulator, workerNode) => accumulator + workerNode.tasksQueue.size,
       0
     )
   }
@@ -637,7 +637,7 @@ export abstract class AbstractPool<
   }
 
   private tasksQueueSize (workerNodeKey: number): number {
-    return this.workerNodes[workerNodeKey].tasksQueue.size()
+    return this.workerNodes[workerNodeKey].tasksQueue.size
   }
 
   private flushTasksQueue (workerNodeKey: number): void {
index 5cc13fcd27e7ea86e3bf504d5b5e3a110665b85d..11766e4e9109dc4356be0a4b589ee5e5ed9b7bd3 100644 (file)
@@ -6,20 +6,24 @@ export class Queue<T> {
   private head: number
   private tail: number
 
-  constructor () {
+  public constructor () {
     this.items = {}
     this.head = 0
     this.tail = 0
   }
 
-  enqueue (item: T): number {
+  public get size (): number {
+    return this.tail - this.head
+  }
+
+  public enqueue (item: T): number {
     this.items[this.tail] = item
     this.tail++
-    return this.size()
+    return this.size
   }
 
-  dequeue (): T | undefined {
-    if (this.size() <= 0) return undefined
+  public dequeue (): T | undefined {
+    if (this.size <= 0) return undefined
     const item = this.items[this.head]
     // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
     delete this.items[this.head]
@@ -30,8 +34,4 @@ export class Queue<T> {
     }
     return item
   }
-
-  size (): number {
-    return this.tail - this.head
-  }
 }
index eba4a896331ba0760045b5e626fe988a5e0a3ece..366d4965960f6eddfb17856331fab8542b44fce3 100644 (file)
@@ -285,7 +285,7 @@ describe('Abstract pool test suite', () => {
     for (const workerNode of pool.workerNodes) {
       expect(workerNode.tasksQueue).toBeDefined()
       expect(workerNode.tasksQueue).toBeInstanceOf(Queue)
-      expect(workerNode.tasksQueue.size()).toBe(0)
+      expect(workerNode.tasksQueue.size).toBe(0)
     }
     await pool.destroy()
   })
index 494876857665e50fb3a025e212f76221bdd929ab..59f76802bb97a109795e92702e470c6a670543e0 100644 (file)
@@ -100,7 +100,7 @@ describe('Fixed cluster pool test suite', () => {
         queuePool.opts.tasksQueueOptions.concurrency
       )
       expect(workerNode.tasksUsage.run).toBe(0)
-      expect(workerNode.tasksQueue.size()).toBeGreaterThan(0)
+      expect(workerNode.tasksQueue.size).toBeGreaterThan(0)
     }
     expect(queuePool.numberOfRunningTasks).toBe(numberOfWorkers)
     expect(queuePool.numberOfQueuedTasks).toBe(
@@ -110,7 +110,7 @@ describe('Fixed cluster pool test suite', () => {
     for (const workerNode of queuePool.workerNodes) {
       expect(workerNode.tasksUsage.running).toBe(0)
       expect(workerNode.tasksUsage.run).toBeGreaterThan(0)
-      expect(workerNode.tasksQueue.size()).toBe(0)
+      expect(workerNode.tasksQueue.size).toBe(0)
     }
     promises.clear()
   })
index 7e7a74cfbdcdf226523eb613d0aa5b72d54266b0..9187b817d1bf749ae956d91a8afcb9436a48b3ab 100644 (file)
@@ -100,7 +100,7 @@ describe('Fixed thread pool test suite', () => {
         queuePool.opts.tasksQueueOptions.concurrency
       )
       expect(workerNode.tasksUsage.run).toBe(0)
-      expect(workerNode.tasksQueue.size()).toBeGreaterThan(0)
+      expect(workerNode.tasksQueue.size).toBeGreaterThan(0)
     }
     expect(queuePool.numberOfRunningTasks).toBe(numberOfThreads)
     expect(queuePool.numberOfQueuedTasks).toBe(
@@ -110,7 +110,7 @@ describe('Fixed thread pool test suite', () => {
     for (const workerNode of queuePool.workerNodes) {
       expect(workerNode.tasksUsage.running).toBe(0)
       expect(workerNode.tasksUsage.run).toBeGreaterThan(0)
-      expect(workerNode.tasksQueue.size()).toBe(0)
+      expect(workerNode.tasksQueue.size).toBe(0)
     }
     promises.clear()
   })