|
1 |
| -import { describe, expect, it } from 'vitest'; |
| 1 | +import { expect, test } from 'vitest'; |
2 | 2 | import { PQueue } from './queue.ts';
|
3 | 3 |
|
4 |
| -describe('PQueue', () => { |
5 |
| - it('should process items in priority order', async () => { |
6 |
| - const queue = new PQueue<number>(); |
| 4 | +test('processes items in priority order', async () => { |
| 5 | + const queue = new PQueue<number>(); |
7 | 6 |
|
8 |
| - queue.push(1, 1); // Priority 1 |
9 |
| - queue.push(2, 3); // Priority 3 |
10 |
| - queue.push(3, 2); // Priority 2 |
| 7 | + queue.push(1, 1); // Priority 1 |
| 8 | + queue.push(2, 3); // Priority 3 |
| 9 | + queue.push(3, 2); // Priority 2 |
11 | 10 |
|
12 |
| - expect(await queue.shift()).toBe(2); // Highest priority |
13 |
| - expect(await queue.shift()).toBe(3); // Next highest priority |
14 |
| - expect(await queue.shift()).toBe(1); // Lowest priority |
15 |
| - }); |
| 11 | + expect(await queue.shift()).toBe(2); // Highest priority |
| 12 | + expect(await queue.shift()).toBe(3); // Next highest priority |
| 13 | + expect(await queue.shift()).toBe(1); // Lowest priority |
| 14 | +}); |
16 | 15 |
|
17 |
| - it('should handle async iteration', async () => { |
18 |
| - const queue = new PQueue<number>(); |
| 16 | +test('handles async iteration', async () => { |
| 17 | + const queue = new PQueue<number>(); |
19 | 18 |
|
20 |
| - queue.push(1, 1); |
21 |
| - queue.push(2, 2); |
22 |
| - queue.push(3, 3); |
| 19 | + queue.push(1, 1); |
| 20 | + queue.push(2, 2); |
| 21 | + queue.push(3, 3); |
23 | 22 |
|
24 |
| - const results: number[] = []; |
25 |
| - for await (const item of queue) { |
26 |
| - results.push(item); |
27 |
| - if (results.length === 3) break; |
28 |
| - } |
| 23 | + const results: number[] = []; |
| 24 | + for await (const item of queue) { |
| 25 | + results.push(item); |
| 26 | + if (results.length === 3) break; |
| 27 | + } |
29 | 28 |
|
30 |
| - expect(results).toEqual([3, 2, 1]); |
31 |
| - }); |
| 29 | + expect(results).toEqual([3, 2, 1]); |
| 30 | +}); |
32 | 31 |
|
33 |
| - it('should resolve pending shifts when items are added', async () => { |
34 |
| - const queue = new PQueue<number>(); |
| 32 | +test('resolves pending shifts when items are added', async () => { |
| 33 | + const queue = new PQueue<number>(); |
35 | 34 |
|
36 |
| - const shiftPromise = queue.shift(); |
37 |
| - queue.push(42, 1); |
| 35 | + const shiftPromise = queue.shift(); |
| 36 | + queue.push(42, 1); |
38 | 37 |
|
39 |
| - expect(await shiftPromise).toBe(42); |
40 |
| - }); |
| 38 | + expect(await shiftPromise).toBe(42); |
| 39 | +}); |
41 | 40 |
|
42 |
| - it('should throw an error when pushing to a closed queue', () => { |
43 |
| - const queue = new PQueue<number>(); |
44 |
| - queue.close(); |
| 41 | +test('throws an error when pushing to a closed queue', () => { |
| 42 | + const queue = new PQueue<number>(); |
| 43 | + queue.close(); |
45 | 44 |
|
46 |
| - expect(() => queue.push(1)).toThrow('Queue closed'); |
47 |
| - }); |
| 45 | + expect(() => queue.push(1)).toThrow('Queue closed'); |
| 46 | +}); |
48 | 47 |
|
49 |
| - it('should throw an error when shifting from a closed queue', async () => { |
50 |
| - const queue = new PQueue<number>(); |
51 |
| - queue.close(); |
| 48 | +test('throws an error when shifting from a closed queue', async () => { |
| 49 | + const queue = new PQueue<number>(); |
| 50 | + queue.close(); |
52 | 51 |
|
53 |
| - await expect(queue.shift()).rejects.toThrow('Queue closed'); |
54 |
| - }); |
| 52 | + await expect(queue.shift()).rejects.toThrow('Queue closed'); |
| 53 | +}); |
55 | 54 |
|
56 |
| - it('should keep unconsumed messages after consumer restart', async () => { |
57 |
| - const queue = new PQueue<number>(); |
| 55 | +test('keeps unconsumed messages after consumer restart', async () => { |
| 56 | + const queue = new PQueue<number>(); |
58 | 57 |
|
59 |
| - const shiftPromise = queue.shift(); |
60 |
| - queue.restartConsumer(); |
61 |
| - queue.push(1, 1); |
62 |
| - queue.push(2, 2); |
| 58 | + const shiftPromise = queue.shift(); |
| 59 | + queue.restartConsumer(); |
| 60 | + queue.push(1, 1); |
| 61 | + queue.push(2, 2); |
63 | 62 |
|
64 |
| - await expect(shiftPromise).rejects.toThrow('Consumer restarted'); |
| 63 | + await expect(shiftPromise).rejects.toThrow('Consumer restarted'); |
65 | 64 |
|
66 |
| - expect(queue.size).toBe(2); |
67 |
| - expect(await queue.shift()).toBe(2); |
68 |
| - expect(await queue.shift()).toBe(1); |
69 |
| - expect(queue.size).toBe(0); |
70 |
| - }); |
| 65 | + expect(queue.size).toBe(2); |
| 66 | + expect(await queue.shift()).toBe(2); |
| 67 | + expect(await queue.shift()).toBe(1); |
| 68 | + expect(queue.size).toBe(0); |
71 | 69 | });
|
0 commit comments