-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
monitor.test.js
257 lines (215 loc) · 8.17 KB
/
monitor.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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
'use strict';
const mock = require('../../tools/mongodb-mock/index');
const { ServerType } = require('../../../src/sdam/common');
const { Topology } = require('../../../src/sdam/topology');
const { Monitor } = require('../../../src/sdam/monitor');
const { expect } = require('chai');
const { ServerDescription } = require('../../../src/sdam/server_description');
const { LEGACY_HELLO_COMMAND } = require('../../../src/constants');
const { isHello } = require('../../../src/utils');
class MockServer {
constructor(options) {
this.s = { pool: { generation: 1 } };
this.description = new ServerDescription(`${options.host}:${options.port}`);
this.description.type = ServerType.Unknown;
}
}
describe('monitoring', function () {
let mockServer;
after(() => mock.cleanup());
beforeEach(function () {
return mock.createServer().then(server => (mockServer = server));
});
// TODO(NODE-3819): Unskip flaky tests.
it.skip('should record roundTripTime', function (done) {
mockServer.setMessageHandler(request => {
const doc = request.document;
if (isHello(doc)) {
request.reply(Object.assign({}, mock.HELLO));
} else if (doc.endSessions) {
request.reply({ ok: 1 });
}
});
// set `heartbeatFrequencyMS` to 250ms to force a quick monitoring check, and wait 500ms to validate below
const topology = new Topology(mockServer.hostAddress(), { heartbeatFrequencyMS: 250 });
topology.connect(err => {
expect(err).to.not.exist;
setTimeout(() => {
expect(topology).property('description').property('servers').to.have.length(1);
const serverDescription = Array.from(topology.description.servers.values())[0];
expect(serverDescription).property('roundTripTime').to.be.greaterThan(0);
topology.close(done);
}, 500);
});
}).skipReason = 'TODO(NODE-3819): Unskip flaky tests';
// TODO(NODE-3600): Unskip flaky test
it.skip('should recover on error during initial connect', function (done) {
// This test should take ~1s because initial server selection fails and an immediate check
// is requested. If the behavior of the immediate check is broken, the test will take ~10s
// to complete. We want to ensure validation of the immediate check behavior, and therefore
// hardcode the test timeout to 2s.
this.timeout(2000);
let acceptConnections = false;
mockServer.setMessageHandler(request => {
if (!acceptConnections) {
request.connection.destroy();
return;
}
const doc = request.document;
if (isHello(doc)) {
request.reply(Object.assign({}, mock.HELLO));
} else if (doc.endSessions) {
request.reply({ ok: 1 });
}
});
setTimeout(() => {
acceptConnections = true;
}, 250);
const topology = new Topology(mockServer.hostAddress(), {});
topology.connect(err => {
expect(err).to.not.exist;
expect(topology).property('description').property('servers').to.have.length(1);
const serverDescription = Array.from(topology.description.servers.values())[0];
expect(serverDescription).property('roundTripTime').to.be.greaterThan(0);
topology.close(done);
});
}).skipReason = 'TODO(NODE-3600): Unskip flaky tests';
describe('Monitor', function () {
let monitor;
beforeEach(() => {
monitor = null;
});
afterEach(() => {
if (monitor) {
monitor.close();
}
});
it('should connect and issue an initial server check', function (done) {
mockServer.setMessageHandler(request => {
const doc = request.document;
if (isHello(doc)) {
request.reply(Object.assign({}, mock.HELLO));
}
});
const server = new MockServer(mockServer.address());
monitor = new Monitor(server, {});
monitor.on('serverHeartbeatFailed', () => done(new Error('unexpected heartbeat failure')));
monitor.on('serverHeartbeatSucceeded', () => {
expect(monitor.connection.isMonitoringConnection).to.be.true;
done();
});
monitor.connect();
});
it('should ignore attempts to connect when not already closed', function (done) {
mockServer.setMessageHandler(request => {
const doc = request.document;
if (isHello(doc)) {
request.reply(Object.assign({}, mock.HELLO));
}
});
const server = new MockServer(mockServer.address());
monitor = new Monitor(server, {});
monitor.on('serverHeartbeatFailed', () => done(new Error('unexpected heartbeat failure')));
monitor.on('serverHeartbeatSucceeded', () => done());
monitor.connect();
monitor.connect();
});
it('should not initiate another check if one is in progress', function (done) {
mockServer.setMessageHandler(request => {
const doc = request.document;
if (isHello(doc)) {
setTimeout(() => request.reply(Object.assign({}, mock.HELLO)), 250);
}
});
const server = new MockServer(mockServer.address());
monitor = new Monitor(server, {});
const startedEvents = [];
monitor.on('serverHeartbeatStarted', event => startedEvents.push(event));
monitor.on('close', () => {
expect(startedEvents).to.have.length(2);
done();
});
monitor.connect();
monitor.once('serverHeartbeatSucceeded', () => {
monitor.requestCheck();
monitor.requestCheck();
monitor.requestCheck();
monitor.requestCheck();
monitor.requestCheck();
const minHeartbeatFrequencyMS = 500;
setTimeout(() => {
// wait for minHeartbeatFrequencyMS, then request a check and verify another check occurred
monitor.once('serverHeartbeatSucceeded', () => {
monitor.close();
});
monitor.requestCheck();
}, minHeartbeatFrequencyMS);
});
});
it('should not close the monitor on a failed heartbeat', function (done) {
let helloCount = 0;
mockServer.setMessageHandler(request => {
const doc = request.document;
if (isHello(doc)) {
helloCount++;
if (helloCount === 2) {
request.reply({ ok: 0, errmsg: 'forced from mock server' });
return;
}
if (helloCount === 3) {
request.connection.destroy();
return;
}
request.reply(mock.HELLO);
}
});
const server = new MockServer(mockServer.address());
server.description = new ServerDescription(server.description.hostAddress);
monitor = new Monitor(server, {
heartbeatFrequencyMS: 250,
minHeartbeatFrequencyMS: 50
});
const events = [];
monitor.on('serverHeartbeatFailed', event => events.push(event));
let successCount = 0;
monitor.on('serverHeartbeatSucceeded', () => {
if (successCount++ === 2) {
monitor.close();
}
});
monitor.on('close', () => {
expect(events).to.have.length(2);
done();
});
monitor.connect();
});
it('should upgrade to hello from legacy hello when initial handshake contains helloOk', function (done) {
const docs = [];
mockServer.setMessageHandler(request => {
const doc = request.document;
docs.push(doc);
if (docs.length === 2) {
expect(docs[0]).to.have.property(LEGACY_HELLO_COMMAND, true);
expect(docs[0]).to.have.property('helloOk', true);
expect(docs[1]).to.have.property('hello', true);
done();
} else if (isHello(doc)) {
setTimeout(() => request.reply(Object.assign({ helloOk: true }, mock.HELLO)), 250);
}
});
const server = new MockServer(mockServer.address());
monitor = new Monitor(server, {});
monitor.connect();
monitor.once('serverHeartbeatSucceeded', () => {
const minHeartbeatFrequencyMS = 500;
setTimeout(() => {
// wait for minHeartbeatFrequencyMS, then request a check and verify another check occurred
monitor.once('serverHeartbeatSucceeded', () => {
monitor.close();
});
monitor.requestCheck();
}, minHeartbeatFrequencyMS);
});
});
});
});