<commit_before># Copyright 2013 MongoDB, 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.

"""Test MongoReplicaSetClients and replica set configuration changes."""

import socket
import sys
import unittest

sys.path[0:0] = [""]

from pymongo import MongoClient
from pymongo.errors import AutoReconnect
from pymongo.mongo_replica_set_client import MongoReplicaSetClient
from pymongo.pool import Pool
from test import host as default_host, port as default_port


class MockPool(Pool):
    def __init__(self, pair, *args, **kwargs):
        if pair:
            # RS client passes 'pair' to Pool's constructor.
            self.mock_host, self.mock_port = pair
        else:
            # MongoClient passes pair to get_socket() instead.
            self.mock_host, self.mock_port = None, None

        Pool.__init__(
            self,
            pair=(default_host, default_port),
            max_size=None,
            net_timeout=None,
            conn_timeout=20,
            use_ssl=False,
            use_greenlets=False)

    def get_socket(self, pair=None, force=False):
        sock_info = Pool.get_socket(self, (default_host, default_port), force)
        sock_info.host = self.mock_host or pair[0]
        return sock_info


MOCK_HOSTS = ['a:27017', 'b:27017', 'c:27017']
MOCK_PRIMARY = MOCK_HOSTS[0]
MOCK_RS_NAME = 'rs'


class MockClientBase(object):
    def __init__(self):
        self.mock_hosts = MOCK_HOSTS

        # Hosts that should raise socket errors.
        self.mock_down_hosts = []

        # Hosts that should respond to ismaster as if they're standalone.
        self.mock_standalone_hosts = []

    def mock_is_master(self, host):
        if host in self.mock_down_hosts:
            raise socket.timeout('timed out')

        if host in self.mock_standalone_hosts:
            return {'ismaster': True}

        if host not in self.mock_hosts:
            # Host removed from set by a reconfig.
            return {'ismaster': False, 'secondary': False}
<commit_msg>TestSecondaryRemoved shouldn't change global state<commit_after># Copyright 2013 MongoDB, 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.

"""Test MongoReplicaSetClients and replica set configuration changes."""

import socket
import sys
import unittest

sys.path[0:0] = [""]

from pymongo import MongoClient
from pymongo.errors import AutoReconnect
from pymongo.mongo_replica_set_client import MongoReplicaSetClient
from pymongo.pool import Pool
from test import host as default_host, port as default_port


class MockPool(Pool):
    def __init__(self, pair, *args, **kwargs):
        if pair:
            # RS client passes 'pair' to Pool's constructor.
            self.mock_host, self.mock_port = pair
        else:
            # MongoClient passes pair to get_socket() instead.
            self.mock_host, self.mock_port = None, None

        Pool.__init__(
            self,
            pair=(default_host, default_port),
            max_size=None,
            net_timeout=None,
            conn_timeout=20,
            use_ssl=False,
            use_greenlets=False)

    def get_socket(self, pair=None, force=False):
        sock_info = Pool.get_socket(self, (default_host, default_port), force)
        sock_info.host = self.mock_host or pair[0]
        return sock_info


MOCK_HOSTS = ['a:27017', 'b:27017', 'c:27017']
MOCK_PRIMARY = MOCK_HOSTS[0]
MOCK_RS_NAME = 'rs'


class MockClientBase(object):
    def __init__(self):
        self.mock_hosts = MOCK_HOSTS[:]

        # Hosts that should raise socket errors.
        self.mock_down_hosts = []

        # Hosts that should respond to ismaster as if they're standalone.
        self.mock_standalone_hosts = []

    def mock_is_master(self, host):
        if host in self.mock_down_hosts:
            raise socket.timeout('timed out')

        if host in self.mock_standalone_hosts:
            return {'ismaster': True}

        if host not in self.mock_hosts:
            # Host removed from set by a reconfig.
            return {'ismaster': False, 'secondary': False}

{'fraction_non_alphanumeric': 0.0823716673298846, 'fraction_numerical': 0.011539992041384799, 'mean_word_length': 2.936570086139389, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, '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': '7038478', 'n_tokens_mistral': 1507, 'n_tokens_neox': 1426, 'n_words': 488}