/
query_secondaries_test.js
347 lines (300 loc) · 10.9 KB
/
query_secondaries_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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
var mongodb = process.env['TEST_NATIVE'] != null ? require('../../lib/mongodb').native() : require('../../lib/mongodb').pure();
var noReplicasetStart = process.env['NO_REPLICASET_START'] != null ? true : false;
var testCase = require('nodeunit').testCase,
debug = require('util').debug,
inspect = require('util').inspect,
gleak = require('../../dev/tools/gleak'),
ReplicaSetManager = require('../tools/replica_set_manager').ReplicaSetManager,
Db = mongodb.Db,
ReplSetServers = mongodb.ReplSetServers,
Server = mongodb.Server,
Step = require("step");
// Keep instance of ReplicaSetManager
var serversUp = false;
var retries = 120;
var RS = RS == null ? null : RS;
var ensureConnection = function(test, numberOfTries, callback) {
// Replica configuration
var replSet = new ReplSetServers( [
new Server( RS.host, RS.ports[1], { auto_reconnect: true } ),
new Server( RS.host, RS.ports[0], { auto_reconnect: true } ),
new Server( RS.host, RS.ports[2], { auto_reconnect: true } )
],
{rs_name:RS.name}
);
if(numberOfTries <= 0) return callback(new Error("could not connect correctly"), null);
var db = new Db('integration_test_', replSet);
// Print any errors
db.on("error", function(err) {
console.log("============================= ensureConnection caught error")
console.dir(err)
if(err != null && err.stack != null) console.log(err.stack)
db.close();
})
// Open the db
db.open(function(err, p_db) {
db.close();
if(err != null) {
// Wait for a sec and retry
setTimeout(function() {
numberOfTries = numberOfTries - 1;
ensureConnection(test, numberOfTries, callback);
}, 1000);
} else {
return callback(null, p_db);
}
})
}
/**
* Retrieve the server information for the current
* instance of the db client
*
* @ignore
*/
exports.setUp = function(callback) {
// Create instance of replicaset manager but only for the first call
if(!serversUp && !noReplicasetStart) {
serversUp = true;
RS = new ReplicaSetManager({retries:120, secondary_count:2, passive_count:1, arbiter_count:1});
RS.startSet(true, function(err, result) {
if(err != null) throw err;
// Finish setup
callback();
});
} else {
RS.restartKilledNodes(function(err, result) {
if(err != null) throw err;
callback();
})
}
}
/**
* Retrieve the server information for the current
* instance of the db client
*
* @ignore
*/
exports.tearDown = function(callback) {
numberOfTestsRun = numberOfTestsRun - 1;
if(numberOfTestsRun == 0) {
// Finished kill all instances
RS.killAll(function() {
callback();
})
} else {
callback();
}
}
exports.shouldReadPrimary = function(test) {
// Replica configuration
var replSet = new ReplSetServers( [
new Server( RS.host, RS.ports[0], { auto_reconnect: true } ),
],
{rs_name:RS.name, read_secondary:true}
);
// Insert some data
var db = new Db('integration_test_', replSet);
db.open(function(err, p_db) {
var waitForSetup = function() {
if(Object.keys(p_db.serverConfig._state.secondaries).length == 0) {
setTimeout(waitForSetup, 1000);
} else {
if(err != null) debug("shouldReadPrimary :: " + inspect(err));
// Drop collection on replicaset
p_db.dropCollection('testsets', function(err, r) {
if(err != null) debug("shouldReadPrimary :: " + inspect(err));
test.equal(false, p_db.serverConfig.isReadPrimary());
test.equal(false, p_db.serverConfig.isPrimary());
p_db.close();
test.done();
});
}
}
setTimeout(waitForSetup, 1000);
})
}
exports.shouldCorrectlyTestConnection = function(test) {
// Replica configuration
var replSet = new ReplSetServers( [
new Server( RS.host, RS.ports[0], { auto_reconnect: true } ),
],
{rs_name:RS.name, read_secondary:true}
);
// Insert some data
var db = new Db('integration_test_', replSet);
db.open(function(err, p_db) {
var waitForSetup = function() {
if(Object.keys(p_db.serverConfig._state.secondaries).length == 0) {
setTimeout(waitForSetup, 1000);
} else {
if(err != null) debug("shouldReadPrimary :: " + inspect(err));
// Drop collection on replicaset
p_db.dropCollection('testsets', function(err, r) {
if(err != null) debug("shouldReadPrimary :: " + inspect(err));
test.ok(p_db.serverConfig.primary != null);
test.ok(p_db.serverConfig.read != null);
test.ok(p_db.serverConfig.primary.port != p_db.serverConfig.read.port);
p_db.close();
test.done();
});
}
}
setTimeout(waitForSetup, 1000);
})
}
exports.shouldCorrectlyQuerySecondaries = function(test) {
var self = this;
// Replica configuration
var replSet = new ReplSetServers( [
new Server( RS.host, RS.ports[0], { auto_reconnect: true } ),
new Server( RS.host, RS.ports[1], { auto_reconnect: true } ),
new Server( RS.host, RS.ports[2], { auto_reconnect: true } ),
],
{rs_name:RS.name, read_secondary:true}
);
// Insert some data
var db = new Db('integration_test_', replSet);
db.open(function(err, p_db) {
// console.log("----------------------------------------------------------------- 0")
p_db.createCollection("testsets", {safe:{w:2, wtimeout:10000}}, function(err, collection) {
// console.log("----------------------------------------------------------------- 1")
collection.insert([{a:20}, {a:30}, {a:40}], {safe:{w:2, wtimeout:10000}}, function(err, result) {
// console.log("----------------------------------------------------------------- 2")
// Ensure replication happened in time
setTimeout(function() {
// Kill the primary
RS.killPrimary(2, function(node) {
// console.log("----------------------------------------------------------------- 3")
// Do a collection find
collection.find().toArray(function(err, items) {
// console.log("----------------------------------------------------------------- 4")
test.ok(err != null);
// console.log("============================================================================ shouldCorrectlyQuerySecondaries")
// console.dir(err)
// console.dir(items)
collection.find().toArray(function(err, items) {
// console.log("----------------------------------------------------------------- 5")
// console.log("============================================================================ shouldCorrectlyQuerySecondaries 1")
// console.dir(err)
// console.dir(items)
test.equal(null, err);
test.equal(3, items.length);
p_db.close();
test.done();
});
});
});
}, 2000);
})
});
})
}
exports.shouldCorrectlyQueryPrimary = function(test) {
// debug("=========================================== shouldCorrectlyQuerySecondaries")
var self = this;
// Replica configuration
var replSet = new ReplSetServers( [
new Server( RS.host, RS.ports[0], { auto_reconnect: true } ),
new Server( RS.host, RS.ports[1], { auto_reconnect: true } ),
new Server( RS.host, RS.ports[2], { auto_reconnect: true } ),
],
{rs_name:RS.name, read_secondary:false}
);
// Insert some data
var db = new Db('integration_test_', replSet);
db.open(function(err, p_db) {
if(err != null) debug("shouldReadPrimary :: " + inspect(err));
// Ensure the checkoutReader gives us the actual writer object
var reader = replSet.checkoutReader();
var writer = replSet.checkoutWriter();
// Ensure the connections are the same
test.equal(reader.socketOptions.host, writer.socketOptions.host);
test.equal(reader.socketOptions.port, writer.socketOptions.port);
// Close connection to Spain
db.close();
test.done();
});
}
exports.shouldAllowToForceReadWithPrimary = function(test) {
// debug("=========================================== shouldAllowToForceReadWithPrimary")
// Replica configuration
var replSet = new ReplSetServers( [
new Server( RS.host, RS.ports[0], { auto_reconnect: true } ),
],
{rs_name:RS.name, read_secondary:true}
);
// Insert some data
var db = new Db('integration_test_', replSet);
db.open(function(err, p_db) {
if(err != null) debug("shouldReadPrimary :: " + inspect(err));
// Create a collection
p_db.createCollection('shouldAllowToForceReadWithPrimary', function(err, collection) {
test.equal(null, err);
// Insert a document
collection.insert({a:1}, {safe:{w:2, wtimeout:10000}}, function(err, result) {
test.equal(null, err);
// Force read using primary
var cursor = collection.find({}, {read:'primary'})
// Get documents
cursor.toArray(function(err, items) {
test.equal(1, items.length);
test.equal(1, items[0].a);
p_db.close();
test.done();
})
});
})
})
}
exports.shouldWorkWithSecondarySeeding = function(test) {
// debug("=========================================== shouldAllowToForceReadWithPrimary")
// Replica configuration
var replSet = new ReplSetServers( [
new Server( RS.host, RS.ports[0], { auto_reconnect: true } ),
],
{rs_name:RS.name, read_secondary:true}
);
// Insert some data
var db = new Db('integration_test_', replSet);
db.on("fullsetup", function() {
// Create a collection
db.createCollection('shouldWorkWithSecondarySeeding', function(err, collection) {
test.equal(null, err);
// Insert a document
collection.insert({a:1}, {safe:{w:2, wtimeout:10000}}, function(err, result) {
test.equal(null, err);
// Force read using primary
var cursor = collection.find({}, {read:'primary'})
// Get documents
cursor.toArray(function(err, items) {
test.equal(1, items.length);
test.equal(1, items[0].a);
db.close();
test.done();
});
});
})
});
db.open(function(err, p_db) {
if(err != null) debug("shouldReadPrimary :: " + inspect(err));
db = p_db;
})
}
/**
* Retrieve the server information for the current
* instance of the db client
*
* @ignore
*/
exports.noGlobalsLeaked = function(test) {
var leaks = gleak.detectNew();
test.equal(0, leaks.length, "global var leak detected: " + leaks.join(', '));
test.done();
}
/**
* Retrieve the server information for the current
* instance of the db client
*
* @ignore
*/
var numberOfTestsRun = Object.keys(this).length - 2;