-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
max_staleness.test.js
182 lines (160 loc) · 5.29 KB
/
max_staleness.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
'use strict';
const { Long } = require('bson');
const { expect } = require('chai');
const mock = require('../tools/mock');
const { ReadPreference } = require('../../src');
const test = {};
describe('Max Staleness', function () {
afterEach(() => mock.cleanup());
beforeEach(() => {
return mock.createServer().then(server => {
test.server = server;
const defaultFields = Object.assign({}, mock.DEFAULT_ISMASTER, { msg: 'isdbgrid' });
// Primary server states
const serverIsMaster = [Object.assign({}, defaultFields)];
server.setMessageHandler(request => {
var doc = request.document;
if (doc.ismaster || doc.hello) {
request.reply(serverIsMaster[0]);
return;
}
if (doc['$query'] && doc['$readPreference']) {
test.checkCommand = doc;
request.reply({
waitedMS: Long.ZERO,
cursor: {
id: Long.ZERO,
ns: 'test.t',
firstBatch: []
},
ok: 1
});
} else if (doc.endSessions) {
request.reply({ ok: 1 });
}
});
});
});
it('should correctly set maxStalenessSeconds on Mongos query on connect', {
metadata: {
requires: {
generators: true,
topology: 'single'
}
},
test: function (done) {
var self = this;
const configuration = this.configuration;
const client = configuration.newClient(
`mongodb://${test.server.uri()}/test?readPreference=secondary&maxStalenessSeconds=250`
);
client.connect(function (err, client) {
expect(err).to.not.exist;
var db = client.db(self.configuration.db);
db.collection('test')
.find({})
.toArray(function (err) {
expect(err).to.not.exist;
expect(test.checkCommand).to.containSubset({
$query: { find: 'test', filter: {} },
$readPreference: { mode: 'secondary', maxStalenessSeconds: 250 }
});
client.close(done);
});
});
}
});
it('should correctly set maxStalenessSeconds on Mongos query using db level readPreference', {
metadata: {
requires: {
generators: true,
topology: 'single'
}
},
test: function (done) {
const configuration = this.configuration;
const client = configuration.newClient(`mongodb://${test.server.uri()}/test`);
client.connect(function (err, client) {
expect(err).to.not.exist;
// Get a db with a new readPreference
var db1 = client.db('test', {
readPreference: new ReadPreference('secondary', null, { maxStalenessSeconds: 250 })
});
db1
.collection('test')
.find({})
.toArray(function (err) {
expect(err).to.not.exist;
expect(test.checkCommand).to.containSubset({
$query: { find: 'test', filter: {} },
$readPreference: { mode: 'secondary', maxStalenessSeconds: 250 }
});
client.close(done);
});
});
}
});
it(
'should correctly set maxStalenessSeconds on Mongos query using collection level readPreference',
{
metadata: {
requires: {
generators: true,
topology: 'single'
}
},
test: function (done) {
var self = this;
const configuration = this.configuration;
const client = configuration.newClient(`mongodb://${test.server.uri()}/test`);
client.connect(function (err, client) {
expect(err).to.not.exist;
var db = client.db(self.configuration.db);
// Get a db with a new readPreference
db.collection('test', {
readPreference: new ReadPreference('secondary', null, { maxStalenessSeconds: 250 })
})
.find({})
.toArray(function (err) {
expect(err).to.not.exist;
expect(test.checkCommand).to.containSubset({
$query: { find: 'test', filter: {} },
$readPreference: { mode: 'secondary', maxStalenessSeconds: 250 }
});
client.close(done);
});
});
}
}
);
it('should correctly set maxStalenessSeconds on Mongos query using cursor level readPreference', {
metadata: {
requires: {
generators: true,
topology: 'single'
}
},
test: function (done) {
var self = this;
const configuration = this.configuration;
const client = configuration.newClient(`mongodb://${test.server.uri()}/test`);
client.connect(function (err, client) {
expect(err).to.not.exist;
var db = client.db(self.configuration.db);
var readPreference = new ReadPreference('secondary', null, { maxStalenessSeconds: 250 });
// Get a db with a new readPreference
db.collection('test')
.find({})
.withReadPreference(readPreference)
.toArray(function (err) {
expect(err).to.not.exist;
expect(test.checkCommand).to.containSubset({
$query: { find: 'test', filter: {} },
$readPreference: { mode: 'secondary', maxStalenessSeconds: 250 }
});
client.close(done);
});
});
}
});
});