Repository navigation
Expand file tree
/
Copy pathcreatePromiseQueue.test.ts
More file actions
123 lines (90 loc) · 3.14 KB
/
Copy pathcreatePromiseQueue.test.ts
File metadata and controls
123 lines (90 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { describe, expect, it, vi } from "vitest";
import { createPromiseQueue } from "./createPromiseQueue.ts";
function asyncSpy(implementation: () => unknown = () => {}) {
return vi.fn().mockImplementation(async () => implementation());
}
function syncSpy(implementation: () => unknown = () => {}) {
return vi.fn().mockImplementation(() => implementation());
}
describe("createPromiseQueue", async () => {
it("should start jobs immediately before hitting the concurrency limit", async () => {
const queue = createPromiseQueue(3);
const job1 = asyncSpy();
const job2 = asyncSpy();
const job3 = asyncSpy();
void queue.add(job1);
void queue.add(job2);
void queue.add(job3);
expect(job1).toHaveBeenCalled();
expect(job2).toHaveBeenCalled();
expect(job3).toHaveBeenCalled();
});
it("should not start a job immediately after hitting the concurrency limit", async () => {
const queue = createPromiseQueue(2);
const job1 = asyncSpy();
const job2 = asyncSpy();
const job3 = asyncSpy();
void queue.add(job1);
void queue.add(job2);
void queue.add(job3);
expect(job3).not.toHaveBeenCalled();
});
it("should start a next job after going below the concurrency limit", async () => {
const queue = createPromiseQueue(2);
const job1 = asyncSpy();
const job2 = asyncSpy();
const job3 = asyncSpy();
void queue.add(job1);
const queuedJob2 = queue.add(job2);
void queue.add(job3);
expect(job3).not.toHaveBeenCalled();
await queuedJob2;
expect(job3).toHaveBeenCalled();
});
it("should resolve with the original result", async () => {
const queue = createPromiseQueue(2);
const job = asyncSpy(() => 42);
const queuedJob = queue.add(job);
await expect(queuedJob).resolves.toBe(42);
});
it("should reject with the original error", async () => {
const queue = createPromiseQueue(2);
const error = new Error("My error.");
const job = asyncSpy(() => {
throw error;
});
const queuedJob = queue.add(job);
await expect(queuedJob).rejects.toBe(error);
});
it("should drain pending jobs after a rejection", async () => {
const queue = createPromiseQueue(1);
const job1 = asyncSpy(() => {
throw new Error("fail");
});
const job2 = asyncSpy(() => "ok");
const q1 = queue.add(job1);
const q2 = queue.add(job2);
await expect(q1).rejects.toThrow("fail");
await expect(q2).resolves.toBe("ok");
});
it("should handle a synchronously throwing job", async () => {
const queue = createPromiseQueue(2);
const error = new Error("sync throw");
const job = syncSpy(() => {
throw error;
});
const queuedJob = queue.add(job);
await expect(queuedJob).rejects.toBe(error);
});
it("should drain pending jobs after a synchronous throw", async () => {
const queue = createPromiseQueue(1);
const job1 = syncSpy(() => {
throw new Error("sync fail");
});
const job2 = asyncSpy(() => "ok");
const q1 = queue.add(job1);
const q2 = queue.add(job2);
await expect(q1).rejects.toThrow("sync fail");
await expect(q2).resolves.toBe("ok");
});
});