Add pytest fixture to clean up the IO loop

Open
#471 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
38/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
python
Domain
testing-qa

Research direction

Start by locating the streamz unit tests and the existing pristine_loop fixture. Compare that fixture with the proposed loop_cleaner cleanup, then run the affected tests to reproduce the running-event-loop error. Done means the tests leave no running or open global asyncio event loop and pass reliably with the jupyter_core change described.

Written by the indexing model from the issue text.

Description

I recently came across this error in my unit tests:

RuntimeError: This event loop is already running

The error doesn't come from streamz, it's from jupyter_core, in this recent change.

But it is triggered because my streamz tests don't clean up properly after them selves.
They leave the global async event loop, asyncio.get_event_loop(), in a running state.

To fix it I had to add this fixture to my unit tests:

@pytest.fixture(autouse=True)
def loop_cleaner():
    yield None
    close_io_loop()


def close_io_loop():

    IOLoop().current().stop()
    IOLoop().current().close()
    assert not asyncio.get_event_loop().is_running()
    asyncio.get_event_loop().close()
    assert asyncio.get_event_loop().is_closed()
    asyncio.set_event_loop(None)

It's a bit similar to the existing pristine_loop, but doesn't exactly do the same thing (afaict).

I hope this can help any one having similar issues.

Dominant language
Python
Stars
1.3k
Forks
149
Avg merge
17h 39m
Merged PRs (30d)
1

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from python-streamz/streamz

All issues in python-streamz/streamz

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.