|
| 1 | +import httpx |
| 2 | +import pytest |
| 3 | + |
| 4 | +from ably import AblyRest |
| 5 | +from ably.types.testoptions import TestOptions |
| 6 | +from ably.util.clock import Clock |
| 7 | +from ably.util.exceptions import AblyException |
| 8 | + |
| 9 | +# A plausible wall-clock reading for a fake to start from, so that a time taken |
| 10 | +# against it looks like a real one. |
| 11 | +EPOCH_MS = 1_700_000_000_000 |
| 12 | + |
| 13 | + |
| 14 | +class FakeClock: |
| 15 | + """A clock whose notional time only the test moves. |
| 16 | +
|
| 17 | + `advance` moves it by hand; `step_ms` moves it by that much on every read, |
| 18 | + which is how a duration measured across several reads is driven. |
| 19 | + """ |
| 20 | + |
| 21 | + def __init__(self, now_ms=EPOCH_MS, step_ms=0): |
| 22 | + self.__now_ms = now_ms |
| 23 | + self.__step_ms = step_ms |
| 24 | + |
| 25 | + def now_ms(self): |
| 26 | + reading = self.__now_ms |
| 27 | + self.__now_ms += self.__step_ms |
| 28 | + return reading |
| 29 | + |
| 30 | + def monotonic_ms(self): |
| 31 | + return float(self.__now_ms) |
| 32 | + |
| 33 | + def advance(self, milliseconds): |
| 34 | + self.__now_ms += milliseconds |
| 35 | + |
| 36 | + def timer(self, timeout, callback): |
| 37 | + raise AssertionError('the REST client schedules no delayed callbacks') |
| 38 | + |
| 39 | + |
| 40 | +class RecordingTransport(httpx.AsyncBaseTransport): |
| 41 | + """Records the host of every request and refuses the ones in `refusing`. |
| 42 | +
|
| 43 | + A host left out of `refusing` is served a server time. `refusing` is set |
| 44 | + after the client is built, since the host to refuse is one of its own. |
| 45 | + """ |
| 46 | + |
| 47 | + def __init__(self): |
| 48 | + self.refusing = () |
| 49 | + self.hosts = [] |
| 50 | + |
| 51 | + async def handle_async_request(self, request): |
| 52 | + self.hosts.append(request.url.host) |
| 53 | + if request.url.host in self.refusing: |
| 54 | + raise httpx.ConnectError('connection refused', request=request) |
| 55 | + return httpx.Response(200, json=[1500000000000]) |
| 56 | + |
| 57 | + |
| 58 | +def test_auth_timestamps_from_the_injected_clock(): |
| 59 | + ably = AblyRest(key='name:secret', test_options=TestOptions(clock=FakeClock(1_500_000_000_000))) |
| 60 | + assert ably.auth._timestamp() == 1_500_000_000_000 |
| 61 | + |
| 62 | + |
| 63 | +def test_a_client_without_test_options_reads_the_real_clock(): |
| 64 | + ably = AblyRest(token='foo') |
| 65 | + assert isinstance(ably.auth._Auth__clock, Clock) |
| 66 | + assert isinstance(ably.http._Http__clock, Clock) |
| 67 | + |
| 68 | + |
| 69 | +async def test_the_cached_fallback_host_expires_on_the_clock(): |
| 70 | + # RSC15f: the cache lasts fallback_retry_timeout milliseconds |
| 71 | + clock = FakeClock() |
| 72 | + transport = RecordingTransport() |
| 73 | + ably = AblyRest(token='foo', fallback_retry_timeout=2000, |
| 74 | + test_options=TestOptions(http_transport=transport, clock=clock)) |
| 75 | + primary = ably.options.get_host() |
| 76 | + transport.refusing = (primary,) |
| 77 | + |
| 78 | + await ably.time() |
| 79 | + |
| 80 | + fallback = transport.hosts[-1] |
| 81 | + assert fallback != primary |
| 82 | + assert ably.http._Http__host_expires == EPOCH_MS + 2000 |
| 83 | + assert ably.http.get_hosts()[0] == fallback |
| 84 | + |
| 85 | + clock.advance(1999) |
| 86 | + assert ably.http.get_hosts()[0] == fallback |
| 87 | + |
| 88 | + clock.advance(2) |
| 89 | + assert ably.http.get_hosts()[0] == primary |
| 90 | + assert ably.http._Http__host_expires is None |
| 91 | + |
| 92 | + await ably.close() |
| 93 | + |
| 94 | + |
| 95 | +async def test_every_host_is_tried_while_the_retry_budget_lasts(): |
| 96 | + # A second of clock time per reading, against the default 15 second budget |
| 97 | + clock = FakeClock(step_ms=1000) |
| 98 | + transport = RecordingTransport() |
| 99 | + ably = AblyRest(token='foo', test_options=TestOptions(http_transport=transport, clock=clock)) |
| 100 | + hosts = ably.http.get_hosts() |
| 101 | + transport.refusing = tuple(hosts) |
| 102 | + |
| 103 | + with pytest.raises(AblyException): |
| 104 | + await ably.time() |
| 105 | + |
| 106 | + assert transport.hosts == hosts |
| 107 | + |
| 108 | + await ably.close() |
| 109 | + |
| 110 | + |
| 111 | +async def test_retrying_stops_once_the_retry_budget_is_spent(): |
| 112 | + # RSC15l: http_max_retry_duration is seconds where the clock is milliseconds |
| 113 | + clock = FakeClock(step_ms=1000) |
| 114 | + transport = RecordingTransport() |
| 115 | + ably = AblyRest(token='foo', http_max_retry_duration=0.5, |
| 116 | + test_options=TestOptions(http_transport=transport, clock=clock)) |
| 117 | + transport.refusing = tuple(ably.http.get_hosts()) |
| 118 | + |
| 119 | + with pytest.raises(AblyException): |
| 120 | + await ably.time() |
| 121 | + |
| 122 | + assert len(transport.hosts) == 1 |
| 123 | + |
| 124 | + await ably.close() |
0 commit comments