Skip to content

Commit

Permalink
Improve resolver tests
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov committed Aug 1, 2016
1 parent 1bfc02a commit 83ca6f8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 60 deletions.
2 changes: 1 addition & 1 deletion aiohttp/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '0.22.4'
__version__ = '1.0.0a0'

# Deprecated, keep it here for a while for backward compatibility.
import multidict # noqa
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ pyenchant
sphinxcontrib-spelling
sphinxcontrib-asyncio
isort
aiodns
97 changes: 38 additions & 59 deletions tests/test_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,80 +37,59 @@ def fake(*args, **kwargs):


@pytest.mark.skipif(aiodns is None, reason="aiodns required")
@pytest.mark.run_loop
def test_async_resolver_positive_lookup(loop):
@asyncio.coroutine
def go():
with patch('aiodns.DNSResolver.query') as mock_query:
mock_query.return_value = fake_result(['127.0.0.1'])
resolver = AsyncResolver(loop=loop)
real = yield from resolver.resolve('www.python.org')
ipaddress.ip_address(real[0]['host'])
loop.run_until_complete(go())
with patch('aiodns.DNSResolver.query') as mock_query:
mock_query.return_value = fake_result(['127.0.0.1'])
resolver = AsyncResolver(loop=loop)
real = yield from resolver.resolve('www.python.org')
ipaddress.ip_address(real[0]['host'])


@pytest.mark.skipif(aiodns is None, reason="aiodns required")
@pytest.mark.run_loop
def test_async_resolver_multiple_replies(loop):
@asyncio.coroutine
def go():
with patch('aiodns.DNSResolver.query') as mock_query:
ips = ['127.0.0.1', '127.0.0.2', '127.0.0.3', '127.0.0.4']
mock_query.return_value = fake_result(ips)
resolver = AsyncResolver(loop=loop)
real = yield from resolver.resolve('www.google.com')
ips = [ipaddress.ip_address(x['host']) for x in real]
assert len(ips) > 3, "Expecting multiple addresses"
loop.run_until_complete(go())
with patch('aiodns.DNSResolver.query') as mock_query:
ips = ['127.0.0.1', '127.0.0.2', '127.0.0.3', '127.0.0.4']
mock_query.return_value = fake_result(ips)
resolver = AsyncResolver(loop=loop)
real = yield from resolver.resolve('www.google.com')
ips = [ipaddress.ip_address(x['host']) for x in real]
assert len(ips) > 3, "Expecting multiple addresses"


@pytest.mark.skipif(aiodns is None, reason="aiodns required")
@pytest.mark.run_loop
def test_async_negative_lookup(loop):
@asyncio.coroutine
def go():
with patch('aiodns.DNSResolver.query') as mock_query:
mock_query.side_effect = aiodns.error.DNSError()
resolver = AsyncResolver(loop=loop)
try:
yield from resolver.resolve('doesnotexist.bla')
assert False, "Expecting aiodns.error.DNSError"
except aiodns.error.DNSError:
pass

loop.run_until_complete(go())
with patch('aiodns.DNSResolver.query') as mock_query:
mock_query.side_effect = aiodns.error.DNSError()
resolver = AsyncResolver(loop=loop)
with pytest.raises(aiodns.error.DNSError):
yield from resolver.resolve('doesnotexist.bla')


@pytest.mark.run_loop
def test_default_resolver_positive_lookup(loop):
@asyncio.coroutine
def go():
loop.getaddrinfo = fake_addrinfo(["127.0.0.1"])
resolver = DefaultResolver(loop=loop)
real = yield from resolver.resolve('www.python.org')
ipaddress.ip_address(real[0]['host'])

loop.run_until_complete(go())
loop.getaddrinfo = fake_addrinfo(["127.0.0.1"])
resolver = DefaultResolver(loop=loop)
real = yield from resolver.resolve('www.python.org')
ipaddress.ip_address(real[0]['host'])


@pytest.mark.run_loop
def test_default_resolver_multiple_replies(loop):
@asyncio.coroutine
def go():
ips = ['127.0.0.1', '127.0.0.2', '127.0.0.3', '127.0.0.4']
loop.getaddrinfo = fake_addrinfo(ips)
resolver = DefaultResolver(loop=loop)
real = yield from resolver.resolve('www.google.com')
ips = [ipaddress.ip_address(x['host']) for x in real]
assert len(ips) > 3, "Expecting multiple addresses"
loop.run_until_complete(go())
ips = ['127.0.0.1', '127.0.0.2', '127.0.0.3', '127.0.0.4']
loop.getaddrinfo = fake_addrinfo(ips)
resolver = DefaultResolver(loop=loop)
real = yield from resolver.resolve('www.google.com')
ips = [ipaddress.ip_address(x['host']) for x in real]
assert len(ips) > 3, "Expecting multiple addresses"


@pytest.mark.run_loop
def test_default_negative_lookup(loop):
@asyncio.coroutine
def go():
ips = []
loop.getaddrinfo = fake_addrinfo(ips)
resolver = DefaultResolver(loop=loop)
try:
yield from resolver.resolve('doesnotexist.bla')
assert False, "Expecting socket.gaierror"
except socket.gaierror:
pass

loop.run_until_complete(go())
ips = []
loop.getaddrinfo = fake_addrinfo(ips)
resolver = DefaultResolver(loop=loop)
with pytest.raises(socket.gaierror):
yield from resolver.resolve('doesnotexist.bla')

0 comments on commit 83ca6f8

Please sign in to comment.