Skip to content

Commit

Permalink
Fix #2024: provide BaseRequest.loop attribute (#2132)
Browse files Browse the repository at this point in the history
* Fix #2024: provide BaseRequest.loop attribute

* Add missing changes
  • Loading branch information
asvetlov authored Jul 27, 2017
1 parent 6f9ffa7 commit 9964ddb
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 17 deletions.
10 changes: 6 additions & 4 deletions aiohttp/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,8 @@ def make_mocked_request(method, path, headers=None, *,
payload=sentinel,
sslcontext=None,
secure_proxy_ssl_header=None,
client_max_size=1024**2):
client_max_size=1024**2,
loop=...):
"""Creates mocked web.Request testing purposes.
Useful in unit tests, when spinning full web server is overkill or
Expand All @@ -511,8 +512,9 @@ def make_mocked_request(method, path, headers=None, *,
"""

task = mock.Mock()
loop = mock.Mock()
loop.create_future.return_value = ()
if loop is ...:
loop = mock.Mock()
loop.create_future.return_value = ()

if version < HttpVersion(1, 1):
closing = True
Expand Down Expand Up @@ -566,7 +568,7 @@ def timeout(*args, **kw):
time_service.timeout.side_effect = timeout

req = Request(message, payload,
protocol, payload_writer, time_service, task,
protocol, payload_writer, time_service, task, loop,
secure_proxy_ssl_header=secure_proxy_ssl_header,
client_max_size=client_max_size)

Expand Down
1 change: 1 addition & 0 deletions aiohttp/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ def _make_request(self, message, payload, protocol, writer, task,
_cls=web_request.Request):
return _cls(
message, payload, protocol, writer, protocol._time_service, task,
self._loop,
secure_proxy_ssl_header=self._secure_proxy_ssl_header,
client_max_size=self._client_max_size)

Expand Down
2 changes: 1 addition & 1 deletion aiohttp/web_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ def handle_error(self, request, status=500, exc=None, message=None):
def handle_parse_error(self, writer, status, exc=None, message=None):
request = BaseRequest(
ERROR, EMPTY_PAYLOAD,
self, writer, self._time_service, None)
self, writer, self._time_service, None, self._loop)

resp = self.handle_error(request, status, exc, message)
yield from resp.prepare(request)
Expand Down
7 changes: 7 additions & 0 deletions aiohttp/web_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class BaseRequest(collections.MutableMapping, HeadersMixin):
hdrs.METH_TRACE, hdrs.METH_DELETE}

def __init__(self, message, payload, protocol, writer, time_service, task,
loop,
*, secure_proxy_ssl_header=None, client_max_size=1024**2):
self._message = message
self._protocol = protocol
Expand All @@ -84,6 +85,7 @@ def __init__(self, message, payload, protocol, writer, time_service, task,
self._cache = {}
self._task = task
self._client_max_size = client_max_size
self._loop = loop

def clone(self, *, method=sentinel, rel_url=sentinel,
headers=sentinel):
Expand Down Expand Up @@ -120,6 +122,7 @@ def clone(self, *, method=sentinel, rel_url=sentinel,
self._writer,
self._time_service,
self._task,
self._loop,
secure_proxy_ssl_header=self._secure_proxy_ssl_header)

@property
Expand All @@ -146,6 +149,10 @@ def message(self):
def rel_url(self):
return self._rel_url

@property
def loop(self):
return self._loop

# MutableMapping API

def __getitem__(self, key):
Expand Down
2 changes: 1 addition & 1 deletion aiohttp/web_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def connection_lost(self, handler, exc=None):
def _make_request(self, message, payload, protocol, writer, task):
return BaseRequest(
message, payload, protocol, writer,
protocol.time_service, task)
protocol.time_service, task, self._loop)

@asyncio.coroutine
def shutdown(self, timeout=None):
Expand Down
2 changes: 1 addition & 1 deletion aiohttp/web_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def prepare(self, request):
return payload_writer

def _pre_start(self, request):
self._loop = request.app.loop
self._loop = request.loop

try:
status, headers, _, writer, protocol = do_handshake(
Expand Down
1 change: 1 addition & 0 deletions changes/2024.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Provide `BaseRequest.loop` attribute
6 changes: 5 additions & 1 deletion docs/testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,8 @@ conditions that hard to reproduce on real server::
transport=sentinel, \
payload=sentinel, \
sslcontext=None, \
secure_proxy_ssl_header=None)
secure_proxy_ssl_header=None,
loop=...)

Creates mocked web.Request testing purposes.

Expand Down Expand Up @@ -417,6 +418,9 @@ conditions that hard to reproduce on real server::
combination that signifies a request is secure.
:type secure_proxy_ssl_header: tuple

:param loop: An event loop instance, mocked loop by default.
:type secure_proxy_ssl_header: :class:`asyncio.AbstractEventLoop`

:return: :class:`aiohttp.web.Request` object.


Expand Down
27 changes: 19 additions & 8 deletions docs/web_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,14 @@ and :ref:`aiohttp-web-signals` handlers.
if peername is not None:
host, port = peername

.. attribute:: loop

An event loop instance used by HTTP request handling.

Read-only :class:`asyncio.AbstractEventLoop` property.

.. versionadded:: 2.3

.. attribute:: cookies

A multidict of all request's cookies.
Expand Down Expand Up @@ -820,7 +828,8 @@ Response
WebSocketResponse
^^^^^^^^^^^^^^^^^

.. class:: WebSocketResponse(*, timeout=10.0, receive_timeout=None, autoclose=True, \
.. class:: WebSocketResponse(*, timeout=10.0, receive_timeout=None, \
autoclose=True, \
autoping=True, heartbeat=None, protocols=())

Class for handling server-side websockets, inherited from
Expand All @@ -834,8 +843,8 @@ WebSocketResponse
.. versionadded:: 1.3.0

To enable back-pressure from slow websocket clients treat methods
`ping()`, `pong()`, `send_str()`, `send_bytes()`, `send_json()` as coroutines.
By default write buffer size is set to 64k.
`ping()`, `pong()`, `send_str()`, `send_bytes()`, `send_json()` as
coroutines. By default write buffer size is set to 64k.

:param bool autoping: Automatically send
:const:`~aiohttp.WSMsgType.PONG` on
Expand All @@ -850,12 +859,14 @@ WebSocketResponse

.. versionadded:: 1.3.0

:param float heartbeat: Send `ping` message every `heartbeat` seconds
and wait `pong` response, close connection if `pong` response
is not received.
:param float heartbeat: Send `ping` message every `heartbeat`
seconds and wait `pong` response, close
connection if `pong` response is not
received.

:param float receive_timeout: Timeout value for `receive` operations.
Default value is None (no timeout for receive operation)
:param float receive_timeout: Timeout value for `receive`
operations. Default value is None
(no timeout for receive operation)

.. versionadded:: 0.19

Expand Down
3 changes: 2 additions & 1 deletion tests/test_web_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ def maker(method, path, headers=None, protocols=False):

return make_mocked_request(
method, path, headers,
app=app, protocol=protocol, payload_writer=writer)
app=app, protocol=protocol, payload_writer=writer,
loop=app.loop)

return maker

Expand Down

0 comments on commit 9964ddb

Please sign in to comment.