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
|
import waitForPromises from 'helpers/wait_for_promises';
import { rateLimitStreamRequests } from '~/streaming/rate_limit_stream_requests';
describe('rateLimitStreamRequests', () => {
const encoder = new TextEncoder('utf-8');
const createStreamResponse = (content = 'foo') =>
new ReadableStream({
pull(controller) {
controller.enqueue(encoder.encode(content));
controller.close();
},
});
const createFactory = (content) => {
return jest.fn(() => {
return Promise.resolve(createStreamResponse(content));
});
};
it('does nothing for zero total requests', () => {
const factory = jest.fn();
const requests = rateLimitStreamRequests({
factory,
total: 0,
});
expect(factory).toHaveBeenCalledTimes(0);
expect(requests.length).toBe(0);
});
it('does not exceed total requests', () => {
const factory = createFactory();
const requests = rateLimitStreamRequests({
factory,
immediateCount: 100,
maxConcurrentRequests: 100,
total: 2,
});
expect(factory).toHaveBeenCalledTimes(2);
expect(requests.length).toBe(2);
});
it('creates immediate requests', () => {
const factory = createFactory();
const requests = rateLimitStreamRequests({
factory,
maxConcurrentRequests: 2,
total: 2,
});
expect(factory).toHaveBeenCalledTimes(2);
expect(requests.length).toBe(2);
});
it('returns correct values', async () => {
const fixture = 'foobar';
const factory = createFactory(fixture);
const requests = rateLimitStreamRequests({
factory,
maxConcurrentRequests: 2,
total: 2,
});
const decoder = new TextDecoder('utf-8');
let result = '';
for await (const stream of requests) {
await stream.pipeTo(
new WritableStream({
// eslint-disable-next-line no-loop-func
write(content) {
result += decoder.decode(content);
},
}),
);
}
expect(result).toBe(fixture + fixture);
});
it('delays rate limited requests', async () => {
const factory = createFactory();
const requests = rateLimitStreamRequests({
factory,
maxConcurrentRequests: 2,
total: 3,
});
expect(factory).toHaveBeenCalledTimes(2);
expect(requests.length).toBe(3);
await waitForPromises();
expect(factory).toHaveBeenCalledTimes(3);
});
it('runs next request after previous has been fulfilled', async () => {
let res;
const factory = jest
.fn()
.mockImplementationOnce(
() =>
new Promise((resolve) => {
res = resolve;
}),
)
.mockImplementationOnce(() => Promise.resolve(createStreamResponse()));
const requests = rateLimitStreamRequests({
factory,
maxConcurrentRequests: 1,
total: 2,
});
expect(factory).toHaveBeenCalledTimes(1);
expect(requests.length).toBe(2);
await waitForPromises();
expect(factory).toHaveBeenCalledTimes(1);
res(createStreamResponse());
await waitForPromises();
expect(factory).toHaveBeenCalledTimes(2);
});
it('uses timer to schedule next request', async () => {
let res;
const factory = jest
.fn()
.mockImplementationOnce(
() =>
new Promise((resolve) => {
res = resolve;
}),
)
.mockImplementationOnce(() => Promise.resolve(createStreamResponse()));
const requests = rateLimitStreamRequests({
factory,
immediateCount: 1,
maxConcurrentRequests: 2,
total: 2,
timeout: 9999,
});
expect(factory).toHaveBeenCalledTimes(1);
expect(requests.length).toBe(2);
await waitForPromises();
expect(factory).toHaveBeenCalledTimes(1);
jest.runAllTimers();
await waitForPromises();
expect(factory).toHaveBeenCalledTimes(2);
res(createStreamResponse());
});
});
|