Teardown of the event_loop fixture no longer replaces the event loop policy #192
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
My understanding is that everyone is happy, if the event loop policy remains unaltered. I think I found a way to get rid of the code that resets the event loop policy.
Problem
Here is my understanding of the problem:
We need to make sure that each test case runs in an isolated fashion. Therefore, the
event_loop
fixture provides a fresh event loop and is automatically applied to all marked tests. When the fixture goes out of scope, it closes the event loop. Subsequent test cases would run into the following error:This is prevented a dedicated fixture teardown prodecure. The teardown sets the event loop policy to
None
, which also disposes the existing, closed event loop. Subsequent calls toasyncio.get_event_loop()
will return a fresh loop. The problem is that setting the event loop policy to None resets the policy to the system default, which may be undesired.Solution
Instead of resetting the event loop policy, the same effect can be achieved by requesting a new loop to replace the existing, closed event loop:
Considerations
What I dislike about this solution is that a new loop is created during the teardown of the
event_loop
fixture. It feels wrong to do it here. The correct place would be to create a new loop during fixture setup. In fact, the fixture does create another loop so we effectively create two new event loops for each test run. This points to an issue with the order in which the fixtures are evaluated.What are your thoughts on this approach?
Resolves #168
Resolves #188