-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
connections_stepdown.test.js
168 lines (148 loc) · 5.14 KB
/
connections_stepdown.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
'use strict';
const chai = require('chai');
const expect = chai.expect;
function ignoreNsNotFound(err) {
if (!err.message.match(/ns not found/)) {
throw err;
}
}
function connectionCount(client) {
return client
.db()
.admin()
.serverStatus()
.then(result => result.connections.totalCreated);
}
function expectPoolWasCleared(initialCount) {
return count => expect(count).to.greaterThan(initialCount);
}
function expectPoolWasNotCleared(initialCount) {
return count => expect(count).to.equal(initialCount);
}
// TODO: NODE-3819: Unskip flaky MacOS tests.
const maybeDescribe = process.platform === 'darwin' ? describe.skip : describe;
maybeDescribe('Connections survive primary step down', function () {
let client;
let checkClient;
let db;
let collection;
beforeEach(function () {
const clientOptions = {
maxPoolSize: 1,
retryWrites: false,
heartbeatFrequencyMS: 100
};
client = this.configuration.newClient(clientOptions);
return client
.connect()
.then(() => {
const primary = Array.from(client.topology.description.servers.values()).filter(
sd => sd.type === 'RSPrimary'
)[0];
checkClient = this.configuration.newClient(
`mongodb://${primary.address}/?directConnection=true`,
clientOptions
);
return checkClient.connect();
})
.then(() => {
db = client.db('step-down');
collection = db.collection('step-down');
})
.then(() => collection.drop({ writeConcern: { w: 'majority' } }))
.catch(ignoreNsNotFound)
.then(() => db.createCollection('step-down', { writeConcern: { w: 'majority' } }));
});
let deferred = [];
afterEach(function () {
return Promise.all(deferred.map(d => d())).then(() => {
deferred = [];
return Promise.all([client.close(), checkClient.close()]);
});
});
it('getMore iteration', {
metadata: { requires: { mongodb: '>=4.2.0', topology: 'replicaset' } },
test: function () {
return collection
.insertMany([{ a: 1 }, { a: 2 }, { a: 3 }, { a: 4 }, { a: 5 }], {
writeConcern: { w: 'majority' }
})
.then(result => expect(result.insertedCount).to.equal(5))
.then(() => {
const cursor = collection.find({}, { batchSize: 2 });
deferred.push(() => cursor.close());
return cursor
.next()
.then(item => expect(item.a).to.equal(1))
.then(() => cursor.next())
.then(item => expect(item.a).to.equal(2))
.then(() => {
return connectionCount(checkClient).then(initialConnectionCount => {
return client
.db('admin')
.command({ replSetFreeze: 0 }, { readPreference: 'secondary' })
.then(result => expect(result).property('info').to.equal('unfreezing'))
.then(() =>
client
.db('admin')
.command({ replSetStepDown: 30, force: true }, { readPreference: 'primary' })
)
.then(() => cursor.next())
.then(item => expect(item.a).to.equal(3))
.then(() =>
connectionCount(checkClient).then(
expectPoolWasNotCleared(initialConnectionCount)
)
);
});
});
});
}
});
function runStepownScenario(errorCode, predicate) {
return connectionCount(checkClient).then(initialConnectionCount => {
return client
.db('admin')
.command({
configureFailPoint: 'failCommand',
mode: { times: 1 },
data: { failCommands: ['insert'], errorCode }
})
.then(() => {
deferred.push(() =>
client.db('admin').command({ configureFailPoint: 'failCommand', mode: 'off' })
);
return collection.insertOne({ test: 1 }).then(
() => Promise.reject(new Error('expected an error')),
err => expect(err.code).to.equal(errorCode)
);
})
.then(() => collection.insertOne({ test: 1 }))
.then(() => connectionCount(checkClient).then(predicate(initialConnectionCount)));
});
}
it('Not Primary - Keep Connection Pool', {
metadata: { requires: { mongodb: '>=4.2.0', topology: 'replicaset' } },
test: function () {
return runStepownScenario(10107, expectPoolWasNotCleared);
}
});
it('Not Primary - Reset Connection Pool', {
metadata: { requires: { mongodb: '4.0.x', topology: 'replicaset' } },
test: function () {
return runStepownScenario(10107, expectPoolWasCleared);
}
});
it('Shutdown in progress - Reset Connection Pool', {
metadata: { requires: { mongodb: '>=4.0.0', topology: 'replicaset' } },
test: function () {
return runStepownScenario(91, expectPoolWasCleared);
}
});
it('Interrupted at shutdown - Reset Connection Pool', {
metadata: { requires: { mongodb: '>=4.0.0', topology: 'replicaset' } },
test: function () {
return runStepownScenario(11600, expectPoolWasCleared);
}
});
});