-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathTeenyStatistics.ts
327 lines (277 loc) · 9.71 KB
/
TeenyStatistics.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import assert from 'assert';
import {afterEach, before, beforeEach, describe, it} from 'mocha';
import * as sinon from 'sinon';
import {
TeenyStatistics,
TeenyStatisticsOptions,
TeenyStatisticsWarning,
} from '../src/TeenyStatistics';
function hooksForEnvCleanupInThisContext() {
const prevEnvMap: Map<string, string | undefined> = new Map([
['TEENY_REQUEST_WARN_CONCURRENT_REQUESTS', undefined],
]);
before(() => {
prevEnvMap.forEach((v, k, map) => {
map.set(k, process.env[k]);
});
});
afterEach(() => {
prevEnvMap.forEach((v, k) => {
if (v === undefined) {
delete process.env[k];
return;
}
process.env[k] = v;
});
});
}
describe('TeenyStatistics', () => {
const sandbox = sinon.createSandbox();
let emitWarnStub: sinon.SinonStub;
beforeEach(() => {
emitWarnStub = sandbox.stub(process, 'emitWarning');
});
afterEach(() => {
sandbox.restore();
});
describe('constructor', () => {
hooksForEnvCleanupInThisContext();
it('should have default concurrent requests', () => {
assert.strictEqual(
TeenyStatistics.DEFAULT_WARN_CONCURRENT_REQUESTS,
5000
);
});
it('should use predefined options by default', () => {
const t = new TeenyStatistics();
assert.deepStrictEqual(t['_options'], {concurrentRequests: 5e3});
});
it('should allow constructor override', () => {
const opts: TeenyStatisticsOptions = {concurrentRequests: 99};
const t = new TeenyStatistics(Object.assign({}, opts));
assert.deepStrictEqual(t['_options'], opts);
});
it('should allow env var override', () => {
process.env.TEENY_REQUEST_WARN_CONCURRENT_REQUESTS = '42';
const t = new TeenyStatistics();
assert.deepStrictEqual(t['_options'], {concurrentRequests: 42});
});
it('should prefer constructor over env var override', () => {
process.env.TEENY_REQUEST_WARN_CONCURRENT_REQUESTS = '123';
const opts: TeenyStatisticsOptions = {concurrentRequests: 321};
const t = new TeenyStatistics(Object.assign({}, opts));
assert.deepStrictEqual(t['_options'], opts);
});
});
describe('getOptions', () => {
it('should return the options, including defaults', () => {
const t = new TeenyStatistics();
assert.deepStrictEqual(t.getOptions(), {
concurrentRequests: TeenyStatistics.DEFAULT_WARN_CONCURRENT_REQUESTS,
});
});
it('should return the non-default options', () => {
const opts1: TeenyStatisticsOptions = {concurrentRequests: 123};
const t = new TeenyStatistics(Object.assign({}, opts1));
assert.deepStrictEqual(t.getOptions(), opts1);
});
it('should return a copy of the options', () => {
const t = new TeenyStatistics();
assert.notStrictEqual(t.getOptions(), t['_options']);
});
});
describe('setOptions', () => {
hooksForEnvCleanupInThisContext();
it('should be optional and set to defaults', () => {
const opts1: TeenyStatisticsOptions = {concurrentRequests: 123};
const t = new TeenyStatistics(Object.assign({}, opts1));
t.setOptions();
assert.deepStrictEqual(t['_options'], {concurrentRequests: 5e3});
});
it('should override previously set using options', () => {
const opts1: TeenyStatisticsOptions = {concurrentRequests: 123};
const opts2: TeenyStatisticsOptions = {concurrentRequests: 321};
const t = new TeenyStatistics(Object.assign({}, opts1));
t.setOptions(Object.assign({}, opts2));
assert.deepStrictEqual(t['_options'], opts2);
});
it('should override previously set using env var', () => {
const opts1: TeenyStatisticsOptions = {concurrentRequests: 123};
const t = new TeenyStatistics(Object.assign({}, opts1));
assert.deepStrictEqual(t['_options'], {concurrentRequests: 123});
process.env.TEENY_REQUEST_WARN_CONCURRENT_REQUESTS = '999';
t.setOptions();
assert.deepStrictEqual(t['_options'], {concurrentRequests: 999});
});
it('should return old options', () => {
const opts1: TeenyStatisticsOptions = {concurrentRequests: 123};
const opts2: TeenyStatisticsOptions = {concurrentRequests: 321};
const t = new TeenyStatistics(Object.assign({}, opts1));
const oldOpts = t.setOptions(Object.assign({}, opts2));
assert.deepStrictEqual(oldOpts, opts1);
});
});
describe('counters', () => {
it('should return counters', () => {
const t = new TeenyStatistics();
assert.deepStrictEqual(t.counters, {concurrentRequests: 0});
});
it('should be read-only', () => {
const t = new TeenyStatistics();
assert.throws(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(t as any).counters = {concurrentRequests: 99};
});
});
});
describe('request concurrency', () => {
let t: TeenyStatistics;
beforeEach(() => {
t = new TeenyStatistics();
});
it('should increment concurrency count', () => {
let numExpected = 0;
assert.strictEqual(t.counters.concurrentRequests, numExpected);
t.requestStarting();
numExpected++;
assert.strictEqual(t.counters.concurrentRequests, numExpected);
t.requestStarting();
numExpected++;
assert.strictEqual(t.counters.concurrentRequests, numExpected);
t.requestStarting();
numExpected++;
assert.strictEqual(t.counters.concurrentRequests, numExpected);
for (let i = 0; i < 100; i++) {
t.requestStarting();
numExpected++;
}
assert.strictEqual(t.counters.concurrentRequests, numExpected);
});
it('should decrement concurrency count', () => {
let numExpected = 0;
assert.strictEqual(t.counters.concurrentRequests, 0);
for (let i = 0; i < 100; i++) {
t.requestStarting();
numExpected++;
}
t.requestFinished();
numExpected--;
assert.strictEqual(t.counters.concurrentRequests, numExpected);
t.requestFinished();
numExpected--;
assert.strictEqual(t.counters.concurrentRequests, numExpected);
t.requestFinished();
numExpected--;
assert.strictEqual(t.counters.concurrentRequests, numExpected);
for (let i = numExpected; i > 0; i--) {
t.requestFinished();
numExpected--;
}
assert.strictEqual(t.counters.concurrentRequests, 0);
});
it('should emit a warning upon reaching threshold', () => {
for (let i = 0; i < 5e3 - 1; i++) {
t.requestStarting();
}
assert(emitWarnStub.notCalled);
t.requestStarting();
assert(
emitWarnStub.calledOnceWith(
sinon.match.instanceOf(TeenyStatisticsWarning)
)
);
});
it('should not re-emit once emitted', () => {
for (let i = 0; i < 5e3 - 1; i++) {
t.requestStarting();
}
assert(emitWarnStub.notCalled);
// first time emitting
t.requestStarting();
assert(
emitWarnStub.calledOnceWith(
sinon.match.instanceOf(TeenyStatisticsWarning)
)
);
// shouldn't emit on the next call (i.e. still greater than threshold)
t.requestStarting();
assert(emitWarnStub.calledOnce);
// shouldn't emit after twice the threshold (possible bad math/logic)
for (let i = 0; i < 5e3; i++) {
t.requestStarting();
}
assert(emitWarnStub.calledOnce);
});
it('should not re-emit when yoyoing threshold', () => {
for (let i = 0; i < 5e3 - 1; i++) {
t.requestStarting();
}
assert(emitWarnStub.notCalled);
// first time emitting
t.requestStarting();
assert(
emitWarnStub.calledOnceWith(
sinon.match.instanceOf(TeenyStatisticsWarning)
)
);
// let's bring the counter back down
for (let i = 5e3; i >= 0; i--) {
t.requestFinished();
}
// and bring it back again surpassing the threshold
for (let i = 0; i < 5e3 * 2; i++) {
t.requestStarting();
}
assert(emitWarnStub.calledOnce);
});
it('should emit a TeenyStatisticsWarning', () => {
for (let i = 0; i < 5e3; i++) {
t.requestStarting();
}
assert(emitWarnStub.calledOnce);
const warning = emitWarnStub.firstCall.args[0] as TeenyStatisticsWarning;
assert.strictEqual(warning.threshold, 5e3);
assert.strictEqual(warning.value, 5e3);
assert.strictEqual(
warning.type,
TeenyStatisticsWarning.CONCURRENT_REQUESTS
);
});
it('should emit a helpful message', () => {
for (let i = 0; i < 5e3; i++) {
t.requestStarting();
}
assert(emitWarnStub.calledOnce);
const errStr: string = emitWarnStub.firstCall.args[0].toString();
assert(
errStr.includes('Possible excessive concurrent requests detected.'),
'describes the nature of the warning'
);
assert(
errStr.includes('TEENY_REQUEST_WARN_CONCURRENT_REQUESTS'),
'mentions env var'
);
assert(
errStr.includes('concurrentRequests'),
'mentions concurrentRequests option'
);
assert(errStr.search(/\b0\b/) !== -1, 'mentions 0');
});
});
});