forked from Automattic/mongoose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.reconnect.test.js
61 lines (50 loc) · 1.42 KB
/
connection.reconnect.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
var start = require('./common');
var mongoose = start.mongoose;
var assert = require('assert');
describe('connection: manual reconnect with authReconnect: false', function(){
it('should continue processing queries/writes', function(done){
// connect to mongod
// perform writes/queries
// take mongod down
// bring mongod up
// writes/queries should work
// - should not get 'no open connections' error
var db = mongoose.createConnection();
db.open(start.uri, { server: { auto_reconnect: false }});
var M = db.model('autoReconnect', { name: String });
var open = false;
var times = 0;
db.on('open', function () {
++times;
open = true;
hit();
})
db.on('disconnected', function () {
open = false;
setTimeout(function () {
db.open(start.uri, { server: { auto_reconnect: false }});
}, 30);
})
function hit () {
if (!open) return;
M.create({ name: times }, function (err, doc) {
if (err) return complete(err);
M.findOne({ _id: doc._id }, function (err, found) {
if (err) return complete(err);
if (times > 1) {
return complete();
}
shutdownMongo();
})
})
}
function shutdownMongo () {
db.db.close();
}
function complete (err) {
if (complete.ran) return ;
complete.ran = 1;
done(err);
}
})
})