Skip to content

Commit

Permalink
docs and tests for new features
Browse files Browse the repository at this point in the history
  • Loading branch information
nullsoepic committed Sep 24, 2024
1 parent e3b3bcb commit 111eeef
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 5 deletions.
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class PriorityQueue<T> {
): Promise<T> {
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);
Expand Down
58 changes: 57 additions & 1 deletion tests/priority-queue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -106,4 +106,60 @@ describe('PriorityQueue', () => {

expect(await result2).toBe(2);
});

it('should check if a task is queued', async () => {
const queue = new PriorityQueue<number>(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<number>(1);
const longTask = new Promise<number>((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<number>(1);
const longTask = new Promise<number>((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<number>(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');
});
});

0 comments on commit 111eeef

Please sign in to comment.