@@ -150,6 +150,57 @@ public void testSecondaryReads3() throws Exception {
150
150
verifySecondaryCounts (secondaryCount , testHosts );
151
151
}
152
152
153
+ @ Test (groups = {"basic" })
154
+ public void testSecondaryReadCursor () throws Exception {
155
+
156
+ final Mongo mongo = loadMongo ();
157
+
158
+ final CommandResult result = serverStatusCmd (mongo );
159
+
160
+ // If the result is null, this is not a replica set.
161
+ if (result == null ) return ;
162
+
163
+ final List <TestHost > testHosts = new ArrayList <TestHost >();
164
+ final String primaryHostnameAndPort = extractHosts (result , testHosts );
165
+ final DBCollection col = loadCleanDbCollection (mongo );
166
+
167
+ final List <ObjectId > insertedIds = insertTestData (col );
168
+
169
+ // Get the opcounter/query data for the hosts.
170
+ loadQueryCount (testHosts , true );
171
+
172
+ final int secondaryCount = getSecondaryCount (testHosts );
173
+
174
+ // Perform some reads on the secondaries
175
+ col .setReadPreference (ReadPreference .SECONDARY );
176
+
177
+ final DBCursor cur = col .find ();
178
+
179
+ ServerAddress curServerAddress = cur .getServerAddress ();
180
+
181
+ assertEquals (true , serverIsSecondary (curServerAddress , testHosts ));
182
+
183
+ try {
184
+ while (cur .hasNext ()) {
185
+ cur .next ();
186
+ assertEquals (true , serverIsSecondary (cur .getServerAddress (), testHosts ));
187
+ }
188
+ } finally { cur .close (); }
189
+
190
+ loadQueryCount (testHosts , false );
191
+ }
192
+
193
+ private boolean serverIsSecondary (final ServerAddress pServerAddr , final List <TestHost > pHosts ) {
194
+ for (final TestHost h : pHosts ) {
195
+ if (!h .stateStr .equals ("SECONDARY" )) continue ;
196
+ final int portIdx = h .hostnameAndPort .indexOf (":" );
197
+ final int port = Integer .parseInt (h .hostnameAndPort .substring (portIdx +1 , h .hostnameAndPort .length ()));
198
+ final String hostname = h .hostnameAndPort .substring (0 , portIdx );
199
+ if (pServerAddr .getPort () == port && hostname .equals (pServerAddr .getHost ())) return true ;
200
+ }
201
+
202
+ return false ;
203
+ }
153
204
154
205
private Mongo loadMongo () throws Exception {
155
206
return new Mongo (new MongoURI ("mongodb://127.0.0.1:27017,127.0.0.1:27018" ));
0 commit comments