Pytest tests: "There is no current event loop in thread" error

2,906 views
Skip to first unread message

manuel miranda

unread,
Aug 2, 2016, 7:14:46 AM8/2/16
to aio-libs
Hi!

I'm trying to write some tests that interact with an API I've built with aiohttp and it's giving the error in the title:

2016-08-02 13:05:58,060 ERROR    aiohttp.web:212 | Error handling request
Traceback (most recent call last):
  File "/Users/manuelmiranda/.virtualenvs/bb/lib/python3.5/site-packages/aiohttp/server.py", line 285, in start
    yield from self.handle_request(message, payload)
  File "/Users/manuelmiranda/.virtualenvs/bb/lib/python3.5/site-packages/aiohttp/web.py", line 90, in handle_request
    resp = yield from handler(request)
  File "/Users/manuelmiranda/Projects/bellboy/bellboy/middlewares.py", line 19, in middleware_handler
    return await handler(request)
  File "/Users/manuelmiranda/Projects/bellboy/bellboy/search/views.py", line 20, in __init__
    self.search_service = SearchService()
  File "/Users/manuelmiranda/Projects/bellboy/bellboy/search/services.py", line 25, in __init__
    self.price_service = PriceService()
  File "/Users/manuelmiranda/.virtualenvs/bb/lib/python3.5/site-packages/di/main.py", line 164, in inner
    return fn(*args, **kwargs)
  File "/Users/manuelmiranda/Projects/bellboy/bellboy/price/services.py", line 13, in __init__
    self.filtering_sorting_service = FilteringSortingService()
  File "/Users/manuelmiranda/.virtualenvs/bb/lib/python3.5/site-packages/di/main.py", line 159, in inner
    kwargs[name] = deps[dependency]
  File "/Users/manuelmiranda/.virtualenvs/bb/lib/python3.5/site-packages/di/main.py", line 289, in __getitem__
    raise e
  File "/Users/manuelmiranda/.virtualenvs/bb/lib/python3.5/site-packages/di/main.py", line 274, in __getitem__
    self._singletons[key] = value(self)
  File "/Users/manuelmiranda/Projects/bellboy/bellboy/bootstrap.py", line 51, in dep_elasticsearch
    return service_factory('ELASTICSEARCH', cfg)
  File "/Users/manuelmiranda/Projects/bellboy/bellboy/bootstrap.py", line 21, in service_factory
    return cls(config[section_name])
  File "/Users/manuelmiranda/Projects/bellboy/bellboy/filteringsorting/storage/elasticsearch.py", line 24, in __init__
    self._es = ElasticSearchClient([config['endpoint']])
  File "/Users/manuelmiranda/.virtualenvs/bb/lib/python3.5/site-packages/aioes/client/__init__.py", line 18, in __init__
    self._transport = Transport(endpoints, loop=loop, **kwargs)
  File "/Users/manuelmiranda/.virtualenvs/bb/lib/python3.5/site-packages/aioes/transport.py", line 36, in __init__
    self._pool = ConnectionPool([], loop=loop)
  File "/Users/manuelmiranda/.virtualenvs/bb/lib/python3.5/site-packages/aioes/pool.py", line 41, in __init__
    self._dead = asyncio.PriorityQueue(len(connections), loop=loop)
  File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncio/queues.py", line 43, in __init__
    self._loop = events.get_event_loop()
  File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncio/events.py", line 626, in get_event_loop
    return get_event_loop_policy().get_event_loop()
  File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/asyncio/events.py", line 572, in get_event_loop
    % threading.current_thread().name)
RuntimeError: There is no current event loop in thread 'MainThread'.

I've followed the examples in http://aiohttp.readthedocs.io/en/stable/testing.html#pytest-example but not working for me. This is what I currently have:



pytest_plugins = 'aiohttp.pytest_plugin'

# The build app function for executing from terminal
def build_app(*args, loop=None):
    app = web.Application(loop=loop, middlewares=[tracebacks_middleware])
    app.router.add_route('GET', '/my_page', SearchView)
    return app

# Fixtures in conftest
@pytest.fixture
def create_app(loop):
    return build_app(loop=loop)


@pytest.fixture
def cli(loop, test_client):
    return loop.run_until_complete(test_client(create_app))

# And the test
async def test_first_get(cli):
    resp = await cli.get('/my_page')
    assert False



And when executing the test is giving the traceback above. Am I missing anything? I need some help there coz I've been really struggling with that for a while... =/

Thanks!

Manu


Andrew Svetlov

unread,
Aug 2, 2016, 7:37:51 AM8/2/16
to aio-libs
Pass event loop instance everywhere, into aioes client too :/

manuel miranda

unread,
Aug 2, 2016, 8:31:01 AM8/2/16
to aio-libs
Ahhh I see... =/
Reply all
Reply to author
Forward
0 new messages