-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest.js
153 lines (116 loc) · 4.8 KB
/
test.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
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 strToBytes = promCollector.__get__('strToBytes');
const beforeHook = promCollector.__get__('beforeHook');
it('strToBytes works with UTF8 string', () => {
expect(strToBytes('Шамиль')).to.eq(12);
});
it('strToBytes works with non-UTF8 string', () => {
expect(strToBytes('Shamil')).to.eq(6);
});
it('strToBytes works with object', () => {
expect(strToBytes({ mesage: 'Hello World!' })).to.eq(25);
});
it('strToBytes returns 0 on exception or undefined', () => {
const obj = {};
obj.a = { b: obj };
expect(strToBytes(obj)).to.eq(0);
expect(strToBytes(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', (a) => {
a = 0;
});
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.connectedSockets;
const connectTotal = metrics.connectTotal;
const disconnectTotal = metrics.disconnectTotal;
const eventsReceivedTotal = metrics.eventsReceivedTotal;
const eventsSentTotal = metrics.eventsSentTotal;
const bytesReceived = metrics.bytesReceived;
const bytesTransmitted = metrics.bytesTransmitted;
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');
});
});