From: Jérôme Benoit Date: Thu, 4 May 2023 22:06:49 +0000 (+0200) Subject: refactor: cleanup queue API X-Git-Tag: v2.4.12~25 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=4d8bf9e40e07bd233be4494fda4e4270fdd8a355;p=poolifier.git refactor: cleanup queue API Signed-off-by: Jérôme Benoit --- diff --git a/src/pools/abstract-pool.ts b/src/pools/abstract-pool.ts index 8ab6b26e..06cdc3cd 100644 --- a/src/pools/abstract-pool.ts +++ b/src/pools/abstract-pool.ts @@ -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 { diff --git a/src/queue.ts b/src/queue.ts index 5cc13fcd..11766e4e 100644 --- a/src/queue.ts +++ b/src/queue.ts @@ -6,20 +6,24 @@ export class Queue { 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 { } return item } - - size (): number { - return this.tail - this.head - } } diff --git a/tests/pools/abstract/abstract-pool.test.js b/tests/pools/abstract/abstract-pool.test.js index eba4a896..366d4965 100644 --- a/tests/pools/abstract/abstract-pool.test.js +++ b/tests/pools/abstract/abstract-pool.test.js @@ -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() }) diff --git a/tests/pools/cluster/fixed.test.js b/tests/pools/cluster/fixed.test.js index 49487685..59f76802 100644 --- a/tests/pools/cluster/fixed.test.js +++ b/tests/pools/cluster/fixed.test.js @@ -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() }) diff --git a/tests/pools/thread/fixed.test.js b/tests/pools/thread/fixed.test.js index 7e7a74cf..9187b817 100644 --- a/tests/pools/thread/fixed.test.js +++ b/tests/pools/thread/fixed.test.js @@ -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() })