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
|
// Tests that member functions setSecondaryOk()/getSecondaryOk() and their
// aliases, setSlaveOk()/getSlaveOk(), produce the same results.
(function() {
"use strict";
const dbName = "test";
const collName = "coll";
const rst = new ReplSetTest({nodes: 2});
rst.startSet();
rst.initiate();
const primary = rst.getPrimary();
const secondary = rst.getSecondary();
assert.commandWorked(primary.getDB(dbName)[collName].insert({x: 1}));
rst.awaitReplication();
// secondaryOk is initially set to true in awaitReplication, so reads on secondaries should
// succeed.
assert.eq(secondary.getDB(dbName).getMongo().getSecondaryOk(), true);
assert.eq(secondary.getDB(dbName).getSecondaryOk(), true);
assert.commandWorked(secondary.getDB(dbName).runCommand({find: collName}),
"find command failed with an unexpected error");
// Set secondaryOk to false, disallowing reads on secondaries.
secondary.getDB(dbName).getMongo().setSecondaryOk(false);
assert.eq(secondary.getDB(dbName).getMongo().getSecondaryOk(), false);
assert.commandFailedWithCode(secondary.getDB(dbName).runCommand({find: collName}),
ErrorCodes.NotPrimaryNoSecondaryOk,
"find did not fail with the correct error code");
// setSlaveOk() is deprecated and aliased to setSecondaryOk(), but ensure
// it still works for backwards compatibility.
secondary.getDB(dbName).getMongo().setSlaveOk();
assert.eq(secondary.getDB(dbName).getMongo().getSlaveOk(), true);
assert.eq(secondary.getDB(dbName).getSlaveOk(), true);
assert.commandWorked(secondary.getDB(dbName).runCommand({find: collName}),
"find command failed with an unexpected error");
// Set slaveOk to false, disallowing reads on secondaries.
secondary.getDB(dbName).getMongo().setSlaveOk(false);
assert.eq(secondary.getDB(dbName).getMongo().getSlaveOk(), false);
assert.commandFailedWithCode(secondary.getDB(dbName).runCommand({find: collName}),
ErrorCodes.NotPrimaryNoSecondaryOk,
"find did not fail with the correct error code");
rst.stopSet();
})();
|