<commit_before>/**
 * Copyright (C) 2008 10gen Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mongodb;

// Mongo
import com.mongodb.*;
import org.bson.types.*;
import com.mongodb.util.*;

import org.testng.annotations.Test;


// Java
import java.util.*;
import java.util.concurrent.*;

public class SecondaryReadTest extends TestCase {


    private static final int INSERT_COUNT = 1000;

    private static final int ITERATION_COUNT = 100;

    private static final int TOTAL_COUNT = INSERT_COUNT * ITERATION_COUNT;

    private static final double MAX_DEVIATION_PERCENT = 1.0;

    @Test(groups = {"basic"})
    public void testSecondaryReads1() throws Exception {

        final Mongo mongo = loadMongo();

        final CommandResult result = serverStatusCmd(mongo);

        // If the result is null, this is not a replica set.
        if (result == null) return;

        final List<TestHost> testHosts = new ArrayList<TestHost>();
        final String primaryHostnameAndPort = extractHosts(result, testHosts);
        final DBCollection col = loadCleanDbCollection(mongo);

        final List<ObjectId> insertedIds = insertTestData(col);

        // Get the opcounter/query data for the hosts.
        loadQueryCount(testHosts, true);

        final int secondaryCount = getSecondaryCount(testHosts);

        // Perform some reads on the secondaries
        col.setReadPreference(ReadPreference.SECONDARY);

        for (int idx=0; idx < ITERATION_COUNT; idx++) {
            for (ObjectId id : insertedIds) {
                final BasicDBObject doc = (BasicDBObject)col.findOne(new BasicDBObject("_id", id));
                if (doc == null) throw new IllegalStateException("Doc not found");
                if (!doc.getObjectId("_id").equals(id)) throw new IllegalStateException("Ids are off");
            }
        }

        loadQueryCount(testHosts, false);

        verifySecondaryCounts(secondaryCount, testHosts);
   }

    @Test(groups = {"basic"})
    public void testSecondaryReads2() throws Exception {

        final Mongo mongo = loadMongo();

        final CommandResult result = serverStatusCmd(mongo);

        // If the result is null, this is not a replica set.
        if (result == null) return;

        final List<TestHost> testHosts = new ArrayList<TestHost>();
        final String primaryHostnameAndPort = extractHosts(result, testHosts);
        final DBCollection col = loadCleanDbCollection(mongo);

        final List<ObjectId> insertedIds = insertTestData(col);

        // Get the opcounter/query data for the hosts.
        loadQueryCount(testHosts, true);

        final int secondaryCount = getSecondaryCount(testHosts);

        // Perform some reads on the secondaries
        mongo.setReadPreference(ReadPreference.SECONDARY);

        for (int idx=0; idx < ITERATION_COUNT; idx++) {
            for (ObjectId id : insertedIds) {
                final BasicDBObject doc = (BasicDBObject)col.findOne(new BasicDBObject("_id", id));
                if (doc == null) throw new IllegalStateException("Doc not found");
                if (!doc.getObjectId("_id").equals(id)) throw new IllegalStateException("Ids are off");
            }
        }

        loadQueryCount(testHosts, false);

        verifySecondaryCounts(secondaryCount, testHosts);
   }

    private Mongo loadMongo() throws Exception {
        return new Mongo(new MongoURI("mongodb://127.0.0.1:27017,127.0.0.1:27018"));
    }

    private CommandResult serverStatusCmd(final Mongo pMongo) {
        // Check to see if this is a replica set... if not, get out of here.
        final CommandResult result = pMongo.getDB("admin").command(new BasicDBObject("replSetGetStatus", 1));

        final String errorMsg = result.getErrorMessage();

        if (errorMsg != null && errorMsg.indexOf("--replSet") != -1) {
            System.err.println("---- SecondaryReadTest: This is not a replica set - not testing secondary reads");
            return null;
        }

        return result;
    }

    @SuppressWarnings({"unchecked"})
    private String extractHosts(final CommandResult pResult, final List<TestHost> pHosts) {
        String primaryHostnameAndPort = null;
        // Extract the repl set members.

        for (final BasicDBObject member : (List<BasicDBObject>)pResult.get("members")) {
            String hostnameAndPort = member.getString("name");
            if (hostnameAndPort.indexOf(":") == -1) hostnameAndPort = hostnameAndPort + ":27017";

            final String stateStr = member.getString("stateStr");<commit_msg>added another test - this time setting read preference on the db object.<commit_after>/**
 * Copyright (C) 2008 10gen Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mongodb;

// Mongo
import com.mongodb.*;
import org.bson.types.*;
import com.mongodb.util.*;

import org.testng.annotations.Test;


// Java
import java.util.*;
import java.util.concurrent.*;

public class SecondaryReadTest extends TestCase {


    private static final int INSERT_COUNT = 1000;

    private static final int ITERATION_COUNT = 100;

    private static final int TOTAL_COUNT = INSERT_COUNT * ITERATION_COUNT;

    private static final double MAX_DEVIATION_PERCENT = 1.0;

    @Test(groups = {"basic"})
    public void testSecondaryReads1() throws Exception {

        final Mongo mongo = loadMongo();

        final CommandResult result = serverStatusCmd(mongo);

        // If the result is null, this is not a replica set.
        if (result == null) return;

        final List<TestHost> testHosts = new ArrayList<TestHost>();
        final String primaryHostnameAndPort = extractHosts(result, testHosts);
        final DBCollection col = loadCleanDbCollection(mongo);

        final List<ObjectId> insertedIds = insertTestData(col);

        // Get the opcounter/query data for the hosts.
        loadQueryCount(testHosts, true);

        final int secondaryCount = getSecondaryCount(testHosts);

        // Perform some reads on the secondaries
        col.setReadPreference(ReadPreference.SECONDARY);

        for (int idx=0; idx < ITERATION_COUNT; idx++) {
            for (ObjectId id : insertedIds) {
                final BasicDBObject doc = (BasicDBObject)col.findOne(new BasicDBObject("_id", id));
                if (doc == null) throw new IllegalStateException("Doc not found");
                if (!doc.getObjectId("_id").equals(id)) throw new IllegalStateException("Ids are off");
            }
        }

        loadQueryCount(testHosts, false);

        verifySecondaryCounts(secondaryCount, testHosts);
   }

    @Test(groups = {"basic"})
    public void testSecondaryReads2() throws Exception {

        final Mongo mongo = loadMongo();

        final CommandResult result = serverStatusCmd(mongo);

        // If the result is null, this is not a replica set.
        if (result == null) return;

        final List<TestHost> testHosts = new ArrayList<TestHost>();
        final String primaryHostnameAndPort = extractHosts(result, testHosts);
        final DBCollection col = loadCleanDbCollection(mongo);

        final List<ObjectId> insertedIds = insertTestData(col);

        // Get the opcounter/query data for the hosts.
        loadQueryCount(testHosts, true);

        final int secondaryCount = getSecondaryCount(testHosts);

        // Perform some reads on the secondaries
        mongo.setReadPreference(ReadPreference.SECONDARY);

        for (int idx=0; idx < ITERATION_COUNT; idx++) {
            for (ObjectId id : insertedIds) {
                final BasicDBObject doc = (BasicDBObject)col.findOne(new BasicDBObject("_id", id));
                if (doc == null) throw new IllegalStateException("Doc not found");
                if (!doc.getObjectId("_id").equals(id)) throw new IllegalStateException("Ids are off");
            }
        }

        loadQueryCount(testHosts, false);

        verifySecondaryCounts(secondaryCount, testHosts);
   }

    @Test(groups = {"basic"})
    public void testSecondaryReads3() throws Exception {

        final Mongo mongo = loadMongo();

        final CommandResult result = serverStatusCmd(mongo);

        // If the result is null, this is not a replica set.
        if (result == null) return;

        final List<TestHost> testHosts = new ArrayList<TestHost>();
        final String primaryHostnameAndPort = extractHosts(result, testHosts);
        final DBCollection col = loadCleanDbCollection(mongo);

        final List<ObjectId> insertedIds = insertTestData(col);

        // Get the opcounter/query data for the hosts.
        loadQueryCount(testHosts, true);

        final int secondaryCount = getSecondaryCount(testHosts);

        // Perform some reads on the secondaries
        col.getDB().setReadPreference(ReadPreference.SECONDARY);

        for (int idx=0; idx < ITERATION_COUNT; idx++) {
            for (ObjectId id : insertedIds) {
                final BasicDBObject doc = (BasicDBObject)col.findOne(new BasicDBObject("_id", id));
                if (doc == null) throw new IllegalStateException("Doc not found");
                if (!doc.getObjectId("_id").equals(id)) throw new IllegalStateException("Ids are off");
            }
        }

        loadQueryCount(testHosts, false);

        verifySecondaryCounts(secondaryCount, testHosts);
   }


    private Mongo loadMongo() throws Exception {
        return new Mongo(new MongoURI("mongodb://127.0.0.1:27017,127.0.0.1:27018"));
    }

    private CommandResult serverStatusCmd(final Mongo pMongo) {
        // Check to see if this is a replica set... if not, get out of here.
        final CommandResult result = pMongo.getDB("admin").command(new BasicDBObject("replSetGetStatus", 1));

        final String errorMsg = result.getErrorMessage();

        if (errorMsg != null && errorMsg.indexOf("--replSet") != -1) {
            System.err.println("---- SecondaryReadTest: This is not a replica set - not testing secondary reads");
            return null;
        }

        return result;
    }

    @SuppressWarnings({"unchecked"})
    private String extractHosts(final CommandResult pResult, final List<TestHost> pHosts) {
        String primaryHostnameAndPort = null;
        // Extract the repl set members.

        for (final BasicDBObject member : (List<BasicDBObject>)pResult.get("members")) {
            String hostnameAndPort = member.getString("name");
            if (hostnameAndPort.indexOf(":") == -1) hostnameAndPort = hostnameAndPort + ":27017";

            final String stateStr = member.getString("stateStr");
{'fraction_non_alphanumeric': 0.09867795731443878, 'fraction_numerical': 0.009332066015726259, 'mean_word_length': 2.887806516627477, 'pattern_counts': {'":': 4, '<': 27, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3510136', 'n_tokens_mistral': 3275, 'n_tokens_neox': 3053, 'n_words': 975}