-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest.js
More file actions
157 lines (119 loc) · 4.83 KB
/
test.js
File metadata and controls
157 lines (119 loc) · 4.83 KB
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
process.env.NODE_ENV = 'test';
const { EventEmitter } = require('events');
const { expect } = require('chai');
const sinon = require('sinon');
const promCollector = require('rewire')('./');
describe('socket.io metrics: internal functions', () => {
// retrieve unexported functions
const byteLen = promCollector.__get__('byteLen');
const beforeHook = promCollector.__get__('beforeHook');
it('byteLen works with UTF8 string', () => {
expect(byteLen('Шамиль')).to.eq(12);
});
it('byteLen works with non-UTF8 string', () => {
expect(byteLen('Shamil')).to.eq(6);
});
it('byteLen works with object', () => {
expect(byteLen({ message: 'Hello World!' })).to.eq(26);
});
it('byteLen works with Buffer', () => {
expect(byteLen(Buffer.from('hello world'))).to.eq(11);
});
it('byteLen returns 0 on exception or undefined', () => {
const obj = {};
obj.a = { b: obj };
expect(byteLen(obj)).to.eq(0);
expect(byteLen(undefined)).to.eq(0);
});
it('beforeHook returns false if first arg is undefined', () => {
expect(beforeHook()).to.eq(false);
});
it('beforeHook must not change returned value', () => {
const obj = { func: (a) => a };
beforeHook(obj, 'func', () => {
// Hook that doesn't use the parameter
});
expect(obj.func(10)).to.not.eq(0);
});
it('beforeHook must catch hook exceptions', () => {
const obj = { func: (a) => a };
beforeHook(obj, 'func', () => {
throw new Error('something went wrong');
});
expect(obj.func(10)).to.eq(10);
});
});
describe('socket.io metrics: collector', () => {
const socket = new EventEmitter();
const io = new EventEmitter();
promCollector(io);
// retrieve unexported metrix variable
const metrics = promCollector.__get__('metrics');
const { connectedSockets } = metrics;
const { connectTotal } = metrics;
const { disconnectTotal } = metrics;
const { eventsReceivedTotal } = metrics;
const { eventsSentTotal } = metrics;
const { bytesReceived } = metrics;
const { bytesTransmitted } = metrics;
const sandbox = sinon.createSandbox();
afterEach(() => {
sandbox.restore();
});
it('on connect - connectTotal and connectedSockets should increment', () => {
sandbox.spy(connectTotal, 'inc');
sandbox.spy(connectedSockets, 'inc');
io.emit('connect', socket);
expect(connectTotal.inc.callCount).to.eq(1);
expect(connectedSockets.inc.callCount).to.eq(1);
});
it('on socket disconnect - disconnectTotal should increment and connectedSockets should decrement', () => {
sandbox.spy(connectedSockets, 'dec');
sandbox.spy(disconnectTotal, 'inc');
socket.emit('disconnect');
expect(connectedSockets.dec.callCount).to.eq(1);
expect(disconnectTotal.inc.callCount).to.eq(1);
});
it('on socket emit - bytesTransmitted and eventsSentTotal should increment', () => {
const bytesTransmittedInc = sandbox.spy();
const eventsSentTotalInc = sandbox.spy();
sandbox.stub(bytesTransmitted, 'labels').returns({ inc: bytesTransmittedInc });
sandbox.stub(eventsSentTotal, 'labels').returns({ inc: eventsSentTotalInc });
socket.emit('test event');
expect(bytesTransmittedInc.callCount).to.eq(1);
expect(eventsSentTotalInc.callCount).to.eq(1);
});
it('on socket emit newListener - bytesTransmitted and eventsSentTotal should not increment', () => {
const bytesTransmittedInc = sandbox.spy();
const eventsSentTotalInc = sandbox.spy();
sandbox.stub(bytesTransmitted, 'labels').returns({ inc: bytesTransmittedInc });
sandbox.stub(eventsSentTotal, 'labels').returns({ inc: eventsSentTotalInc });
socket.emit('newListener');
expect(bytesTransmittedInc.callCount).to.eq(0);
expect(eventsSentTotalInc.callCount).to.eq(0);
});
it('on socket event - bytesReceived and eventsReceivedTotal should increment', (done) => {
const bytesReceivedInc = sandbox.spy();
const eventsReceivedTotalInc = sandbox.spy();
sandbox.stub(bytesReceived, 'labels').returns({ inc: bytesReceivedInc });
sandbox.stub(eventsReceivedTotal, 'labels').returns({ inc: eventsReceivedTotalInc });
socket.on('event', () => {
expect(bytesReceivedInc.callCount).to.eq(1);
expect(eventsReceivedTotalInc.callCount).to.eq(1);
done();
});
socket.emit('event');
});
it('on socket disconnect event - bytesReceived and eventsReceivedTotal should not increment', (done) => {
const bytesReceivedInc = sandbox.spy();
const eventsReceivedTotalInc = sandbox.spy();
sandbox.stub(bytesReceived, 'labels').returns({ inc: bytesReceivedInc });
sandbox.stub(eventsReceivedTotal, 'labels').returns({ inc: eventsReceivedTotalInc });
socket.on('disconnect', () => {
expect(bytesReceivedInc.callCount).to.eq(0);
expect(eventsReceivedTotalInc.callCount).to.eq(0);
done();
});
socket.emit('disconnect');
});
});