This repository has been archived by the owner on Feb 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 108
/
read_preference_tests.js
96 lines (83 loc) · 3.16 KB
/
read_preference_tests.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
'use strict';
const expect = require('chai').expect;
const ReplSet = require('../../../../lib/topologies/replset');
const ReadPreference = require('../../../../lib/topologies/read_preference');
const mock = require('mongodb-mock-server');
const ReplSetFixture = require('../common').ReplSetFixture;
const ReplSetState = require('../../../../lib/topologies/replset_state');
const MongoError = require('../../../..').MongoError;
describe('ReadPreference (ReplSet)', function() {
let test;
before(() => (test = new ReplSetFixture()));
afterEach(() => mock.cleanup());
beforeEach(() => test.setup());
it('Should not be "connected" with ReadPreference secondary unless secondary is connected', {
metadata: {
requires: {
topology: 'single'
}
},
test: function(done) {
const replSet = new ReplSet(
[test.primaryServer.address(), test.firstSecondaryServer.address()],
{
setName: 'rs',
connectionTimeout: 3000,
socketTimeout: 0,
haInterval: 100,
size: 1
}
);
replSet.on('error', done);
replSet.on('connect', server => {
let err;
try {
expect(server.s.replicaSetState.hasSecondary()).to.equal(true);
} catch (e) {
err = e;
}
replSet.destroy();
done(err);
});
replSet.connect({ readPreference: new ReadPreference('secondary') });
}
});
it('should correctly sort servers by `lastIsMasterMS` during nearest selection', function() {
const state = new ReplSetState();
const sampleData = [
{ type: 'RSPrimary', lastIsMasterMS: 5 },
{ type: 'RSSecondary', lastIsMasterMS: 4 },
{ type: 'RSSecondary', lastIsMasterMS: 4 },
{ type: 'RSSecondary', lastIsMasterMS: 109 },
{ type: 'RSSecondary', lastIsMasterMS: 110 },
{ type: 'RSSecondary', lastIsMasterMS: 110 },
{ type: 'RSSecondary', lastIsMasterMS: 245 },
{ type: 'RSSecondary', lastIsMasterMS: 221 },
{ type: 'RSSecondary', lastIsMasterMS: 199 },
{ type: 'RSSecondary', lastIsMasterMS: 129 },
{ type: 'RSSecondary', lastIsMasterMS: 131 },
{ type: 'RSSecondary', lastIsMasterMS: 284 },
{ type: 'RSSecondary', lastIsMasterMS: 298 },
{ type: 'RSSecondary', lastIsMasterMS: 289 },
{ type: 'RSSecondary', lastIsMasterMS: 312 }
];
// load mock data into replset state
sampleData.forEach(desc => {
desc.ismaster = { maxWireVersion: 6 }; // for maxStalenessSeconds test
desc.staleness = Math.floor(Math.random() * 100);
if (desc.type === 'RSPrimary') {
state.primary = desc;
} else {
state.secondaries.push(desc);
}
});
// select the nearest server without max staleness seconds
let server = state.pickServer(ReadPreference.nearest);
expect(server).to.not.be.an.instanceOf(MongoError);
expect(server.lastIsMasterMS).to.equal(4);
// select the nearest server with max staleness seconds
server = state.pickServer(new ReadPreference('nearest', { maxStalenessSeconds: 100 }));
expect(server).to.not.be.an.instanceOf(MongoError);
expect(server.lastIsMasterMS).to.equal(4);
});
});