This repository has been archived by the owner on Sep 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 133
/
remote_throttler.js
181 lines (164 loc) · 6.73 KB
/
remote_throttler.js
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
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import { assert } from 'chai';
import Metrics from '../../src/metrics/metrics.js';
import MockLogger from '../lib/mock_logger';
import ConfigServer from '../lib/config_server';
import LocalMetricFactory from '../lib/metrics/local/metric_factory.js';
import LocalBackend from '../lib/metrics/local/backend.js';
import RemoteThrottler from '../../src/throttler/remote_throttler';
import sinon from 'sinon';
describe('RemoteThrottler should', () => {
let server: ConfigServer;
let logger: MockLogger;
let metrics: Metrics;
let throttler: RemoteThrottler;
let serviceName = 'service';
let operation = 'op';
let other_operation = 'oop';
let uuid = 'uuid';
let creditsUpdatedHook;
before(() => {
server = new ConfigServer().start();
});
after(() => {
server.close();
});
beforeEach(() => {
server.clearConfigs();
logger = new MockLogger();
metrics = new Metrics(new LocalMetricFactory());
creditsUpdatedHook = sinon.spy();
throttler = new RemoteThrottler(serviceName, {
refreshIntervalMs: 0,
initialDelayMs: 60000,
metrics: metrics,
logger: logger,
onCreditsUpdate: (...args) => creditsUpdatedHook(...args),
});
});
afterEach(() => {
throttler.close();
});
it('return false for isAllowed on initial call and return true once credits are initialized', done => {
throttler.setProcess({ uuid: uuid });
server.addCredits(serviceName, [{ operation: operation, credits: 3 }]);
creditsUpdatedHook = _throttler => {
assert.isOk(_throttler.isAllowed(operation));
assert.equal(_throttler._credits[operation], 2);
assert.equal(LocalBackend.counterValue(metrics.throttlerUpdateSuccess), 1);
done();
};
assert.isNotOk(throttler.isAllowed(operation));
throttler._refreshCredits();
});
it('log an error if _refreshCredits is called prior to UUID being set', () => {
throttler._fetchCredits = sinon.spy();
throttler._refreshCredits();
assert.equal(logger._errorMsgs.length, 1);
sinon.assert.notCalled(throttler._fetchCredits);
});
it('not fetch credits if uuid is invalid', () => {
throttler._fetchCredits = sinon.spy();
throttler.setProcess({ uuid: null });
throttler._refreshCredits();
assert.equal(logger._errorMsgs.length, 1, `errors=${logger._errorMsgs}`);
sinon.assert.notCalled(throttler._fetchCredits);
});
it("return false for _isAllowed if operation isn't in _credits or operation has no credits", () => {
assert.isNotOk(
throttler._isAllowed(operation),
'operation is not set so operation should not be allowed'
);
throttler._credits[operation] = 0;
assert.isNotOk(throttler._isAllowed(operation), 'operation is set but lacks credit');
});
it("return false for isAllowed if operation doesn't have enough credits", () => {
throttler._credits[operation] = 0.5;
assert.isNotOk(throttler._isAllowed(operation));
});
it('succeed when we retrieve credits for multiple operations', done => {
throttler.setProcess({ uuid: uuid });
server.addCredits(serviceName, [
{ operation: operation, credits: 5 },
{ operation: other_operation, credits: 3 },
]);
throttler._credits[operation] = 0;
throttler._credits[other_operation] = 0;
creditsUpdatedHook = _throttler => {
assert.isOk(_throttler.isAllowed(operation));
assert.equal(_throttler._credits[operation], 4);
assert.isOk(_throttler.isAllowed(other_operation));
assert.equal(_throttler._credits[other_operation], 2);
assert.equal(LocalBackend.counterValue(metrics.throttlerUpdateSuccess), 1);
done();
};
throttler._refreshCredits();
});
it('emit failure metric on failing to query for credits', done => {
throttler.setProcess({ uuid: uuid });
throttler._credits[operation] = 0;
metrics.throttlerUpdateFailure.increment = function() {
assert.equal(logger._errorMsgs.length, 1, `errors=${logger._errorMsgs}`);
done();
};
throttler._host = 'Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch';
throttler._refreshCredits();
});
it('emit failure metric on failing to parse bad http json response', done => {
throttler.setProcess({ uuid: uuid });
throttler._credits[operation] = 0;
metrics.throttlerUpdateFailure.increment = function() {
assert.equal(logger._errorMsgs.length, 1, `errors=${logger._errorMsgs}`);
done();
};
server.addCredits(serviceName, 'not-json');
throttler._refreshCredits();
});
it('emit failure metric when server returns an invalid response', done => {
throttler.setProcess({ uuid: uuid });
throttler._credits[operation] = 0;
metrics.throttlerUpdateFailure.increment = function() {
assert.equal(logger._errorMsgs.length, 1, `errors=${logger._errorMsgs}`);
done();
};
throttler._refreshCredits();
});
it('not fetch credits if no operations have been seen', () => {
throttler = new RemoteThrottler(serviceName);
throttler._fetchCredits = sinon.spy();
throttler.setProcess({ uuid: uuid });
throttler._refreshCredits();
sinon.assert.notCalled(throttler._fetchCredits);
throttler.close();
});
it('refresh credits after _afterInitialDelay is called', done => {
throttler.setProcess({ uuid: uuid });
throttler._credits[operation] = 0;
server.addCredits(serviceName, [{ operation: operation, credits: 5 }]);
creditsUpdatedHook = _throttler => {
assert.isOk(_throttler.isAllowed(operation));
assert.equal(_throttler._credits[operation], 4);
assert.equal(LocalBackend.counterValue(metrics.throttlerUpdateSuccess), 1);
done();
};
throttler._afterInitialDelay();
});
});