def test___eq__(self):
        servers = [('ham1', 0.2), ('ham2', 0.8)]
        balancer1 = balancing.WeightedRoundRobin(*servers)
        balancer2 = balancing.WeightedRoundRobin(*servers)
        self.assertTrue(balancer1 == balancer2)

        servers = [('ham1', 0.2), ('ham2', 0.3), ('ham3', 0.5)]
        balancer3 = balancing.WeightedRoundRobin(*servers)
        self.assertFalse(balancer1 == balancer3)
    def test_get_next(self):
        servers = [('ham1', 0.2), ('ham2', 0.8)]
        balancer = balancing.WeightedRoundRobin(*servers)
        self.assertEqual(('ham2', Decimal('0.8')), balancer.get_next())
        self.assertEqual([1, 0], balancer.load)
        balancer._load = [80, 0]
        self.assertEqual(('ham1', Decimal('0.2')), balancer.get_next())
        self.assertEqual([80, 1], balancer.load)
        balancer._load = [80, 20]
        self.assertEqual(('ham2', Decimal('0.8')), balancer.get_next())
        self.assertEqual([81, 20], balancer.load)

        servers = [('ham1', 0.1), ('ham2', 0.2), ('ham3', 0.7)]
        balancer = balancing.WeightedRoundRobin(*servers)
        exp_sum = count = 101
        while count > 0:
            count -= 1
            _ = balancer.get_next()
        self.assertEqual(exp_sum, sum(balancer.load))
        self.assertEqual([34, 34, 33], balancer.load)

        servers = [('ham1', 0.2), ('ham2', 0.2), ('ham3', 0.7)]
        balancer = balancing.WeightedRoundRobin(*servers)
        exp_sum = count = 101
        while count > 0:
            count -= 1
            _ = balancer.get_next()
        self.assertEqual(exp_sum, sum(balancer.load))
        self.assertEqual([34, 34, 33], balancer.load)

        servers = [('ham1', 0.25), ('ham2', 0.25), ('ham3', 0.25),
                   ('ham4', 0.25)]
        balancer = balancing.WeightedRoundRobin(*servers)
        exp_sum = count = 101
        while count > 0:
            count -= 1
            _ = balancer.get_next()
        self.assertEqual(exp_sum, sum(balancer.load))
        self.assertEqual([26, 25, 25, 25], balancer.load)

        servers = [('ham1', 0.5), ('ham2', 0.5)]
        balancer = balancing.WeightedRoundRobin(*servers)
        count = 201
        while count > 0:
            count -= 1
            _ = balancer.get_next()
        self.assertEqual(1, sum(balancer.load))
        self.assertEqual([1, 0], balancer.load)
    def test___init__(self):
        balancer = balancing.WeightedRoundRobin()
        self.assertEqual([], balancer._members)
        self.assertEqual([], balancer._ratios)
        self.assertEqual([], balancer._load)

        # init with args
        class FakeWRR(balancing.WeightedRoundRobin):
            def set_members(self, *args):
                self.set_members_called = True
        balancer = FakeWRR('ham', 'spam')
        self.assertTrue(balancer.set_members_called)
    def test_set_members(self):
        balancer = balancing.WeightedRoundRobin()
        balancer._members = ['ham']
        balancer.set_members()
        self.assertEqual([], balancer.members)

        servers = [('ham1', 0.2), ('ham2', 0.8)]

        balancer.set_members(*servers)
        exp = [('ham2', Decimal('0.8')), ('ham1', Decimal('0.2'))]
        self.assertEqual(exp, balancer.members)
        self.assertEqual([400, 100], balancer.ratios)
        self.assertEqual([0, 0], balancer.load)
示例#5
0
    def test_get_group_servers(self):
        fab = _MockupFabric(_HOST, _PORT)
        fab.seed()

        exp = [
            # Secondary
            fabric.FabricMySQLServer(
                uuid='a6ac2895-574f-11e3-bc32-bcaec56cc4a7',
                group='testgroup1',
                host='tests.example.com',
                port=3372,
                mode=1,
                status=2,
                weight=1.0),
            # Primary
            fabric.FabricMySQLServer(
                uuid='af1cb1e4-574f-11e3-bc33-bcaec56cc4a7',
                group='testgroup1',
                host='tests.example.com',
                port=3373,
                mode=3,
                status=3,
                weight=1.0),
        ]

        self.assertEqual(exp, fab.get_group_servers('testgroup1'))
        self.assertEqual(exp,
                         fab.get_group_servers('testgroup1', use_cache=False))

        exp_balancers = {
            'testgroup1':
            balancing.WeightedRoundRobin((exp[0].uuid, exp[0].weight))
        }
        self.assertEqual(exp_balancers, fab._group_balancers)

        # No instances available, checking cache
        fab._fabric_instances = {}
        fab.get_group_servers('testgroup1')
        self.assertEqual(exp, fab.get_group_servers('testgroup1'))

        # Force lookup
        self.assertRaises(errors.InterfaceError,
                          fab.get_group_servers,
                          'testgroup1',
                          use_cache=False)
 def test___repr__(self):
     balancer = balancing.WeightedRoundRobin(*[('ham1', 0.2), ('ham2',
                                                               0.8)])
     exp = ("<class 'mysql.connector.fabric.balancing.WeightedRoundRobin'>"
            "(load=[0, 0], ratios=[400, 100])")
     self.assertEqual(exp, repr(balancer))
 def test_reset_load(self):
     balancer = balancing.WeightedRoundRobin(*[('ham1', 0.2), ('ham2',
                                                               0.8)])
     balancer._load = [5, 6]
     balancer.reset()
     self.assertEqual([0, 0], balancer.load)
 def test_load(self):
     balancer = balancing.WeightedRoundRobin()
     self.assertEqual([], balancer.load)
     balancer._load = ['ham']
     self.assertEqual(['ham'], balancer.load)
 def test_ratios(self):
     balancer = balancing.WeightedRoundRobin()
     self.assertEqual([], balancer.ratios)
     balancer._ratios = ['ham']
     self.assertEqual(['ham'], balancer.ratios)
示例#10
0
 def test_members(self):
     balancer = balancing.WeightedRoundRobin()
     self.assertEqual([], balancer.members)
     balancer._members = ['ham']
     self.assertEqual(['ham'], balancer.members)