-
Notifications
You must be signed in to change notification settings - Fork 3
/
spec.js
145 lines (133 loc) · 5.02 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
const { resolve } = require('path');
const wait = require('@lets/wait');
const results = [];
describe('Integration', () => {
let SDC;
before(() => {
teardown();
require('../lib/sender');
require.cache[require.resolve('../lib/sender')].exports = () => (...args) => results.push(...args);
SDC = require('..');
});
beforeEach(() => {
results.length = 0;
});
after(teardown);
it('Should sanitise key', () => {
const client = new SDC({ MTU: 0 });
client.count('Hello1-there$.');
expect(results[0]).to.contain('hello1_there_.');
});
it('Should only fire when the bulk gets full', () => {
const client = new SDC({ MTU: 100 });
client.count(new Array(46).join('a'));
expect(results).to.have.lengthOf(0);
client.count(new Array(46).join('a'));
expect(results).to.have.lengthOf(0);
client.count(new Array(2).join('a'));
expect(results).to.have.lengthOf(1);
});
it('Should allow for no MTU by setting it to 0 (or anything lower that 5)', () => {
const client = new SDC({ MTU: 0 });
expect(results).to.have.lengthOf(0);
client.count(new Array(2).join('a'));
expect(results).to.have.lengthOf(1);
});
it('Should fire after ttl has expired', async() => {
const client = new SDC({ timeout: 50 });
client.count(new Array(10).join('a'));
expect(results, 'Expected not to fire immediately').to.have.lengthOf(0);
await wait(21);
expect(results, 'Expected not to fire before timeout has expired').to.have.lengthOf(0);
await wait(32);
expect(results, 'Expected to fire once timeout has expired').to.have.lengthOf(1);
});
it('Should maintain ttl after adding to bulk', async() => {
const client = new SDC({ timeout: 50 });
client.count(new Array(10).join('a'));
expect(results, 'Expected not to fire immediately').to.have.lengthOf(0);
await wait(21);
client.count(new Array(10).join('a'));
expect(results, 'Expected not to fire before timeout has expired').to.have.lengthOf(0);
await wait(30);
expect(results, 'Expected to fire once timeout has expired').to.have.lengthOf(1);
});
it('Should flush immaterially explicitly', async() => {
const client = new SDC({ timeout: 50 });
client.count('a');
client.flush();
expect(results, 'Expected to fire immaterially').to.have.lengthOf(1);
});
});
describe('Integration: bulk sending', () => {
const { Socket: { prototype: UDPsocket } } = require('dgram');
const { Socket: { prototype: TCPsocket } } = require('net');
const { send } = UDPsocket;
const { write } = TCPsocket;
function mock(fn) {
UDPsocket.send = function(...args) {
fn(...args);
};
TCPsocket.write = function(...args) {
if (`${args[0]}`.startsWith(' ')) {
write.apply(this, args); // required for test suite to communicate
} else {
fn(...args);
}
};
}
before(teardown);
beforeEach(() => {
mock(() => null);
});
afterEach(() => {
UDPsocket.send = send;
TCPsocket.write = write;
});
after(teardown);
[
{ protocol: 'UDP', port: 2003 },
process.env.CI ? null : { protocol: 'TCP', port: 8081 }
].filter(Boolean).forEach(
({ protocol, port }) => {
process.stdout.write(JSON.stringify({ protocol, port }));
it(`Should flush metrics to ${protocol} socket in bulk`, async() => {
let metrics;
mock((buffer) => {
metrics = buffer;
});
const SDC = require('..');
const client = new SDC({ protocol, port });
new Array(4).fill('a').forEach(client.count);
expect(metrics).to.be.undefined;
await wait(5);
client.flush();
expect(metrics).to.be.instanceof(Buffer);
expect(metrics.toString()).to.have.entriesCount('\n', 3);
});
it(`Should send metrics to ${protocol} socket in bulk`, async() => {
let metrics;
mock((buffer) => {
metrics = buffer;
});
const SDC = require('..');
const client = new SDC({ protocol, port, timeout: 1 });
new Array(4).fill('a').forEach(client.count);
expect(metrics).to.be.undefined;
await wait(5);
expect(metrics).to.be.instanceof(Buffer);
expect(metrics.toString()).to.have.entriesCount('\n', 3);
});
}
);
});
function teardown() {
delete require.cache[require.resolve('..')];
const lib = resolve(__dirname, '..', 'lib');
Object.keys(require.cache)
.filter((key) => key.startsWith(lib))
.forEach((key) => {
delete require.cache[key];
})
;
}