|
| 1 | +import { delay } from '@uploadcare/api-client-utils' |
| 2 | +import { Queue } from '../../src/tools/Queue' |
| 3 | +import { expect } from '@jest/globals' |
| 4 | + |
| 5 | +const DELAY = 100 |
| 6 | +const TIME_TOLERANCE = DELAY / 2 |
| 7 | + |
| 8 | +describe('Queue', () => { |
| 9 | + describe('#add', () => { |
| 10 | + it('should return promise resolved with the same value', async () => { |
| 11 | + expect.assertions(1) |
| 12 | + const queue = new Queue(1) |
| 13 | + const promise = queue.add(() => Promise.resolve('result')) |
| 14 | + await expect(promise).resolves.toBe('result') |
| 15 | + }) |
| 16 | + |
| 17 | + it('should return promise rejected with the same error', async () => { |
| 18 | + expect.assertions(1) |
| 19 | + const queue = new Queue(1) |
| 20 | + const promise = queue.add(() => Promise.reject('result')) |
| 21 | + await expect(promise).rejects.toBe('result') |
| 22 | + }) |
| 23 | + }) |
| 24 | + |
| 25 | + it('should handle rejected promises', async () => { |
| 26 | + expect.assertions(2) |
| 27 | + const queue = new Queue(4) |
| 28 | + const promises = [ |
| 29 | + queue.add(() => Promise.resolve()), |
| 30 | + queue.add(() => Promise.reject()), |
| 31 | + queue.add(() => Promise.resolve()), |
| 32 | + queue.add(() => Promise.reject()) |
| 33 | + ] |
| 34 | + expect(queue.running).toBe(4) |
| 35 | + await Promise.allSettled(promises) |
| 36 | + expect(queue.running).toBe(0) |
| 37 | + }) |
| 38 | + |
| 39 | + it('should run tasks in LILO sequense', async () => { |
| 40 | + expect.assertions(1) |
| 41 | + const queue = new Queue(1) |
| 42 | + const order: number[] = [] |
| 43 | + const promises = [ |
| 44 | + queue.add(() => Promise.resolve().then(() => order.push(1))), |
| 45 | + queue.add(() => Promise.resolve().then(() => order.push(2))), |
| 46 | + queue.add(() => Promise.resolve().then(() => order.push(3))) |
| 47 | + ] |
| 48 | + await Promise.all(promises) |
| 49 | + await delay(0) |
| 50 | + expect(order).toEqual([1, 2, 3]) |
| 51 | + }) |
| 52 | + |
| 53 | + it('should run tasks concurrently', async () => { |
| 54 | + expect.assertions(12) |
| 55 | + const queue = new Queue(4) |
| 56 | + const times: number[] = [] |
| 57 | + const startTime = Date.now() |
| 58 | + const promises = Array.from({ length: 12 }).map(() => { |
| 59 | + return queue.add(() => |
| 60 | + delay(DELAY).then(() => times.push(Date.now() - startTime)) |
| 61 | + ) |
| 62 | + }) |
| 63 | + await Promise.all(promises) |
| 64 | + |
| 65 | + expect(Math.abs(times[0] - DELAY * 1)).toBeLessThan(TIME_TOLERANCE) |
| 66 | + expect(Math.abs(times[1] - DELAY * 1)).toBeLessThan(TIME_TOLERANCE) |
| 67 | + expect(Math.abs(times[2] - DELAY * 1)).toBeLessThan(TIME_TOLERANCE) |
| 68 | + expect(Math.abs(times[3] - DELAY * 1)).toBeLessThan(TIME_TOLERANCE) |
| 69 | + |
| 70 | + expect(Math.abs(times[4] - DELAY * 2)).toBeLessThan(TIME_TOLERANCE) |
| 71 | + expect(Math.abs(times[5] - DELAY * 2)).toBeLessThan(TIME_TOLERANCE) |
| 72 | + expect(Math.abs(times[6] - DELAY * 2)).toBeLessThan(TIME_TOLERANCE) |
| 73 | + expect(Math.abs(times[7] - DELAY * 2)).toBeLessThan(TIME_TOLERANCE) |
| 74 | + |
| 75 | + expect(Math.abs(times[8] - DELAY * 3)).toBeLessThan(TIME_TOLERANCE) |
| 76 | + expect(Math.abs(times[9] - DELAY * 3)).toBeLessThan(TIME_TOLERANCE) |
| 77 | + expect(Math.abs(times[10] - DELAY * 3)).toBeLessThan(TIME_TOLERANCE) |
| 78 | + expect(Math.abs(times[11] - DELAY * 3)).toBeLessThan(TIME_TOLERANCE) |
| 79 | + }) |
| 80 | + |
| 81 | + describe('get pending', () => { |
| 82 | + it('should be able to get pending tasks count', async () => { |
| 83 | + expect.assertions(3) |
| 84 | + const queue = new Queue(2) |
| 85 | + expect(queue.pending).toBe(0) |
| 86 | + const promises = [ |
| 87 | + queue.add(() => Promise.resolve()), |
| 88 | + queue.add(() => Promise.resolve()), |
| 89 | + queue.add(() => Promise.resolve()) |
| 90 | + ] |
| 91 | + // 2 task is running, 1 are pending |
| 92 | + expect(queue.pending).toBe(1) |
| 93 | + await Promise.all(promises) |
| 94 | + expect(queue.pending).toBe(0) |
| 95 | + }) |
| 96 | + }) |
| 97 | + |
| 98 | + describe('get running', () => { |
| 99 | + it('should be able to get running tasks count', async () => { |
| 100 | + expect.assertions(3) |
| 101 | + const queue = new Queue(2) |
| 102 | + expect(queue.running).toBe(0) |
| 103 | + const promises = [ |
| 104 | + queue.add(() => Promise.resolve()), |
| 105 | + queue.add(() => Promise.resolve()), |
| 106 | + queue.add(() => Promise.resolve()) |
| 107 | + ] |
| 108 | + expect(queue.running).toBe(2) |
| 109 | + await Promise.all(promises) |
| 110 | + expect(queue.running).toBe(0) |
| 111 | + }) |
| 112 | + }) |
| 113 | + |
| 114 | + describe('get concurrency', () => { |
| 115 | + it('should be able to get concurrency', async () => { |
| 116 | + expect.assertions(1) |
| 117 | + const queue = new Queue(2) |
| 118 | + expect(queue.concurrency).toBe(2) |
| 119 | + }) |
| 120 | + }) |
| 121 | + describe('set concurrency', () => { |
| 122 | + it('should be able to change concurrency', async () => { |
| 123 | + expect.assertions(9) |
| 124 | + const queue = new Queue(1) |
| 125 | + const times: number[] = [] |
| 126 | + const startTime = Date.now() |
| 127 | + const promises = Array.from({ length: 5 }).map(() => { |
| 128 | + return queue.add(() => |
| 129 | + delay(DELAY).then(() => times.push(Date.now() - startTime)) |
| 130 | + ) |
| 131 | + }) |
| 132 | + await delay(0) |
| 133 | + expect(queue.running).toBe(1) |
| 134 | + await promises[0] |
| 135 | + queue.concurrency = 2 |
| 136 | + expect(queue.concurrency).toBe(2) |
| 137 | + expect(queue.running).toBe(2) |
| 138 | + await Promise.all(promises) |
| 139 | + |
| 140 | + expect(Math.abs(times[0] - DELAY * 1)).toBeLessThan(TIME_TOLERANCE) |
| 141 | + |
| 142 | + expect(Math.abs(times[1] - DELAY * 2)).toBeLessThan(TIME_TOLERANCE) |
| 143 | + expect(Math.abs(times[2] - DELAY * 2)).toBeLessThan(TIME_TOLERANCE) |
| 144 | + |
| 145 | + expect(Math.abs(times[3] - DELAY * 3)).toBeLessThan(TIME_TOLERANCE) |
| 146 | + expect(Math.abs(times[4] - DELAY * 3)).toBeLessThan(TIME_TOLERANCE) |
| 147 | + |
| 148 | + expect(queue.running).toBe(0) |
| 149 | + }) |
| 150 | + }) |
| 151 | +}) |
0 commit comments