-
Notifications
You must be signed in to change notification settings - Fork 3
/
spec.js
226 lines (222 loc) · 7.43 KB
/
spec.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
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
const dependencies = [
'formatter',
'sender',
'push',
'flush'
];
const stubs = {};
const originals = {};
const parameters = {};
const results = {};
const contexts = {};
const called = {};
function piggyback(dependency, fn) {
stubs[dependency] = function(...args) {
const result = originals[dependency].apply(this, args);
results[dependency] = fn(...args);
contexts[dependency] = this;
parameters[dependency] = args;
called[dependency] = true;
return result;
};
}
describe('SDC', () => {
let SDC;
let client = null;
const { random } = Math;
before(() => {
require('./lib/sample');
delete require.cache[require.resolve('.')];
require.cache[require.resolve('./lib/sample')].exports = (...args) => stubs.sample(...args);
dependencies.forEach((dependency) => {
const route = `./lib/${dependency}`;
originals[dependency] = require(route);
piggyback(dependency, () => null);
require.cache[require.resolve(route)].exports = function(...args) {
return stubs[dependency].apply(this, args);
};
});
SDC = require('.');
});
beforeEach(() => {
dependencies.forEach((dependency) => {
piggyback(dependency, () => null);
stubs.sample = () => null;
delete results[dependency];
delete contexts[dependency];
delete parameters[dependency];
delete called[dependency];
});
});
afterEach(() => {
Math.random = random;
client && client.destroy();
client = null;
});
after(() => {
dependencies.forEach((dependency) => {
delete require.cache[require.resolve(`./lib/${dependency}`)];
});
delete require.cache[require.resolve('./lib/sample')];
delete require.cache[require.resolve('.')];
});
it('Should have a static getter containing all send types', () => {
expect(SDC.TYPES).to.deep.equal({
count: 'count',
time: 'time',
gauge: 'gauge',
set: 'set',
histogram: 'histogram'
});
});
it('Should not accept changes to the static types object (freeze)', () => {
const { TYPES } = SDC;
TYPES.counter = 'count';
expect(TYPES.counter).to.be.undefined;
});
it('Should have properties passed by constructor options', () => {
const options = {
MTU: 1234,
timeout: 1050,
timer: null
};
client = new SDC(options);
expect(client).to.include({
MTU: 1234,
timeout: 1050,
timer: null
});
});
[
'count',
'time',
'gauge',
'set',
'histogram',
'generic'
].forEach(
(method) => it(
`Should expose method "${method}"`,
() => expect(new SDC()[method]).to.be.a('function')
)
);
it('Should have some (non prototype) functionality', () => {
client = new SDC();
expect(client.bulk).to.deep.equal([]);
expect(client.timer).to.equal(null);
expect(client.send).to.be.a('function');
expect(client.format).to.be.a('function');
expect(client.flush).to.be.a('function');
});
it('Should have specific metrics functions to call on generic function', () => {
client = new SDC();
const excepted = [];
const sent = ['A', 2, { key: 'balue' }];
client.generic = (...args) => excepted.push(...args);
[
'count',
'time',
'gauge',
'set',
'histogram'
].forEach((type) => {
excepted.length = 0;
client[type](...sent);
expect(excepted).to.deep.equal([type, ...sent]);
});
});
it('Should assign default tags to metrics', () => {
client = new SDC({ tags: { environment: 'production' } });
let _tags;
client.format = (type, key, value, { tags } = {}) => {
_tags = tags;
};
client.generic('count', 'a');
expect(_tags).to.include({ environment: 'production' });
});
it('Should accept options as last argument', () => {
client = new SDC();
let _tags;
client.format = (type, key, value, { tags } = {}) => {
_tags = tags;
};
client.generic('count', 'a', { tags: { environment: 'development' } });
expect(_tags).to.include({ environment: 'development' });
});
it('Should override default tags', () => {
client = new SDC({ tags: { environment: 'production' } });
let _tags;
client.format = (type, key, value, { tags } = {}) => {
_tags = tags;
};
client.generic('count', 'a', 1, { tags: { environment: 'development' } });
expect(_tags).to.include({ environment: 'development' });
});
it('Should push when sample is true', () => {
client = new SDC();
stubs.sample = () => true;
client.generic('count', 'a', 1, { rate: 0.4 });
expect(called.push).to.be.true;
});
it('Should push when sample is false, but enforceRate is true', () => {
client = new SDC({ enforceRate: true });
stubs.sample = () => true;
client.generic('count', 'a', 1, { rate: 0.4 });
expect(called.push).to.be.true;
});
it('Should override with local enforceRate', () => {
client = new SDC({ enforceRate: false });
stubs.sample = () => true;
client.generic('count', 'a', 1, { rate: 0.1, enforceRate: false });
expect(called.push).to.be.true;
});
it('Should skip when sample is false', () => {
client = new SDC();
stubs.sample = () => false;
client.generic('count', 'a', 1, { rate: 0.4 });
expect(called.push).to.be.undefined;
});
it('Should call push in context', () => {
client = new SDC();
client.generic('count', 'a');
expect(contexts.push).to.equal(client);
});
it('Should expose it\'s bulk size', () => {
client = new SDC();
const before = client.size;
client.generic('count', 'a');
expect(client.size).to.be.above(before);
});
it('Should return a number (bulk size)', () => {
client = new SDC();
const bulkSize = client.generic('count', 'a');
expect(bulkSize).to.be.a('number');
expect(bulkSize).to.equal(client.size);
});
it('Should follow functional pipeline', () => {
const order = [];
client = new SDC();
piggyback('push', () => order.push('push'));
const format = client.format;
client.format = function(...args) {
order.push('format');
return format.apply(this, args);
};
client.generic('count', 'a');
expect(order).to.deep.equal(['format', 'push']);
});
it('Should push whatever is returned from format method', () => {
client = new SDC();
client.format = () => 'some string';
client.generic('count', 'a');
expect(parameters.push).to.deep.equal(['some string']);
});
it('Should flush metrics before process exit', () => {
const stats = new SDC();
let flushed = false;
stats.send = () => { flushed = true; };
expect(flushed).to.be.false;
process.emit('exit', 0);
expect(flushed).to.be.true;
});
});