test.js
1.9 KB
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
var ReplSet = require('./lib2/topologies/replset'),
ReadPreference = require('./lib2/topologies/read_preference');
// Attempt to connect
var server = new ReplSet([{
host: 'localhost', port: 31000
}, {
host: 'localhost', port: 31001
}], {
setName: 'rs'
});
function executeCursors(_server, cb) {
var count = 100;
for(var i = 0; i < 100; i++) {
// Execute the write
var cursor = _server.cursor('test.test', {
find: 'test.test'
, query: {a:1}
}, {readPreference: new ReadPreference('secondary')});
// Get the first document
cursor.next(function(err, doc) {
count = count - 1;
if(err) console.dir(err)
if(count == 0) return cb();
});
}
}
server.on('connect', function(_server) {
console.log("---------------------------------- 0")
// Attempt authentication
_server.auth('scram-sha-1', 'admin', 'root', 'root', function(err, r) {
console.log("---------------------------------- 1")
// console.dir(err)
// console.dir(r)
_server.insert('test.test', [{a:1}], function(err, r) {
console.log("---------------------------------- 2")
console.dir(err)
if(r)console.dir(r.result)
var name = null;
_server.on('joined', function(_t, _server) {
if(name == _server.name) {
console.log("=========== joined :: " + _t + " :: " + _server.name)
executeCursors(_server, function() {
});
}
})
// var s = _server.s.replicaSetState.secondaries[0];
// s.destroy({emitClose:true});
executeCursors(_server, function() {
console.log("============== 0")
// Attempt to force a server reconnect
var s = _server.s.replicaSetState.secondaries[0];
name = s.name;
s.destroy({emitClose:true});
// console.log("============== 1")
// _server.destroy();
// test.done();
});
});
});
});
server.connect();