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

Return original event loop after test case #7

Closed
jonatlib opened this issue Mar 29, 2016 · 4 comments
Closed

Return original event loop after test case #7

jonatlib opened this issue Mar 29, 2016 · 4 comments
Assignees

Comments

@jonatlib
Copy link

Description

After running any test case which is using async test, the loop will end up closed. Which is because:

  1. When asynctest.TestCase initializing new test it creates new loop in _init_loop method.
  2. Then replace current loop with this new created
  3. After test it closes TestCase's loop, which is now the current loop - and never return original one.

Then this closed loop end up as current loop.

Issue

The problem is that, after running any async test case, the asyncio module end up in unusable and different state than which it was before running tests.

Solution

  1. In _init_loop store reference to original event loop, and then proceed same way.
  2. Closing of test's loop is done inside cleanup - so add another cleanup after the one which close the loop. This new cleanup will restore original loop.
    • Or just override _unset_loop to set original loop back
@Martiusweb Martiusweb self-assigned this Mar 29, 2016
@Martiusweb
Copy link
Owner

Thanks for reporting your issue.

Can you explain me why you want or need the loop outside the test case? It seems like a legitimate request, but I want to be sure it's implemented correctly, because I don't want to introduce confusion about the state of the default loop in a test function.

I am considering doing the following:

  • add an option allowing to set the default loop to None during the test, ensuring that the loop object is passed explicitly and asyncio.get_event_loop() isn't used in the tested code.
  • keeping track of the default loop the loop obtained by asyncio.get_event_loop() before replacing it, and restoring it after the test (as proposed in this issue). An other solution would be to mock asyncio.get_event_loop().

In any case, asyncio.get_event_loop() must never return a loop object which is not the same as self.loop.

@jonatlib
Copy link
Author

Thank you for a quick reply.

I don't explicitly need the loop outside of the test case. But i have couple of old test cases which are using unittest.TestCase. These tests creating some objects instances which are now using some part of asyncio module - but these tests do not need asyncio.

Then i start using asynctest, which led to broken unittest test cases. Because i have run all test cases, and if asynctest case was executed before unittest case, the loop was already closed.

Correct way would be mocking out asyncio parts from these objects, and i will do so.
But also as i mentioned before running a test case should not lead to change of a state in the asyncio module (the current event loop state).

@Martiusweb
Copy link
Owner

Yes, you are right about the state change. I will try to push a fix today.

@Martiusweb
Copy link
Owner

I close the bug, I will put a release on pypi soon.

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

2 participants