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

I suggest to add "self.loop.run_until_complete(asyncio.sleep(0.1))" in AioHTTPTestCase.tearDown #3388

Closed
harobed opened this issue Nov 11, 2018 · 8 comments
Labels
needs-info Issue is lacking sufficient information and will be closed if not provided Stale

Comments

@harobed
Copy link

harobed commented Nov 11, 2018

In my test file, I need to add self.loop.run_until_complete(asyncio.sleep(0.1)) in tearDown to remove ResourceWarning: unclosed transport, see #1115

import unittest
import asyncio
from aiohttp.test_utils import AioHTTPTestCase, unittest_run_loop
import aresponses

from app import App


class Test(AioHTTPTestCase):
    async def get_application(self):
        return App()

    def tearDown(self):
        self.loop.run_until_complete(
            asyncio.sleep(0.1)
        )  # This hack fix this bug https://github.com/aio-libs/aiohttp/issues/1115
        super().tearDown()

    @unittest_run_loop
    async def test_b(self):
        async with aresponses.ResponsesMockServer(loop=self.loop) as arsps:
            arsps.add(arsps.ANY, "/", "get", arsps.Response(text="hey!"))
            resp = await self.client.request("GET", "/")
            self.assertEqual(resp.status, 200)
            self.assertEqual(await resp.text(), "hey!")


if __name__ == "__main__":
    unittest.main()

I suggest to add self.loop.run_until_complete(asyncio.sleep(0.1)) in https://github.com/aio-libs/aiohttp/blob/master/aiohttp/test_utils.py#L432, example:

    def tearDown(self) -> None:
        self.loop.run_until_complete(asyncio.sleep(0.1))
        self.loop.run_until_complete(self.tearDownAsync())
        self.loop.run_until_complete(self.client.close())
        teardown_test_loop(self.loop)

What do you think of that?

Best regards,
Stéphane

@aio-libs-bot
Copy link

GitMate.io thinks the contributor most likely able to help you is @asvetlov.

Possibly related issues are #2706 (asyncio.sleep() raise RuntimeError exception in AioHTTPTestCase.), #2248 (Test only asyncio loop by default.), #1007 (Next aiohttp release will be 1.0), #526 (HTTP/1.0 Keep-Alive behaviour), and #1922 (aiohttp 2.1.0 release).

@webknjaz
Copy link
Member

Not 0.1, just 0

@kxepal
Copy link
Member

kxepal commented Nov 11, 2018

This issue doesn't happens for me with a given test script and without custom tearDown, while it is reproduceable with script from #1115. Some flaky case?

@harobed
Copy link
Author

harobed commented Nov 11, 2018

Not 0.1, just 0

0 don't remove warning message on my host.

@harobed
Copy link
Author

harobed commented Nov 11, 2018

No flaky case on my macOS laptop with Python 3.7.0.

@harobed
Copy link
Author

harobed commented Nov 11, 2018

Same warning message with Pytho 3.7.1:

/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/selector_events.py:666: ResourceWarning: unclosed transport <_SelectorSocketTransport fd=11>
  source=self)

@kxepal
Copy link
Member

kxepal commented Nov 12, 2018

No ResourceWarnings for me on Gentoo Linux and FreeBSD. That's why I'm asking.

@Dreamsorcerer
Copy link
Member

Is this still an issue? I also can't reproduce (and the original reproducer is incomplete).

@Dreamsorcerer Dreamsorcerer added the needs-info Issue is lacking sufficient information and will be closed if not provided label Aug 9, 2024
@github-actions github-actions bot added the Stale label Sep 9, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Sep 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-info Issue is lacking sufficient information and will be closed if not provided Stale
Projects
None yet
Development

No branches or pull requests

5 participants