Skip to content

Commit 17078b8

Browse files
committed
CXX-612 attach read preference properly in count/distinct commands
1 parent 935f2b4 commit 17078b8

File tree

1 file changed

+30
-27
lines changed

1 file changed

+30
-27
lines changed

src/mongo/integration/replica_set/read_preference.cpp

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -103,40 +103,43 @@ namespace {
103103
test_conn->findOne(TEST_NS, q);
104104
}
105105

106+
BSONObj makeReadPreferenceObject(ReadPreference rp) {
107+
switch (rp) {
108+
case ReadPreference_PrimaryOnly:
109+
return BSON("mode" << "primary");
110+
case ReadPreference_PrimaryPreferred:
111+
return BSON("mode" << "primaryPreferred");
112+
case ReadPreference_SecondaryOnly:
113+
return BSON("mode" << "secondary");
114+
case ReadPreference_SecondaryPreferred:
115+
return BSON("mode" << "secondaryPreferred");
116+
case ReadPreference_Nearest:
117+
return BSON("mode" << "nearest");
118+
default:
119+
return BSONObj();
120+
}
121+
}
122+
123+
BSONObj makeTestCommand(const std::string& cmdName, ReadPreference rp) {
124+
BSONObjBuilder cmd;
125+
cmd.append("query", BSON(cmdName << TEST_COLL));
126+
cmd.append("$readPreference", makeReadPreferenceObject(rp));
127+
return cmd.obj();
128+
}
129+
106130
void count(const auto_ptr<DBClientReplicaSet>& test_conn, ReadPreference rp) {
107-
Query q = Query().readPref(rp, BSONArray());
108-
test_conn->count(TEST_NS, q, QueryOption_SlaveOk);
131+
BSONObj ignoredResult;
132+
test_conn->runCommand(TEST_DB, makeTestCommand("count", rp), ignoredResult);
109133
}
110134

111135
void distinct(const auto_ptr<DBClientReplicaSet>& test_conn, ReadPreference rp) {
112-
Query q = Query().readPref(rp, BSONArray());
113-
test_conn->distinct(TEST_NS, "a", q);
136+
BSONObj ignoredResult;
137+
test_conn->runCommand(TEST_DB, makeTestCommand("distinct", rp), ignoredResult);
114138
}
115139

116140
void collStats(const auto_ptr<DBClientReplicaSet>& test_conn, ReadPreference rp) {
117-
BSONObjBuilder cmd;
118-
cmd.append("query", BSON("collStats" << TEST_COLL));
119-
120-
switch (rp) {
121-
case ReadPreference_PrimaryOnly:
122-
cmd.append("$readPreference", BSON("mode" << "primary"));
123-
break;
124-
case ReadPreference_PrimaryPreferred:
125-
cmd.append("$readPreference", BSON("mode" << "primaryPreferred"));
126-
break;
127-
case ReadPreference_SecondaryOnly:
128-
cmd.append("$readPreference", BSON("mode" << "secondary"));
129-
break;
130-
case ReadPreference_SecondaryPreferred:
131-
cmd.append("$readPreference", BSON("mode" << "secondaryPreferred"));
132-
break;
133-
case ReadPreference_Nearest:
134-
// for completeness
135-
break;
136-
}
137-
138-
BSONObj info;
139-
test_conn->runCommand(TEST_DB, cmd.obj(), info);
141+
BSONObj ignoredResult;
142+
test_conn->runCommand(TEST_DB, makeTestCommand("collStats", rp), ignoredResult);
140143
}
141144

142145
TEST_F(ReadPreferenceTest, RoutingQuery) {

0 commit comments

Comments
 (0)