Skip to content

During tests: the on_startup signal is sent before the loop is set  #2060

Closed
@cecton

Description

Long story short

Between the version 2.1 and 2.2 we change the order of when the signal on_startup is sent during the tests and when we make the handlers. Because of that, all the on_startup hooks doesn't have a loop in app.loop.

ed28add

commit ed28add756e2a5dc8caf082641ac1d1810a18c7a
Author: Andrew Svetlov <andrew.svetlov@gmail.com>
Date:   Thu Jun 15 00:51:40 2017 +0300

    Fix #1947: don't raise DeprecationWarning inside on_startup() handler for test_utils

diff --git a/CHANGES.rst b/CHANGES.rst
index 82e545b9..d1640a92 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -18,7 +18,8 @@ Changes
 
 - Use CIMultiDict for ClientRequest.skip_auto_headers #1970
 
--
+- Fix wrong startup sequence: test server doesn't raise
+  `DeprecationWarning` now #1947
 
 -
 
diff --git a/aiohttp/test_utils.py b/aiohttp/test_utils.py
index 4791cde8..21f89362 100644
--- a/aiohttp/test_utils.py
+++ b/aiohttp/test_utils.py
@@ -148,8 +148,8 @@ class TestServer(BaseTestServer):
 
     @asyncio.coroutine
     def _make_factory(self, **kwargs):
-        self.handler = self.app.make_handler(loop=self._loop, **kwargs)
         yield from self.app.startup()
+        self.handler = self.app.make_handler(loop=self._loop, **kwargs)
         return self.handler
 
     @asyncio.coroutine

Expected behaviour

Keep the behavior consistent when running the app within or without a test.

Actual behaviour

app.loop is None during a test but it is an event loop when running with web.run_app.

Steps to reproduce

import aiohttp
from aiohttp import web
from aiohttp.test_utils import AioHTTPTestCase, unittest_run_loop
from distutils.version import StrictVersion


async def hook(app):
    assert StrictVersion(aiohttp.__version__) >= StrictVersion("2.2")
    assert app.loop is not None # fails in the test case but not in run_app


class TestCase(AioHTTPTestCase):
    async def get_application(self):
        app = web.Application()
        app.on_startup.append(hook)
        return app

    @unittest_run_loop
    async def test_on_startup_hook(self):
        pass # will fail before executing the test


if __name__ == '__main__':
    app = web.Application()
    app.on_startup.append(hook)
    web.run_app(app)

Your environment

ArchLinux, Python 3.6

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions