-
Notifications
You must be signed in to change notification settings - Fork 0
/
limiter.test.ts
224 lines (188 loc) · 5.2 KB
/
limiter.test.ts
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import { beforeAll, expect, test, jest } from "bun:test";
import { Limiter, LimiterRetryError } from "./limiter";
const delay = (ms: number) => new Promise((r) => setTimeout(r, ms));
const setup = ({ send, close, delay = 300 }: any) => {
return jest.fn(() => {
let closed = false;
let loading = false;
return {
process: jest.fn(async () => {
if (closed) throw new Error("Connection closed");
//if (loading) throw new Error("Connection in use");
loading = true;
await send();
await new Promise((resolve) => setTimeout(resolve, delay));
loading = false;
}),
close: jest.fn(async () => {
close();
closed = true;
}),
send,
};
});
};
test(
"Limiter: opens #limit of concurent connections",
async () => {
const connection = setup({
send: jest.fn(() => Promise.resolve()),
close: jest.fn(() => Promise.resolve()),
delay: 500,
});
const limiter = new Limiter({ limit: 3 });
const connections = Array.from({ length: 7 }, () => connection());
limiter.process(
...connections.map((c) => {
return c.process;
}),
);
await delay(0);
expect(limiter.length).toBe(3);
await delay(500);
expect(limiter.length).toBe(3);
await delay(500);
expect(limiter.length).toBe(1);
await limiter.done();
expect(connections[0].send).toBeCalledTimes(7);
},
{ timeout: 5000 },
);
test("Limiter: can add new connections to poll", async () => {
const connection = setup({
send: jest.fn(() => Promise.resolve()),
close: jest.fn(() => Promise.resolve()),
delay: 500,
});
const limiter = new Limiter({ limit: 3 });
limiter.process(connection().process);
limiter.process(connection().process);
limiter.process(connection().process);
limiter.process(connection().process, connection().process);
await delay(0);
expect(limiter.length).toBe(3);
await delay(500);
expect(limiter.length).toBe(2);
await delay(500);
expect(limiter.length).toBe(0);
});
test("Limiter: limit RPS - requests are evenly distributed", async () => {
const connection = setup({
send: jest.fn(() => {
return Promise.resolve();
}),
close: jest.fn(() => Promise.resolve()),
delay: 0,
});
const limiter = new Limiter({ limit: 20, rps: 10 });
const connections = Array.from({ length: 45 }, () => connection());
let count = 0;
const timestamps: number[] = [];
await limiter.process(
...connections.map((c) => {
return () => {
++count;
timestamps.push(Date.now());
return c.process();
};
}),
);
expect(count).toBe(45);
const diffsAvg =
timestamps
.map((t, i) => {
return i === 0 ? 100 : t - timestamps[i - 1];
})
.reduce((a, b) => a + b) / timestamps.length;
expect(diffsAvg).toBeGreaterThan(99);
expect(diffsAvg).toBeLessThan(102); // 100ms +- 2ms
});
test("Limiter: throws an error by deafult", async () => {
const connection = setup({
send: jest.fn(() => Promise.reject(1)),
close: jest.fn(() => Promise.resolve()),
delay: 500,
});
const limiter = new Limiter({ limit: 3 });
const connections = Array.from({ length: 6 }, () => connection());
try {
await limiter.process(
...connections.map((c) => {
return c.process;
}),
);
} catch (e) {
expect(e).toBe(1);
}
expect(limiter.length).toBe(0);
expect(connections[0].send).toBeCalledTimes(3);
});
test("Limiter: #onError, no trow", async () => {
const connection = setup({
send: jest.fn(() => Promise.reject(1)),
close: jest.fn(() => Promise.resolve()),
delay: 500,
});
const onError = jest.fn(() => {});
const limiter = new Limiter({
limit: 3,
onError,
});
const connections = Array.from({ length: 6 }, () => connection());
await limiter.process(
...connections.map((c) => {
return c.process;
}),
);
expect(limiter.length).toBe(0);
expect(connections[0].send).toBeCalledTimes(6);
expect(onError).toBeCalledTimes(6);
});
test("Limiter: #maxRetry, exit on fail", async () => {
const connection = setup({
send: () => Promise.reject(1),
close: jest.fn(() => Promise.resolve()),
delay: 0,
});
const limiter = new Limiter({
limit: 3,
maxRetry: 3,
});
const connections = Array.from({ length: 6 }, () => connection());
let count = 0;
try {
await limiter.process(
...connections.map((c) => {
++count;
return c.process;
}),
);
} catch (e) {
expect(e).toBeInstanceOf(LimiterRetryError);
}
expect(limiter.length).toBe(0);
});
test("Limiter: #onError, #maxRetry", async () => {
const connection = setup({
send: jest.fn(() => Promise.reject(new Error("Connection error"))),
close: jest.fn(() => Promise.resolve()),
delay: 0,
});
let error;
const onError = jest.fn((err) => {
error = err;
});
const limiter = new Limiter({
limit: 3,
maxRetry: 3,
onError,
});
const connections = Array.from({ length: 6 }, () => connection());
await limiter.process(
...connections.map((c) => {
return c.process;
}),
);
expect(onError).toBeCalledTimes(6);
expect(error).toBeInstanceOf(LimiterRetryError);
});