Skip to content

plugin doesn't work together with pytest-randomly #202

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

Closed
skarzi opened this issue Jan 28, 2021 · 2 comments
Closed

plugin doesn't work together with pytest-randomly #202

skarzi opened this issue Jan 28, 2021 · 2 comments

Comments

@skarzi
Copy link

skarzi commented Jan 28, 2021

Hi 👋

first of all big thanks for this awesome plugin!

Recently I was writing tests for django-channels async consumers and middlewares. For writing these tests I am using also pytest-randomly and django-channel InMemoryChannelLayer.
When tests are running without pytest-randomly (with flag -p no:randomly) tests are passing, however with pytest-randomly plugin on I get exceptions like:

RuntimeError: Task <Task pending name='Task-49' coro=<InMemoryChannelLayer.receive() running at /usr/local/lib/python3.8/site-packages/channels/layers.py:250> cb=[_wait.<locals>._on_completion() at /usr/local/lib/python3.8/asyncio/tasks.py:515] created at /usr/local/lib/python3.8/site-packages/channels/utils.py:40> got Future <Future pending created at /usr/local/lib/python3.8/asyncio/base_events.py:422> attached to a different loop

When I add event_loop session-scoped fixture like suggested in #38 (comment) everything works like a charm even with pytest-randomly.

Do you have any idea why this is happening?

@Tinche
Copy link
Member

Tinche commented Jan 28, 2021

It's probably interfering with the way we generate the event loop and initialize async fixtures. Feel free to poke around yourself and see if there's an easy fix :)

I would start by adding print statements to the existing request-scoped event loop fixture

@skarzi
Copy link
Author

skarzi commented Jan 29, 2021

I managed to find the root of the issue, and it's not related to the pytest-asyncio ;)
Basically, InMemoryChannelLayer is using random.choice() to generate new channels names, pytest-randomly is controlling random seed for each test, so it's common to get identical channel name for many tests and finally, channels aren't automatically flushed at the end of each test and this means that channels' Queues are reused, but they were created/bound to the previous event loop, so RuntimeError is raised.

@skarzi skarzi closed this as completed Jan 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants