From 111eeef26acfb5bab857035b94849ce57ed8dccc Mon Sep 17 00:00:00 2001 From: nullsoepic Date: Tue, 24 Sep 2024 20:57:10 +0200 Subject: [PATCH] docs and tests for new features --- README.md | 26 ++++++++++++++-- package.json | 2 +- src/index.ts | 2 +- tests/priority-queue.test.ts | 58 +++++++++++++++++++++++++++++++++++- 4 files changed, 83 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 2e0f0bc..a517470 100644 --- a/README.md +++ b/README.md @@ -10,20 +10,22 @@ - **Concurrency Control**: You can set the desired concurrency level to control how many tasks are processed simultaneously. - **Task Cancellation**: Supports cancelling tasks by their unique IDs. - **Error Handling**: Properly handles errors and rejections, ensuring that failed tasks do not block the queue. +- **Task Status Checking**: Provides methods to check if a task is queued, processing, or both. +- **Unique Task IDs**: Ensures that each task has a unique identifier for easy management and status checking. ### Installation To use `priqo` in your project, install it using your favourite package manager: ```bash -bun install priqo +npm install priqo ``` ### Usage Here is an example of how to use `priqo`: -```typescript +```ts import { PriorityQueue } from 'priqo'; const queue = new PriorityQueue(3); // Create a priority queue with a concurrency of 3 @@ -58,6 +60,11 @@ const result1 = await queue.enqueue( ); console.log(result1); // Output: 1 + +// Check task status +console.log(queue.isQueued('highPriorityTask')); // true or false +console.log(queue.isProcessing('longTask')); // true or false +console.log(queue.isProcessingOrQueued('anotherLongTask')); // true or false ``` ### API Documentation @@ -85,9 +92,24 @@ console.log(result1); // Output: 1 - Returns whether the queue is empty (both processing and waiting queues). - **`setConcurrency`**: + - `setConcurrency(size: number): void` - Sets the desired concurrency level of the queue. +- **`isQueued`**: + + - `isQueued(id: string): boolean` + - Checks if a task with the given ID is currently in the waiting queue. + +- **`isProcessing`**: + + - `isProcessing(id: string): boolean` + - Checks if a task with the given ID is currently being processed. + +- **`isProcessingOrQueued`**: + - `isProcessingOrQueued(id: string): boolean` + - Checks if a task with the given ID is either in the waiting queue or currently being processed. + ### Testing The library includes comprehensive tests to ensure its functionality. These tests are written and run using Bun's built-in testing framework. You can execute the tests using the `bun test` command. diff --git a/package.json b/package.json index c049f3b..bf18da8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "priqo", - "version": "1.1.0", + "version": "1.1.1", "description": "A asynchronous priority queue implementation", "main": "dist/index.js", "module": "dist/index.js", diff --git a/src/index.ts b/src/index.ts index fc4a9fe..6f8dc53 100644 --- a/src/index.ts +++ b/src/index.ts @@ -24,7 +24,7 @@ export class PriorityQueue { ): Promise { return new Promise((resolve, reject) => { if (this.isProcessingOrQueued(id)) { - reject(new Error(`Task ${id} is already being processed`)); + reject(new Error(`Task with id ${id} already exists`)); } this.queue.push({ fn, args, priority, id, resolve, reject }); this.queue.sort((a, b) => b.priority - a.priority); diff --git a/tests/priority-queue.test.ts b/tests/priority-queue.test.ts index 7a0c8b7..0c563fc 100644 --- a/tests/priority-queue.test.ts +++ b/tests/priority-queue.test.ts @@ -79,7 +79,7 @@ describe('PriorityQueue', () => { ); const result2 = queue.enqueue(async (x) => x, [2], 2, 'successfulTask'); - await expect(result1).rejects.toThrow('Task failed'); + expect(result1).rejects.toThrow('Task failed'); const res2 = await result2; expect(res2).toBe(2); }); @@ -106,4 +106,60 @@ describe('PriorityQueue', () => { expect(await result2).toBe(2); }); + + it('should check if a task is queued', async () => { + const queue = new PriorityQueue(1); + queue.enqueue(async (x) => x, [1], 1, 'task1'); + const result2 = queue.enqueue(async (x) => x, [2], 2, 'task2'); + + expect(queue.isQueued('task2')).toBe(true); + expect(queue.isQueued('task1')).toBe(false); + expect(queue.isQueued('nonexistent')).toBe(false); + + await result2; + }); + + it('should check if a task is processing', async () => { + const queue = new PriorityQueue(1); + const longTask = new Promise((resolve) => + setTimeout(() => resolve(1), 100) + ); + const result1 = queue.enqueue(async () => longTask, [], 1, 'task1'); + queue.enqueue(async (x) => x, [2], 2, 'task2'); + + expect(queue.isProcessing('task1')).toBe(true); + expect(queue.isProcessing('task2')).toBe(false); + expect(queue.isProcessing('nonexistent')).toBe(false); + + await result1; + }); + + it('should check if a task is queued or processing', async () => { + const queue = new PriorityQueue(1); + const longTask = new Promise((resolve) => + setTimeout(() => resolve(1), 100) + ); + const result1 = queue.enqueue(async () => longTask, [], 1, 'task1'); + queue.enqueue(async (x) => x, [2], 2, 'task2'); + + expect(queue.isProcessingOrQueued('task1')).toBe(true); + expect(queue.isProcessingOrQueued('task2')).toBe(true); + expect(queue.isProcessingOrQueued('nonexistent')).toBe(false); + + await result1; + }); + + it('should ensure task ids are unique', async () => { + const queue = new PriorityQueue(1); + queue.enqueue( + async () => new Promise((resolve) => setTimeout(() => resolve(1), 1000)), + [], + 1, + 'task1' + ); + + const res = queue.enqueue(async (x) => x, [2], 2, 'task1'); + + expect(res).rejects.toThrow('Task with id task1 already exists'); + }); });