Skip to content

bpo-37015: Ensure tasks created by _accept_connection2 due to AsyncMock are completed #13661

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

Merged
merged 3 commits into from
May 30, 2019

Conversation

tirkarthi
Copy link
Member

@tirkarthi tirkarthi commented May 29, 2019

From 3.8 async functions used with mock.patch return an AsyncMock. _accept_connection2 is an async function where create_task is also mocked. Don't mock create_task so that tasks are created out of coroutine returned by AsyncMock and the tasks are completed.

https://bugs.python.org/issue37015

@tirkarthi
Copy link
Member Author

The other way to solve this would be to remove mock.patch for create_task so that _accept_connection2 returns an AsyncMock and self.create_task creates a task from the coroutine. This would require a call like loop.run_until_complete(asyncio.sleep(0)) to ensure the task is completed I guess. I prefer this since this just restores 3.7 behavior without additional code change but I can change the approach if needed.

Command to reproduce the warnings :

./python.exe -Wall -X tracemalloc -m unittest -vv test.test_asyncio.test_selector_events.BaseSelectorEventLoopTests.test_accept_connection_multiple

mock_obj = mock.patch.object
with mock_obj(self.loop, '_accept_connection2') as accept2_mock:
with mock_obj(self.loop, '_accept_connection2',
new=mock.MagicMock()) as accept2_mock:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting! I usually use new for created objects and new_callable when I want a different mock type.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change in behavior has to be documented as noted in the tracker. Currently there is below text where MagicMock is documented as the target type but that changed in 3.8 with AsyncMock for async functions and MagicMock for normal functions when new is omitted in mock.patch.

https://docs.python.org/3/library/unittest.mock.html#patch

If new is omitted, then the target is replaced with a MagicMock

@asvetlov
Copy link
Contributor

The other way to solve this would be to remove mock.patch for create_task

Please do it. Double mocking for both _accept_connection2 and create_task looks redundant.

P.S.
Thank you very much for taking care of tests.
I'm pretty busy now and very appreciate your help with solving non-urgent but extremely important tasks,

@tirkarthi
Copy link
Member Author

You're welcome @asvetlov . I have made the requested changes to make sure only _accept_connection2 is mocked and the tasks are completed. My assumption is that create_task creates the task but doesn't execute them and using asyncio.sleep(0) there is a control switch to the created tasks without sleeping and they are completed. I have added a comment. Feel free to add suggestions if I have misunderstood something.

Thanks

@tirkarthi tirkarthi changed the title bpo-37015: Use MagicMock for _accept_connection2 which is an async function not awaited bpo-37015: Ensure tasks created by _accept_connection2 due to AsyncMock are completed May 30, 2019
Copy link
Contributor

@asvetlov asvetlov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@miss-islington miss-islington merged commit 0f39c2b into python:master May 30, 2019
DinoV pushed a commit to DinoV/cpython that referenced this pull request Jan 14, 2020
…ck are completed (pythonGH-13661)

From 3.8 async functions used with mock.patch return an `AsyncMock`. `_accept_connection2` is an async function where create_task is also mocked. Don't mock `create_task` so that tasks are created out of coroutine returned by `AsyncMock` and the tasks are completed.


https://bugs.python.org/issue37015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
skip news tests Tests in the Lib/test dir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants