Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cover ttl=0 (expire always) _DNSCacheTable case #7014

Merged
merged 10 commits into from
Oct 22, 2022
1 change: 1 addition & 0 deletions CHANGES/7014.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed bug when using ``TCPConnector`` with ``ttl_dns_cache=0``.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ Illia Volochii
Ilya Chichak
Ilya Gruzinov
Ingmar Steen
Ivan Lakovic
Ivan Larin
Jacob Champion
Jaesung Lee
Expand Down
4 changes: 2 additions & 2 deletions aiohttp/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,13 +678,13 @@ def __contains__(self, host: object) -> bool:
def add(self, key: Tuple[str, int], addrs: List[Dict[str, Any]]) -> None:
self._addrs_rr[key] = (cycle(addrs), len(addrs))

if self._ttl:
if self._ttl is not None:
self._timestamps[key] = monotonic()

def remove(self, key: Tuple[str, int]) -> None:
self._addrs_rr.pop(key, None)

if self._ttl:
if self._ttl is not None:
self._timestamps.pop(key, None)

def clear(self) -> None:
Expand Down
23 changes: 20 additions & 3 deletions tests/test_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -2179,10 +2179,27 @@ def test_not_expired_ttl(self) -> None:
dns_cache_table.add("localhost", ["127.0.0.1"])
assert not dns_cache_table.expired("localhost")

async def test_expired_ttl(self, loop: Any) -> None:
dns_cache_table = _DNSCacheTable(ttl=0.01)
def test_expired_ttl(self, monkeypatch: pytest.MonkeyPatch) -> None:
dns_cache_table = _DNSCacheTable(ttl=1)
monkeypatch.setattr("aiohttp.connector.monotonic", lambda: 1)
dns_cache_table.add("localhost", ["127.0.0.1"])
await asyncio.sleep(0.02)
monkeypatch.setattr("aiohttp.connector.monotonic", lambda: 2)
assert not dns_cache_table.expired("localhost")
monkeypatch.setattr("aiohttp.connector.monotonic", lambda: 3)
assert dns_cache_table.expired("localhost")

def test_never_expire(self, monkeypatch: pytest.MonkeyPatch) -> None:
dns_cache_table = _DNSCacheTable(ttl=None)
monkeypatch.setattr("aiohttp.connector.monotonic", lambda: 1)
dns_cache_table.add("localhost", ["127.0.0.1"])
monkeypatch.setattr("aiohttp.connector.monotonic", lambda: 10000000)
assert not dns_cache_table.expired("localhost")

def test_always_expire(self, monkeypatch: pytest.MonkeyPatch) -> None:
dns_cache_table = _DNSCacheTable(ttl=0)
monkeypatch.setattr("aiohttp.connector.monotonic", lambda: 1)
dns_cache_table.add("localhost", ["127.0.0.1"])
monkeypatch.setattr("aiohttp.connector.monotonic", lambda: 1.00001)
assert dns_cache_table.expired("localhost")

def test_next_addrs(self, dns_cache_table: Any) -> None:
Expand Down