From 0f0b392501e5b678ba0d613880044b05bc013c44 Mon Sep 17 00:00:00 2001 From: tumb1er Date: Mon, 25 Jul 2016 12:00:13 +0300 Subject: [PATCH] add test for ssl init --- aiohttp/worker.py | 9 ++++----- tests/test_worker.py | 17 +++++++++++++---- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/aiohttp/worker.py b/aiohttp/worker.py index 64dacbebf8c..78505dfc34f 100644 --- a/aiohttp/worker.py +++ b/aiohttp/worker.py @@ -17,10 +17,6 @@ class GunicornWebWorker(base.Worker): def __init__(self, *args, **kw): # pragma: no cover super().__init__(*args, **kw) - if self.cfg.is_ssl: - self.ssl_context = self._create_ssl_context(self.cfg) - else: - self.ssl_context = None self.servers = {} self.exit_code = 0 @@ -85,10 +81,13 @@ def close(self): @asyncio.coroutine def _run(self): + + ctx = self._create_ssl_context(self.cfg) if self.cfg.is_ssl else None + for sock in self.sockets: handler = self.make_handler(self.wsgi) srv = yield from self.loop.create_server(handler, sock=sock.sock, - ssl=self.ssl_context) + ssl=ctx) self.servers[srv] = handler # If our parent changed then we shut down. diff --git a/tests/test_worker.py b/tests/test_worker.py index 15abe1d9f9e..889c3cabbe4 100644 --- a/tests/test_worker.py +++ b/tests/test_worker.py @@ -116,15 +116,23 @@ def test__run_ok(worker, loop): ret.set_result(sock) worker.wsgi.make_handler.return_value.num_connections = 1 worker.cfg.max_requests = 100 + worker.cfg.is_ssl = True - with mock.patch('aiohttp.worker.asyncio') as m_asyncio: - m_asyncio.sleep = mock.Mock( - wraps=asyncio.coroutine(lambda *a, **kw: None)) - loop.run_until_complete(worker._run()) + ssl_context = mock.Mock() + with mock.patch('ssl.SSLContext', return_value=ssl_context): + with mock.patch('aiohttp.worker.asyncio') as m_asyncio: + m_asyncio.sleep = mock.Mock( + wraps=asyncio.coroutine(lambda *a, **kw: None)) + loop.run_until_complete(worker._run()) assert worker.notify.called assert worker.log.info.called + args, kwargs = loop.create_server.call_args + assert 'ssl' in kwargs + ctx = kwargs['ssl'] + assert ctx is ssl_context + def test__run_exc(worker, loop): with mock.patch('aiohttp.worker.os') as m_os: @@ -138,6 +146,7 @@ def test__run_exc(worker, loop): worker.log = mock.Mock() worker.loop = mock.Mock() worker.notify = mock.Mock() + worker.cfg.is_ssl = False with mock.patch('aiohttp.worker.asyncio.sleep') as m_sleep: slp = helpers.create_future(loop)