Пример #1
0
    def __get_rest_hosts(self):
        """
        Return the list of hosts as they should be tried. First comes the main
        host. Then the fallback hosts in random order.
        The returned list will have a length of up to http_max_retry_count.
        """
        # Defaults
        host = self.rest_host
        if host is None:
            host = Defaults.rest_host

        environment = self.environment
        if environment is None:
            environment = Defaults.environment

        http_max_retry_count = self.http_max_retry_count
        if http_max_retry_count is None:
            http_max_retry_count = Defaults.http_max_retry_count

        # Prepend environment
        if environment != 'production':
            host = '%s-%s' % (environment, host)

        # Fallback hosts
        fallback_hosts = self.fallback_hosts
        if fallback_hosts is None:
            if host == Defaults.rest_host or self.fallback_hosts_use_default:
                fallback_hosts = Defaults.fallback_hosts
            elif environment != 'production':
                fallback_hosts = Defaults.get_environment_fallback_hosts(environment)
            else:
                fallback_hosts = []

        # Explicit warning about deprecating the option
        if self.fallback_hosts_use_default:
            if environment != Defaults.environment:
                warnings.warn(
                    "It is no longer required to set 'fallback_hosts_use_default', the correct fallback hosts "
                    "are now inferred from the environment, 'fallback_hosts': {}"
                    .format(','.join(fallback_hosts)), DeprecationWarning
                )
            else:
                warnings.warn(
                    "It is no longer required to set 'fallback_hosts_use_default': 'fallback_hosts': {}"
                    .format(','.join(fallback_hosts)), DeprecationWarning
                )

        # Shuffle
        fallback_hosts = list(fallback_hosts)
        random.shuffle(fallback_hosts)

        # First main host
        hosts = [host] + fallback_hosts
        hosts = hosts[:http_max_retry_count]
        return hosts
Пример #2
0
    def test_fallback_hosts(self):
        # Specify the fallback_hosts (RSC15a)
        fallback_hosts = [
            ['fallback1.com', 'fallback2.com'],
            [],
        ]

        # Fallback hosts specified (RSC15g1)
        for aux in fallback_hosts:
            ably = AblyRest(token='foo', fallback_hosts=aux)
            assert sorted(aux) == sorted(
                ably.options.get_fallback_rest_hosts())

        # Specify environment (RSC15g2)
        ably = AblyRest(token='foo',
                        environment='sandbox',
                        http_max_retry_count=10)
        assert sorted(
            Defaults.get_environment_fallback_hosts('sandbox')) == sorted(
                ably.options.get_fallback_rest_hosts())

        # Fallback hosts and environment not specified (RSC15g3)
        ably = AblyRest(token='foo', http_max_retry_count=10)
        assert sorted(Defaults.fallback_hosts) == sorted(
            ably.options.get_fallback_rest_hosts())

        # Specify environment and fallback_hosts_use_default, no fallback hosts (RSC15g4)
        # We specify http_max_retry_count=10 so all the fallback hosts get in the list
        ably = AblyRest(token='foo',
                        environment='not_considered',
                        fallback_hosts_use_default=True,
                        http_max_retry_count=10)
        assert sorted(Defaults.fallback_hosts) == sorted(
            ably.options.get_fallback_rest_hosts())

        # RSC15f
        ably = AblyRest(token='foo')
        assert 600000 == ably.options.fallback_retry_timeout
        ably = AblyRest(token='foo', fallback_retry_timeout=1000)
        assert 1000 == ably.options.fallback_retry_timeout

        with warnings.catch_warnings(record=True) as ws:
            # Cause all warnings to always be triggered
            warnings.simplefilter("always")
            AblyRest(token='foo', fallback_hosts_use_default=True)
            # Verify warning is raised for fallback_hosts_use_default
            ws = [w for w in ws if issubclass(w.category, DeprecationWarning)]
            assert len(ws) == 1