|
| 1 | +import { AsyncQueue } from './AsyncQueue'; |
| 2 | +import { sleep } from './room/utils'; |
| 3 | + |
| 4 | +describe('asyncQueue', () => { |
| 5 | + it('runs multiple tasks in order', async () => { |
| 6 | + const queue = new AsyncQueue(); |
| 7 | + const tasksExecuted: number[] = []; |
| 8 | + |
| 9 | + for (let i = 0; i < 5; i++) { |
| 10 | + queue.run(async () => { |
| 11 | + await sleep(50); |
| 12 | + tasksExecuted.push(i); |
| 13 | + }); |
| 14 | + } |
| 15 | + await queue.flush(); |
| 16 | + expect(tasksExecuted).toMatchObject([0, 1, 2, 3, 4]); |
| 17 | + }); |
| 18 | + it('runs tasks sequentially and not in parallel', async () => { |
| 19 | + const queue = new AsyncQueue(); |
| 20 | + const results: number[] = []; |
| 21 | + for (let i = 0; i < 5; i++) { |
| 22 | + queue.run(async () => { |
| 23 | + results.push(i); |
| 24 | + await sleep(10); |
| 25 | + results.push(i); |
| 26 | + }); |
| 27 | + } |
| 28 | + await queue.flush(); |
| 29 | + expect(results).toMatchObject([0, 0, 1, 1, 2, 2, 3, 3, 4, 4]); |
| 30 | + }); |
| 31 | + it('continues executing tasks if one task throws an error', async () => { |
| 32 | + const queue = new AsyncQueue(); |
| 33 | + |
| 34 | + let task1threw = false; |
| 35 | + let task2Executed = false; |
| 36 | + |
| 37 | + queue |
| 38 | + .run(async () => { |
| 39 | + await sleep(100); |
| 40 | + throw Error('task 1 throws'); |
| 41 | + }) |
| 42 | + .catch(() => { |
| 43 | + task1threw = true; |
| 44 | + }); |
| 45 | + |
| 46 | + await queue |
| 47 | + .run(async () => { |
| 48 | + task2Executed = true; |
| 49 | + }) |
| 50 | + .catch(() => { |
| 51 | + fail('task 2 should not have thrown'); |
| 52 | + }); |
| 53 | + |
| 54 | + expect(task1threw).toBeTruthy(); |
| 55 | + expect(task2Executed).toBeTruthy(); |
| 56 | + }); |
| 57 | + it('returns the result of the task', async () => { |
| 58 | + const queue = new AsyncQueue(); |
| 59 | + |
| 60 | + const result = await queue.run(async () => { |
| 61 | + await sleep(10); |
| 62 | + return 'result'; |
| 63 | + }); |
| 64 | + |
| 65 | + expect(result).toBe('result'); |
| 66 | + }); |
| 67 | + it('returns only when the enqueued task and all previous tasks have completed', async () => { |
| 68 | + const queue = new AsyncQueue(); |
| 69 | + const tasksExecuted: number[] = []; |
| 70 | + for (let i = 0; i < 10; i += 1) { |
| 71 | + queue.run(async () => { |
| 72 | + await sleep(10); |
| 73 | + tasksExecuted.push(i); |
| 74 | + return i; |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + const result = await queue.run(async () => { |
| 79 | + await sleep(10); |
| 80 | + tasksExecuted.push(999); |
| 81 | + return 'result'; |
| 82 | + }); |
| 83 | + |
| 84 | + expect(result).toBe('result'); |
| 85 | + expect(tasksExecuted).toMatchObject([...new Array(10).fill(0).map((_, idx) => idx), 999]); |
| 86 | + }); |
| 87 | + it('can handle queue sizes of up to 10_000 tasks', async () => { |
| 88 | + const queue = new AsyncQueue(); |
| 89 | + const tasksExecuted: number[] = []; |
| 90 | + |
| 91 | + for (let i = 0; i < 10_000; i++) { |
| 92 | + queue.run(async () => { |
| 93 | + tasksExecuted.push(i); |
| 94 | + }); |
| 95 | + } |
| 96 | + await queue.flush(); |
| 97 | + expect(tasksExecuted).toMatchObject(new Array(10_000).fill(0).map((_, idx) => idx)); |
| 98 | + }); |
| 99 | +}); |
0 commit comments