Skip to content

Commit

Permalink
Signals on_headers_sent and on_content_sent aio-libs#2313
Browse files Browse the repository at this point in the history
For now we are going to not implement the signals related to the
chunks uploaded. Once all MVP of the request tracing is implemented
we will improve it adding the proper signals to follow the upload
and download by chunk
  • Loading branch information
pfreixes committed Oct 25, 2017
1 parent 3af66fc commit 3115fac
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 8 deletions.
5 changes: 0 additions & 5 deletions aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,6 @@ def _request(self, method, url, *,
ssl_context=ssl_context, proxy_headers=proxy_headers,
on_headers_sent=self.on_request_headers_sent,
on_content_sent=self.on_request_content_sent,
on_content_chunk_sent=self.on_request_content_chunk_sent, # noqa
trace_context=trace_context)

# connection timeout
Expand Down Expand Up @@ -739,10 +738,6 @@ def on_request_createconn_end(self):
def on_request_headers_sent(self):
return self._on_request_headers_sent

@property
def on_request_content_chunk_sent(self):
return self._on_request_content_chunk_sent

@property
def on_request_content_sent(self):
return self._on_request_content_sent
Expand Down
5 changes: 2 additions & 3 deletions aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ def __init__(self, method, url, *,
timer=None, session=None, auto_decompress=True,
verify_ssl=None, fingerprint=None, ssl_context=None,
proxy_headers=None, on_headers_sent=None,
on_content_sent=None, on_content_chunk_sent=None,
trace_context=None):
on_content_sent=None, trace_context=None):

if verify_ssl is False and ssl_context is not None:
raise ValueError(
Expand Down Expand Up @@ -122,7 +121,6 @@ def __init__(self, method, url, *,

self._on_headers_sent = on_headers_sent
self._on_content_sent = on_content_sent
self._on_content_chunk_sent = on_content_chunk_sent
self._trace_context = trace_context

if loop.get_debug():
Expand Down Expand Up @@ -399,6 +397,7 @@ def write_bytes(self, writer, conn):

try:
if isinstance(self.body, payload.Payload):
print(self.body)
yield from self.body.write(writer)
else:
if isinstance(self.body, (bytes, bytearray)):
Expand Down
40 changes: 40 additions & 0 deletions tests/test_client_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,46 @@ def test_gen_netloc_no_port(make_request):
'012345678901234567890'


@asyncio.coroutine
def test_tracing(loop, conn):
trace_context = mock.Mock()
on_headers_sent = mock.Mock()
on_content_sent = mock.Mock()

req = ClientRequest(
'get',
URL('http://python.org'),
data=b'foo',
on_headers_sent=on_headers_sent,
on_content_sent=on_content_sent,
trace_context=trace_context,
loop=loop
)
resp = req.send(conn)
yield from req.close()
resp.close()

on_headers_sent.send.assert_called_with(trace_context)
on_content_sent.send.assert_called_with(trace_context)


@asyncio.coroutine
def test_tracing_no_content(loop, conn):
on_content_sent = mock.Mock()

req = ClientRequest(
'get',
URL('http://python.org'),
on_content_sent=on_content_sent,
loop=loop
)
resp = req.send(conn)
yield from req.close()
resp.close()

assert not on_content_sent.send.called


@asyncio.coroutine
def test_connection_header(loop, conn):
req = ClientRequest('get', URL('http://python.org'), loop=loop)
Expand Down
18 changes: 18 additions & 0 deletions tests/test_client_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,3 +585,21 @@ def test_request_tracing_proxies_connector_signals(loop):
id(connector.on_createconn_start)
assert id(session.on_request_createconn_end) ==\
id(connector.on_createconn_end)


@asyncio.coroutine
def test_request_tracing_clientrequest_signals(loop):

class MyClientRequest(ClientRequest):

def __init__(self, *args, **kwargs):
super(MyClientRequest, self).__init__(*args, **kwargs)
MyClientRequest.on_headers_sent = self._on_headers_sent
MyClientRequest.on_content_sent = self._on_content_sent

trace_context = mock.Mock()

session = aiohttp.ClientSession(loop=loop, request_class=MyClientRequest)
yield from session.get('http://example.com', trace_context=trace_context)
assert MyClientRequest.on_headers_sent == session.on_request_headers_sent
assert MyClientRequest.on_content_sent == session.on_request_content_sent

0 comments on commit 3115fac

Please sign in to comment.