Skip to content

Commit

Permalink
Remove dead code used only in tests (aio-libs#2620)
Browse files Browse the repository at this point in the history
  • Loading branch information
socketpair committed Dec 27, 2017
1 parent 11024e1 commit c4f87ed
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 29 deletions.
22 changes: 4 additions & 18 deletions aiohttp/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
34 changes: 23 additions & 11 deletions tests/test_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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'])
Expand Down

0 comments on commit c4f87ed

Please sign in to comment.