Skip to content

Commit

Permalink
Switch unit tests from aiohttp to tornado
Browse files Browse the repository at this point in the history
aiohttp has a bug in GET requests for static content
(aio-libs/aiohttp#4809).
  • Loading branch information
bmerry committed Jun 10, 2020
1 parent 022dbc8 commit 11ed28c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 25 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ where = src
test =
pytest
responses
aiohttp
tornado

fetch =
requests
Expand Down
6 changes: 6 additions & 0 deletions src/katsdpmodels/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ def readinto(self, b) -> int:
raise OSError('Did not receive expected byte range')
bytes_read = resp.raw.readinto(b)
self._offset += bytes_read
with open('readinto.log', 'a') as f:
print(f'headers: {resp.headers}', file=f)
print(f'range: {start}-{end}', file=f)
expected = end - start + 1
print(f'received: {bytes_read} / {expected}', file=f)
print(file=f)
return bytes_read

def close(self) -> None:
Expand Down
12 changes: 3 additions & 9 deletions test-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
aiohttp
async-timeout # via aiohttp
attrs # via pytest, aiohttp
chardet # via aiohttp
attrs # via pytest
coverage
idna-ssl # via aiohttp
importlib-metadata # via pytest
more-itertools # via pytest
multidict # via aiohttp
packaging # via pytest
pluggy # via pytest
py # via pytest
pytest
pytest-cov
responses==0.10.14
typing-extensions # via aiohttp
tornado
wcwidth # via pytest
yarl # via aiohttp
zipp # via importlib
zipp # via importlib-metadata
33 changes: 18 additions & 15 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@

import pytest
import responses
import aiohttp.web
import tornado.httpserver
import tornado.netutil
import tornado.web

import test_utils

Expand Down Expand Up @@ -58,20 +60,21 @@ def web_server() -> Generator[Callable[[str], str], None, None]:
"""

async def server_coro(info_future: 'concurrent.futures.Future[_Info]') -> None:
data_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data')
app = aiohttp.web.Application()
app.add_routes([aiohttp.web.static('/data', data_dir)])
runner = aiohttp.web.AppRunner(app)
await runner.setup()
sock = socket.socket()
sock.bind(('127.0.0.1', 0)) # Allocate an available port
site = aiohttp.web.SockSite(runner, sock)
await site.start()
finished_event = asyncio.Event()
data_url = urllib.parse.urljoin(site.name, '/data/')
info_future.set_result((asyncio.get_event_loop(), finished_event, data_url))
await finished_event.wait()
await runner.cleanup()
try:
data_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data')
app = tornado.web.Application([], static_path=data_dir)
sockets = tornado.netutil.bind_sockets(0, '127.0.0.1')
port = sockets[0].getsockname()[1]
server = tornado.httpserver.HTTPServer(app)
server.add_sockets(sockets)
server.start()
finished_event = asyncio.Event()
data_url = f'http://127.0.0.1:{port}/static/'
info_future.set_result((asyncio.get_event_loop(), finished_event, data_url))
await finished_event.wait()
server.stop()
except Exception as exc:
info_future.set_exception(exc)

def server_thread(info_future: 'concurrent.futures.Future[_Info]') -> None:
loop = asyncio.new_event_loop()
Expand Down

0 comments on commit 11ed28c

Please sign in to comment.