forked from christkv/node-mongodb-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiple_dbs_on_connection_pool_test.js
274 lines (241 loc) · 8.91 KB
/
multiple_dbs_on_connection_pool_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
var mongodb = process.env['TEST_NATIVE'] != null ? require('../lib/mongodb').native() : require('../lib/mongodb').pure();
var useSSL = process.env['USE_SSL'] != null ? true : false;
var testCase = require('nodeunit').testCase,
debug = require('util').debug,
inspect = require('util').inspect,
nodeunit = require('nodeunit'),
gleak = require('../dev/tools/gleak'),
Db = mongodb.Db,
Cursor = mongodb.Cursor,
Collection = mongodb.Collection,
ServerManager = require('../test/tools/server_manager').ServerManager,
Server = mongodb.Server;
var MONGODB = 'integration_tests';
var native_parser = (process.env['TEST_NATIVE'] != null);
var client = null;
/**
* Retrieve the server information for the current
* instance of the db client
*
* @ignore
*/
exports.setUp = function(callback) {
var self = exports;
client = new Db(MONGODB, new Server("127.0.0.1", 27017, {auto_reconnect: true, poolSize: 4, ssl:useSSL}), {native_parser: (process.env['TEST_NATIVE'] != null)});
client.open(function(err, db_p) {
if(numberOfTestsRun == (Object.keys(self).length)) {
// If first test drop the db
client.dropDatabase(function(err, done) {
callback();
});
} else {
return callback();
}
});
}
/**
* Retrieve the server information for the current
* instance of the db client
*
* @ignore
*/
exports.tearDown = function(callback) {
var self = this;
numberOfTestsRun = numberOfTestsRun - 1;
// Close connection
client.close();
callback();
}
/**
* @ignore
*/
exports.shouldCorrectlyEmitErrorOnAllDbsOnPoolClose = function(test) {
if(process.platform !== 'linux') {
var db = new Db('tests', new Server("127.0.0.1", 27027, {auto_reconnect: true}), {native_parser: (process.env['TEST_NATIVE'] != null)});
// All inserted docs
var docs = [];
var errs = [];
var insertDocs = [];
var numberOfCloses = 0;
// Start server
var serverManager = new ServerManager({auth:false, purgedirectories:true, journal:false, start_port:27027})
serverManager.start(false, function() {
db.on("close", function(err) {
numberOfCloses = numberOfCloses + 1;
})
db.open(function(err, db) {
db.createCollection('shouldCorrectlyErrorOnAllDbs', function(err, collection) {
test.equal(null, err);
collection.insert({a:1}, {safe:true}, function(err, result) {
test.equal(null, err);
// Open a second db
var db2 = db.db('tests_2');
// Add a close handler
db2.on("close", function(err) {
numberOfCloses = numberOfCloses + 1;
});
db.serverConfig.connectionPool.openConnections[0].connection.destroy();
// Kill server and end test
serverManager.stop(9, function() {
test.equal(2, numberOfCloses)
test.done();
});
});
});
});
});
} else {
test.done();
}
}
/**
* Test the auto connect functionality of the db
*
* @ignore
*/
exports.shouldCorrectlyUseSameConnectionsForTwoDifferentDbs = function(test) {
var second_test_database = new Db(MONGODB + "_2", new Server("127.0.0.1", 27017, {auto_reconnect: true, ssl:useSSL}), {native_parser: (process.env['TEST_NATIVE'] != null), retryMiliSeconds:50});
// Just create second database
second_test_database.open(function(err, second_test_database) {
// Close second database
second_test_database.close();
// Let's grab a connection to the different db resusing our connection pools
var secondDb = client.db(MONGODB + "_2");
secondDb.createCollection('shouldCorrectlyUseSameConnectionsForTwoDifferentDbs', function(err, collection) {
// Insert a dummy document
collection.insert({a:20}, {safe: true}, function(err, r) {
test.equal(null, err);
// Query it
collection.findOne({}, function(err, item) {
test.equal(20, item.a);
// Use the other db
client.createCollection('shouldCorrectlyUseSameConnectionsForTwoDifferentDbs', function(err, collection) {
// Insert a dummy document
collection.insert({b:20}, {safe: true}, function(err, r) {
test.equal(null, err);
// Query it
collection.findOne({}, function(err, item) {
test.equal(20, item.b);
// Drop the second db
secondDb.dropDatabase(function(err, item) {
test.equal(null, err);
test.done();
})
})
});
});
})
});
});
});
}
/**
* Test the auto connect functionality of the db
*
* @ignore
*/
exports.shouldCorrectlyUseSameConnectionsForTwoDifferentDbs = function(test) {
var second_test_database = new Db(MONGODB + "_2", new Server("127.0.0.1", 27017, {auto_reconnect: true, ssl:useSSL}), {native_parser: (process.env['TEST_NATIVE'] != null), retryMiliSeconds:50});
// Just create second database
second_test_database.open(function(err, second_test_database) {
// Close second database
second_test_database.close();
// Let's grab a connection to the different db resusing our connection pools
var secondDb = client.db(MONGODB + "_2");
secondDb.createCollection('shouldCorrectlyUseSameConnectionsForTwoDifferentDbs', function(err, collection) {
// Insert a dummy document
collection.insert({a:20}, {safe: true}, function(err, r) {
test.equal(null, err);
// Query it
collection.findOne({}, function(err, item) {
test.equal(20, item.a);
// Use the other db
client.createCollection('shouldCorrectlyUseSameConnectionsForTwoDifferentDbs', function(err, collection) {
// Insert a dummy document
collection.insert({b:20}, {safe: true}, function(err, r) {
test.equal(null, err);
// Query it
collection.findOne({}, function(err, item) {
test.equal(20, item.b);
// Drop the second db
secondDb.dropDatabase(function(err, item) {
test.equal(null, err);
test.done();
})
})
});
});
})
});
});
});
}
/**
* Simple example connecting to two different databases sharing the socket connections below.
*
* @_class db
* @_function db
*/
exports.shouldCorrectlyShareConnectionPoolsAcrossMultipleDbInstances = function(test) {
var db = new Db('integration_tests', new Server("127.0.0.1", 27017,
{auto_reconnect: false, poolSize: 4, ssl:useSSL}), {native_parser: native_parser});
// Establish connection to db
db.open(function(err, db) {
test.equal(null, err);
// Reference a different database sharing the same connections
// for the data transfer
var secondDb = db.db("integration_tests_2");
// Fetch the collections
var multipleColl1 = db.collection("multiple_db_instances");
var multipleColl2 = secondDb.collection("multiple_db_instances");
// Write a record into each and then count the records stored
multipleColl1.insert({a:1}, {safe:true}, function(err, result) {
multipleColl2.insert({a:1}, {safe:true}, function(err, result) {
// Count over the results ensuring only on record in each collection
multipleColl1.count(function(err, count) {
test.equal(1, count);
multipleColl2.count(function(err, count) {
test.equal(1, count);
db.close();
test.done();
});
});
});
});
});
}
/**
* @ignore
*/
exports.shouldCorrectlyHandleMultipleDbsFindAndModifies = function(test) {
var mongo_server = new Server("127.0.0.1", 27017, {auto_reconnect: true, poolSize: 1, socketOptions: {noDelay:false}});
new Db('init_db', mongo_server, {}).open(function (error, db) {
var db_instance = db.db('site1');
db_instance = db.db('site2');
db_instance = db.db('rss');
db_instance.collection('counters', function(err, collection) {
collection.findAndModify({}, {}, {'$inc': {'db': 1}}, {new:true}, function(error, counters){
db.close();
test.done();
});
});
});
},
/**
* 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;