-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
reconnect_tests.js
123 lines (105 loc) · 3.33 KB
/
reconnect_tests.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
'use strict';
const Mongos = require('../../../../lib/core/topologies/mongos');
const expect = require('chai').expect;
const mock = require('mongodb-mock-server');
const genClusterTime = require('../common').genClusterTime;
const Connection = require('../../../../lib/core/connection/connection');
const ConnectionSpy = require('../../functional/shared').ConnectionSpy;
describe.skip('Reconnect (Mongos)', function() {
const fixture = {};
function startServer() {
fixture.spy = new ConnectionSpy();
Connection.enableConnectionAccounting(fixture.spy);
return mock.createServer(fixture.port).then(mockServer => {
mockServer.setMessageHandler(request => {
request.reply(
Object.assign({}, mock.DEFAULT_ISMASTER, {
$clusterTime: genClusterTime(Date.now()),
msg: 'isdbgrid'
})
);
});
fixture.server = mockServer;
fixture.port = mockServer.port;
});
}
function stopServer() {
Connection.disableConnectionAccounting();
return mock.cleanup();
}
beforeEach(() => startServer());
afterEach(() => stopServer());
// NOTE: skipped due to flakiness and extremely long test runs
it.skip('should not connection swarm when reconnecting', function(done) {
const reconnectInterval = 500;
const socketTimeout = reconnectInterval * 5;
const haInterval = reconnectInterval * 10;
const reconnectTries = Number.MAX_VALUE;
const connectOptions = {
haInterval,
reconnectInterval,
socketTimeout,
reconnectTries,
reconnect: true,
poolSize: 500
};
const mongos = new Mongos([fixture.server.address()], connectOptions);
function runIsMaster(assertion) {
return new Promise((resolve, reject) => {
mongos.command('admin.$cmd', { ismaster: 1 }, {}, (err, response) => {
try {
assertion(err, response);
resolve();
} catch (e) {
reject(e);
}
});
});
}
function connectMongos() {
return new Promise((resolve, reject) => {
mongos.once('error', reject);
mongos.once('connect', resolve);
mongos.connect(connectOptions);
});
}
function assertSuccess(err, response) {
expect(err).to.not.exist;
expect(response).to.exist;
}
function assertError(err, response) {
expect(err).to.exist;
expect(response).to.not.exist;
}
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function cleanup(err) {
mongos.destroy();
return err;
}
Promise.resolve()
.then(() => connectMongos())
.then(() => runIsMaster(assertSuccess))
.then(() => stopServer())
.then(() => runIsMaster(assertError))
.then(() => delay(haInterval * 2))
.then(() => startServer())
.then(() => {
return new Promise((resolve, reject) => {
const timeoutTimer = setTimeout(
() => reject(new Error('timed out waiting for connection count')),
5000
);
fixture.spy.on('connectionRemoved', () => {
if (fixture.spy.connectionCount() === 1) {
clearTimeout(timeoutTimer);
resolve();
}
});
});
})
.then(() => cleanup(), cleanup)
.then(done);
});
});