Ejemplo n.º 1
0
    def test_gevent_pool_ping(self):

        servers = StaticServerList([])
        for i in range(10):
            servers.add_server('127.0.0.%s' % i)

        ping = _MockPing(delay=0.1)
        strategy = GeventPingStrategy()

        results = strategy.ping(ping, servers)

        # verify the results
        self.assertEqual(10, len(results))

        for server in results:
            self.assertTrue(server.is_alive)
Ejemplo n.º 2
0
    def _create_service():

        servers = StaticServerList(_EXPECTED_SERVERS)
        load_balancer = LoadBalancer(servers,
                                     ping=ping.DummyPing(),
                                     ping_on_start=False)
        load_balancer.ping()

        return Service(load_balancer, use_https=True, request_timeout=0.1)
Ejemplo n.º 3
0
    def test_no_servers_reachable(self):

        servers = StaticServerList(['127.0.0.1', '127.0.0.2', '127.0.0.3'])
        load_balancer = LoadBalancer(servers,
                                     ping=_MockPing(False),
                                     ping_on_start=False)

        rule = RoundRobinRule()
        rule.load_balancer = load_balancer

        self.assertRaises(BallastException, rule.choose)
Ejemplo n.º 4
0
    def test_serial_ping(self):

        servers = StaticServerList(['127.0.0.1', '127.0.0.2', '127.0.0.3'])
        ping = _MockPing(delay=0.4)
        strategy = SerialPingStrategy()

        results = strategy.ping(ping, servers)

        # verify the results
        self.assertEqual(3, len(results))

        for server in results:
            self.assertTrue(server.is_alive)
Ejemplo n.º 5
0
    def test_equal_choice(self):

        servers = StaticServerList(['127.0.0.1', '127.0.0.2', '127.0.0.3'])
        load_balancer = LoadBalancer(servers,
                                     ping=DummyPing(),
                                     ping_on_start=False)

        # ping our servers once to set them available
        load_balancer.ping()

        rule = RoundRobinRule()
        rule.load_balancer = load_balancer

        stats = dict()

        expected_iterations = 1000

        # each should be chosen an equal number of times
        # loop a bunch to get some stats
        for i in range(expected_iterations):
            server1 = rule.choose()
            if server1 in stats:
                stats[server1] += 1
            else:
                stats[server1] = 1

            server2 = rule.choose()
            if server2 in stats:
                stats[server2] += 1
            else:
                stats[server2] = 1

            server3 = rule.choose()
            if server3 in stats:
                stats[server3] += 1
            else:
                stats[server3] = 1

        # all 3 should have been chosen the same number of times
        self.assertEqual(3, len(stats))
        for server in stats:
            self.assertEqual(expected_iterations, stats[server])
Ejemplo n.º 6
0
    def __init__(self, *args, **kwargs):
        self._load_balancer = kwargs.get('load_balancer')
        self._use_https = kwargs.get('use_https', False)
        self._request_timeout = kwargs.get('request_timeout', self.DEFAULT_REQUEST_TIMEOUT)
        self._logger = logging.getLogger(self.__module__)

        # if our load balancer wasn't configured via kwargs
        # and there are no positional args, we have a problem
        if self._load_balancer is None and len(args) == 0:
            raise BallastConfigurationException(
                "Expected either a collection of server "
                "addresses OR a ballast.LoadBalancer "
                "instance as an init parameter."
            )

        # either the load balancer is specified or the collection, not both
        if self._load_balancer is not None and len(args) > 0:
            raise BallastConfigurationException(
                "Keyword arg 'load_balancer' specified in addition to positional arg. "
                "Please either remove the positional arg, or specify the load balancer "
                "as the positional arg."
            )

        # load balancer or collection of servers
        # can be defined via 1st positional arg
        # (but only if load balancer not set via kwargs)
        a = args[0]
        if isinstance(a, LoadBalancer):
            self._load_balancer = a
        elif isinstance(a, ServerList):
            servers = a
            self._load_balancer = LoadBalancer(servers)
        elif hasattr(a, '__iter__') and not isinstance(a, basestring):
            servers = StaticServerList(a)
            self._load_balancer = LoadBalancer(servers)
        else:
            raise BallastConfigurationException(
                "An invalid configuration parameter was provided: %s" %
                a
            )