From 397a8448720091c2d5f6b0de7470f2b18ebd8669 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=BE=D1=80=D0=B5=D0=BD=D0=B1=D0=B5=D1=80=D0=B3=20?= =?UTF-8?q?=D0=9C=D0=B0=D1=80=D0=BA?= Date: Wed, 27 Dec 2017 11:47:23 +0500 Subject: [PATCH] Remove dead code used only in tests (#2620) --- aiohttp/connector.py | 22 ++++------------------ tests/test_connector.py | 34 +++++++++++++++++++++++----------- 2 files changed, 27 insertions(+), 29 deletions(-) diff --git a/aiohttp/connector.py b/aiohttp/connector.py index e2dd92cc293..26005a64267 100644 --- a/aiohttp/connector.py +++ b/aiohttp/connector.py @@ -9,7 +9,6 @@ from http.cookies import SimpleCookie from itertools import cycle, islice from time import monotonic -from types import MappingProxyType from . import hdrs, helpers from .client_exceptions import (ClientConnectionError, @@ -546,40 +545,32 @@ async def _create_connection(self, req, traces=None): class _DNSCacheTable: def __init__(self, ttl=None): - self._addrs = {} self._addrs_rr = {} self._timestamps = {} self._ttl = ttl def __contains__(self, host): - return host in self._addrs - - @property - def addrs(self): - return self._addrs + return host in self._addrs_rr def add(self, host, addrs): - self._addrs[host] = addrs - self._addrs_rr[host] = cycle(addrs) + self._addrs_rr[host] = (cycle(addrs), len(addrs)) if self._ttl: self._timestamps[host] = monotonic() def remove(self, host): - self._addrs.pop(host, None) self._addrs_rr.pop(host, None) if self._ttl: self._timestamps.pop(host, None) def clear(self): - self._addrs.clear() self._addrs_rr.clear() self._timestamps.clear() def next_addrs(self, host): - loop = self._addrs_rr[host] - addrs = list(islice(loop, len(self._addrs[host]))) + loop, length = self._addrs_rr[host] + addrs = list(islice(loop, length)) # Consume one more element to shift internal state of `cycle` next(loop) return addrs @@ -705,11 +696,6 @@ def use_dns_cache(self): """True if local DNS caching is enabled.""" return self._use_dns_cache - @property - def cached_hosts(self): - """Read-only dict of cached DNS record.""" - return MappingProxyType(self._cached_hosts.addrs) - def clear_dns_cache(self, host=None, port=None): """Remove specified host/port or clear all dns local cache.""" if host is not None and port is not None: diff --git a/tests/test_connector.py b/tests/test_connector.py index 05eaf112771..1bf74f70b9b 100644 --- a/tests/test_connector.py +++ b/tests/test_connector.py @@ -1079,7 +1079,6 @@ def test_tcp_connector_ctor(loop): assert conn.use_dns_cache assert conn.family == 0 - assert conn.cached_hosts == {} def test_tcp_connector_ctor_fingerprint_valid(loop): @@ -1112,11 +1111,19 @@ def test_tcp_connector_clear_dns_cache(loop): conn._cached_hosts.add(('localhost', 123), hosts) conn._cached_hosts.add(('localhost', 124), hosts) conn.clear_dns_cache('localhost', 123) - assert ('localhost', 123) not in conn.cached_hosts + with pytest.raises(KeyError): + conn._cached_hosts.next_addrs(('localhost', 123)) + + assert conn._cached_hosts.next_addrs(('localhost', 124)) == hosts + + # Remove removed element is OK conn.clear_dns_cache('localhost', 123) - assert ('localhost', 123) not in conn.cached_hosts + with pytest.raises(KeyError): + conn._cached_hosts.next_addrs(('localhost', 123)) + conn.clear_dns_cache() - assert conn.cached_hosts == {} + with pytest.raises(KeyError): + conn._cached_hosts.next_addrs(('localhost', 124)) def test_tcp_connector_clear_dns_cache_bad_args(loop): @@ -1925,23 +1932,28 @@ class TestDNSCacheTable: def dns_cache_table(self): return _DNSCacheTable() - def test_addrs(self, dns_cache_table): + def test_next_addrs_basic(self, dns_cache_table): dns_cache_table.add('localhost', ['127.0.0.1']) dns_cache_table.add('foo', ['127.0.0.2']) - assert dns_cache_table.addrs == { - 'localhost': ['127.0.0.1'], - 'foo': ['127.0.0.2'] - } + + addrs = dns_cache_table.next_addrs('localhost') + assert addrs == ['127.0.0.1'] + addrs = dns_cache_table.next_addrs('foo') + assert addrs == ['127.0.0.2'] + with pytest.raises(KeyError): + dns_cache_table.next_addrs('no-such-host') def test_remove(self, dns_cache_table): dns_cache_table.add('localhost', ['127.0.0.1']) dns_cache_table.remove('localhost') - assert dns_cache_table.addrs == {} + with pytest.raises(KeyError): + dns_cache_table.next_addrs('localhost') def test_clear(self, dns_cache_table): dns_cache_table.add('localhost', ['127.0.0.1']) dns_cache_table.clear() - assert dns_cache_table.addrs == {} + with pytest.raises(KeyError): + dns_cache_table.next_addrs('localhost') def test_not_expired_ttl_None(self, dns_cache_table): dns_cache_table.add('localhost', ['127.0.0.1'])