diff --git a/ably/pubsub/request/http.py b/ably/pubsub/request/http.py index 1141caef..52375c62 100644 --- a/ably/pubsub/request/http.py +++ b/ably/pubsub/request/http.py @@ -165,10 +165,9 @@ def get_hosts(self): self.__host_expires = None return hosts - hosts = list(hosts) - hosts.remove(host) - hosts.insert(0, host) - return hosts + # The realtime connection tries every fallback host, so the saved one may not be among + # these. Put it first and keep the number of attempts unchanged. + return [host, *(other for other in hosts if other != host)][:len(hosts)] @reauth_if_expired async def make_request(self, method, path, version=None, headers=None, body=None, diff --git a/test/unit/http_test.py b/test/unit/http_test.py index 2d8b2f1d..3151002c 100644 --- a/test/unit/http_test.py +++ b/test/unit/http_test.py @@ -28,6 +28,22 @@ def test_http_get_rest_hosts_works_when_fallback_realtime_host_is_not_set(): assert all(isinstance(host, str) for host in hosts) +@pytest.mark.parametrize("fallback_index", range(5)) +def test_http_get_hosts_accepts_any_realtime_fallback_host(fallback_index): + ably = create_http_client(token="foo") + hosts = ably.options.get_hosts() + fallback_host = ably.options.get_fallback_hosts()[fallback_index] + # The realtime connection may land on any fallback host, not only those REST would retry + ably.options.fallback_host = fallback_host + + rest_hosts = ably.http.get_hosts() + + assert rest_hosts[0] == fallback_host + assert rest_hosts[1] == hosts[0] + assert len(rest_hosts) == len(hosts) + assert len(set(rest_hosts)) == len(rest_hosts) + + class RecordingTransport(httpx.AsyncBaseTransport): def __init__(self, response_factory): self.requests = []