From 4d8bf9e40e07bd233be4494fda4e4270fdd8a355 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 5 May 2023 00:06:49 +0200 Subject: [PATCH] refactor: cleanup queue API MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/pools/abstract-pool.ts | 4 ++-- src/queue.ts | 18 +++++++++--------- tests/pools/abstract/abstract-pool.test.js | 2 +- tests/pools/cluster/fixed.test.js | 4 ++-- tests/pools/thread/fixed.test.js | 4 ++-- 5 files changed, 16 insertions(+), 16 deletions(-) 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() }) -- 2.34.1