def test_replica_set_client(self):
        c = MockReplicaSetClient(standalones=[],
                                 members=['a:1', 'b:2', 'c:3'],
                                 mongoses=[],
                                 host='a:1,b:2,c:3',
                                 replicaSet='rs')

        self.assertTrue(('b', 2) in c.secondaries)
        self.assertTrue(('c', 3) in c.secondaries)

        # C is removed.
        c.mock_ismaster_hosts.remove('c:3')
        c.refresh()

        self.assertEqual(('a', 1), c.primary)
        self.assertEqual(set([('b', 2)]), c.secondaries)
    def test_replica_set_client(self):
        c = MockReplicaSetClient(
            standalones=[],
            members=['a:1', 'b:2', 'c:3'],
            mongoses=[],
            host='a:1,b:2,c:3',
            replicaSet='rs')

        self.assertTrue(('b', 2) in c.secondaries)
        self.assertTrue(('c', 3) in c.secondaries)

        # C is removed.
        c.mock_ismaster_hosts.remove('c:3')
        c.refresh()

        self.assertEqual(('a', 1), c.primary)
        self.assertEqual(set([('b', 2)]), c.secondaries)
    def test_replica_set_client(self):
        c = MockReplicaSetClient(standalones=[],
                                 members=['a:1', 'b:2'],
                                 mongoses=[],
                                 host='a:1',
                                 replicaSet='rs')

        self.assertEqual(('a', 1), c.primary)
        self.assertEqual(set([('b', 2)]), c.secondaries)

        # C is added.
        c.mock_members.append('c:3')
        c.mock_ismaster_hosts.append('c:3')
        c.refresh()

        self.assertEqual(('a', 1), c.primary)
        self.assertEqual(set([('b', 2), ('c', 3)]), c.secondaries)
    def test_replica_set_client(self):
        c = MockReplicaSetClient(
            standalones=[],
            members=['a:1', 'b:2'],
            mongoses=[],
            host='a:1',
            replicaSet='rs')

        self.assertEqual(('a', 1), c.primary)
        self.assertEqual(set([('b', 2)]), c.secondaries)

        # C is added.
        c.mock_members.append('c:3')
        c.mock_ismaster_hosts.append('c:3')
        c.refresh()

        self.assertEqual(('a', 1), c.primary)
        self.assertEqual(set([('b', 2), ('c', 3)]), c.secondaries)
    def test_socket_error_marks_member_down(self):
        c = MockReplicaSetClient(standalones=[],
                                 members=['a:1', 'b:2'],
                                 mongoses=[],
                                 host='a:1',
                                 replicaSet='rs')

        self.assertEqual(2, len(c._MongoReplicaSetClient__rs_state.members))

        # b now raises socket.error.
        c.mock_down_hosts.append('b:2')
        self.assertRaises(ConnectionFailure,
                          c.db.collection.find_one,
                          read_preference=ReadPreference.SECONDARY)

        self.assertEqual(1, len(c._MongoReplicaSetClient__rs_state.members))
    def test_max_write_batch_size(self):
        c = MockReplicaSetClient(standalones=[],
                                 members=['a:1', 'b:2'],
                                 mongoses=[],
                                 host='a:1',
                                 replicaSet='rs',
                                 _connect=False)

        c.set_max_write_batch_size('a:1', 1)
        c.set_max_write_batch_size('b:2', 2)

        # Starts with default max batch size.
        self.assertEqual(1000, c.max_write_batch_size)
        c.db.collection.find_one()  # Connect.

        # Uses primary's max batch size.
        self.assertEqual(c.max_write_batch_size, 1)

        # b becomes primary.
        c.mock_primary = 'b:2'
        c.refresh()
        self.assertEqual(c.max_write_batch_size, 2)
    def test_max_write_batch_size(self):
        c = MockReplicaSetClient(
            standalones=[],
            members=['a:1', 'b:2'],
            mongoses=[],
            host='a:1',
            replicaSet='rs',
            _connect=False)

        c.set_max_write_batch_size('a:1', 1)
        c.set_max_write_batch_size('b:2', 2)

        # Starts with default max batch size.
        self.assertEqual(1000, c.max_write_batch_size)
        c.db.collection.find_one()  # Connect.

        # Uses primary's max batch size.
        self.assertEqual(c.max_write_batch_size, 1)

        # b becomes primary.
        c.mock_primary = 'b:2'
        c.refresh()
        self.assertEqual(c.max_write_batch_size, 2)
    def test_wire_version(self):
        c = MockReplicaSetClient(standalones=[],
                                 members=['a:1', 'b:2', 'c:3'],
                                 mongoses=[],
                                 host='a:1',
                                 replicaSet='rs',
                                 _connect=False)

        c.set_wire_version_range('a:1', 1, 5)
        c.set_wire_version_range('b:2', 0, 1)
        c.set_wire_version_range('c:3', 1, 2)
        c.db.collection.find_one()  # Connect.
        self.assertEqual(c.min_wire_version, 1)
        self.assertEqual(c.max_wire_version, 5)

        c.set_wire_version_range('a:1', 2, 2)
        c.refresh()
        self.assertEqual(c.min_wire_version, 2)
        self.assertEqual(c.max_wire_version, 2)

        # A secondary doesn't overlap with us.
        c.set_wire_version_range('b:2', 5, 6)

        # refresh() raises, as do all following operations.
        self.assertRaises(ConfigurationError, c.refresh)
        self.assertRaises(ConfigurationError, c.db.collection.find_one)
        self.assertRaises(ConfigurationError, c.db.collection.insert, {})
    def test_wire_version(self):
        c = MockReplicaSetClient(
            standalones=[],
            members=['a:1', 'b:2', 'c:3'],
            mongoses=[],
            host='a:1',
            replicaSet='rs',
            _connect=False)

        c.set_wire_version_range('a:1', 1, 5)
        c.set_wire_version_range('b:2', 0, 1)
        c.set_wire_version_range('c:3', 1, 2)
        c.db.collection.find_one()  # Connect.
        self.assertEqual(c.min_wire_version, 1)
        self.assertEqual(c.max_wire_version, 5)

        c.set_wire_version_range('a:1', 2, 2)
        c.refresh()
        self.assertEqual(c.min_wire_version, 2)
        self.assertEqual(c.max_wire_version, 2)

        # A secondary doesn't overlap with us.
        c.set_wire_version_range('b:2', 5, 6)

        # refresh() raises, as do all following operations.
        self.assertRaises(ConfigurationError, c.refresh)
        self.assertRaises(ConfigurationError, c.db.collection.find_one)
        self.assertRaises(ConfigurationError, c.db.collection.insert, {})