-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Pausable response streams #1179
Changes from 2 commits
ec0b99f
1805946
7af87dd
ec30d9e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
|
||
from sanic import Sanic | ||
from sanic.response import HTTPResponse, stream, StreamingHTTPResponse, file, file_stream, json | ||
from sanic.server import HttpProtocol | ||
from sanic.testing import HOST, PORT | ||
from unittest.mock import MagicMock | ||
|
||
|
@@ -30,9 +31,10 @@ async def hello_route(request): | |
|
||
|
||
async def sample_streaming_fn(response): | ||
response.write('foo,') | ||
await response.write('foo,') | ||
await asyncio.sleep(.001) | ||
response.write('bar') | ||
await response.write('bar') | ||
|
||
|
||
|
||
def test_method_not_allowed(): | ||
|
@@ -168,20 +170,39 @@ def test_stream_response_includes_chunked_header(): | |
|
||
def test_stream_response_writes_correct_content_to_transport(streaming_app): | ||
response = StreamingHTTPResponse(sample_streaming_fn) | ||
response.transport = MagicMock(asyncio.Transport) | ||
response.protocol = MagicMock(HttpProtocol) | ||
response.protocol.transport = MagicMock(asyncio.Transport) | ||
|
||
async def mock_drain(): | ||
pass | ||
|
||
def mock_push_data(data): | ||
response.protocol.transport.write(data) | ||
|
||
response.protocol.push_data = mock_push_data | ||
response.protocol.drain = mock_drain | ||
|
||
@streaming_app.listener('after_server_start') | ||
async def run_stream(app, loop): | ||
await response.stream() | ||
assert response.transport.write.call_args_list[1][0][0] == ( | ||
# assert response.protocol.push_data.call_args_list[1][0][0] == ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these comments seem accidental -- did you mean to leave them here? They seem to be the exact same as the tests themselves There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TBH, I don't remember. |
||
# b'4\r\nfoo,\r\n' | ||
# ) | ||
assert response.protocol.transport.write.call_args_list[1][0][0] == ( | ||
b'4\r\nfoo,\r\n' | ||
) | ||
|
||
assert response.transport.write.call_args_list[2][0][0] == ( | ||
# assert response.protocol.push_data.call_args_list[2][0][0] == ( | ||
# b'3\r\nbar\r\n' | ||
# ) | ||
assert response.protocol.transport.write.call_args_list[2][0][0] == ( | ||
b'3\r\nbar\r\n' | ||
) | ||
|
||
assert response.transport.write.call_args_list[3][0][0] == ( | ||
# assert response.protocol.push_data.call_args_list[3][0][0] == ( | ||
# b'0\r\n\r\n' | ||
# ) | ||
assert response.protocol.transport.write.call_args_list[3][0][0] == ( | ||
b'0\r\n\r\n' | ||
) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR looks good. Hoever, won't this require an update to the docs to use the
await
syntax for streaming?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. You're right.
I changed the response-streaming example, but I forgot to change the response-streaming docs.