Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use with pytest #13

Closed
aherok opened this issue Mar 23, 2020 · 2 comments
Closed

Cannot use with pytest #13

aherok opened this issue Mar 23, 2020 · 2 comments

Comments

@aherok
Copy link

aherok commented Mar 23, 2020

Hello!
I've used the package although I cannot use it my pytest scenarios due to the infamous got Future <Future pending> attached to a different loop issue.

Consider the following test code:

import pytest
from aiodataloader import DataLoader


async def load_data(ids):
    return map(lambda x: x + 5, ids)


loader = DataLoader(batch_load_fn=load_data)


@pytest.mark.asyncio
async def test_loader_returns_data(event_loop):
    data = await loader.load(1)
    assert data == 6

When run, I get an error:

RuntimeError: Task <Task pending coro=<test_user_loader_succeedes() running at /app/loaders/tests/test_loader.py:14> cb=[_run_until_complete_cb() at /usr/local/lib/python3.7/asyncio/base_events.py:158]> got Future <Future pending> attached to a different loop

Is there an easy way to fix it?

@romikforest
Copy link

romikforest commented Mar 9, 2021

The loader should be created after the asyncio loop is started. But I still have Task was destroyed but it is pending! with aiodataloader under the heavy load.

@septatrix
Copy link

septatrix commented Sep 10, 2022

You can simply create the loader as a fixture:

import pytest
from aiodataloader import DataLoader

async def load_data(ids):
    return map(lambda x: x + 5, ids)

@pytest.fixture
def loader():
    return DataLoader(batch_load_fn=load_data)

@pytest.mark.asyncio
async def test_loader_returns_data(event_loop, loader):
    data = await loader.load(1)
    assert data == 6

This also ensures that tests do not influence each other

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants